zoukankan      html  css  js  c++  java
  • Flask 2 程序的基本结构1

    NOTE

    1.初始化:所有的Flask程序都必须创建一个程序实例.

    app = Flask(__name__) # 向Flask的构造函数传入参数__name__
    

    2.路由和视图函数(VF):

    Clients --(requests)--> Server --> Flask Object --(mapping/route)--> View Function --(responses)--> Server --> Clients

    # 通过Object提供的app.route修饰器将函数index()注册为View Function
    @app.route('/') # 路由:URL 
    def index() :
            return '<h1>HelloWorld!<h1>'
    

    3.启动服务器:使用run方法。

    
    if __name__ == '__main__':
            app.run(debug = True) # debug模式
    

    4.执行

    Hello.py

    #!/usr/bin/env python
    
    # import module
    from flask import Flask
    
    # Creat an Object
    app = Flask(__name__)
    
    # URL Route
    @app.route('/')
    def index() :
            return '<h1>HelloWorld!<h1>'
    
    if __name__ == '__main__':
            app.run(debug = True)
    
    (venv) sh-3.2# python hello.py 
     * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
     * Restarting with stat
     * Debugger is active!
     * Debugger pin code: 195-717-834
    127.0.0.1 - - [15/Feb/2017 22:43:35] "GET / HTTP/1.1" 200 -
    127.0.0.1 - - [15/Feb/2017 22:43:36] "GET /favicon.ico HTTP/1.1" 404 -
    

    效果:

    2017/2/16

  • 相关阅读:
    leetcode Super Ugly Number
    leetcode Find Median from Data Stream
    leetcode Remove Invalid Parentheses
    leetcode Range Sum Query
    leetcode Range Sum Query
    leetcode Minimum Height Trees
    hdu 3836 Equivalent Sets
    hdu 1269 迷宫城堡
    hud 2586 How far away ?
    poj 1330 Nearest Common Ancestors
  • 原文地址:https://www.cnblogs.com/qq952693358/p/6404060.html
Copyright © 2011-2022 走看看