flask啓動過程+實現網頁跳轉

目錄

 

1. 激活虛擬環境

2.設置變量

3. html文件

4. myflask.py文件內容

5. 執行flask

6.第二種執行flask的方法


1. 激活虛擬環境

cd到工程目錄

E:\workspace\ffmpeg\bin>cd E:\workspace\stock\hdf5test

E:\workspace\stock\hdf5test>
E:\workspace\stock\hdf5test>venv\Scripts\activate

2.設置變量

(venv) E:\workspace\stock\hdf5test>set FLASK_APP=myflask.py

myflask.py在工程目錄E:\workspace\stock\hdf5test\下,與venv虛擬環境文件夾同層目錄。

3. html文件

工程目錄E:\workspace\stock\hdf5test\下新建目錄static,放入index.html

<html>

<body>

<p>
<a href="/config">config</a>
</p>

<p>
<a href="/query">query</a>
</p>

</body>
</html>

4. myflask.py文件內容

# coding=gbk

from flask import Flask
app = Flask(__name__)

#1.修改配置
@app.route('/config')
def config():
    return '配置你的配置'

#2.查詢信息
@app.route('/query')
def query():
    return '你要的結果'

@app.route('/')
def index():
    print('index')
    return app.send_static_file('index.html')

if __name__ == "__main__":
    print('this is main!')
    #use_reloader=False  防止執行兩遍
    app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=False)

5. 執行flask

(venv) E:\workspace\stock\hdf5test>flask run -h 0.0.0.0 -p 5000

爲了讓其他PC可以訪問,加上-h -p

6.第二種執行flask的方法

(venv) E:\workspace\stock\hdf5test>python myflask.py
this is main!
 * Serving Flask app "myflask" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployme
nt.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
this is main!
 * Debugger is active!
 * Debugger PIN: 254-244-829
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

用python myflask.py來啓動flask,會進入main函數:

if __name__ == "__main__":
    print('this is main!')
    #use_reloader=False  防止執行兩遍
    app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=False)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章