Microsoft Windows [版本 10.0.18362.778]
© 2019 Microsoft Corporation。保留所有权利。
C:\Users\pc>F:
F:>cd python_ven_demo
F:\python_ven_demo>
F:\python_ven_demo>python -m venv .
F:\python_ven_demo>cd Scripts
F:\python_ven_demo\Scripts>activate.bat
(python_ven_demo) F:\python_ven_demo\Scripts>pip install Flask
上面的pip install Flask失败,换一个源就好了:
(python_ven_demo) F:\python_ven_demo\Scripts>pip install flask -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
Collecting flask
Downloading http://pypi.doubanio.com/packages/f2/28/2a03252dfb9ebf377f40fba6a7841b47083260bf8bd8e737b0c6952df83f/Flask-1.1.2-py2.py3-none-any.whl (94kB)
100% |████████████████████████████████| 102kB 3.9MB/s
Collecting Jinja2>=2.10.1 (from flask)
Downloading http://pypi.doubanio.com/packages/30/9e/f663a2aa66a09d838042ae1a2c5659828bb9b41ea3a6efa20a20fd92b121/Jinja2-2.11.2-py2.py3-none-any.whl (125kB)
100% |████████████████████████████████| 133kB 2.8MB/s
Collecting itsdangerous>=0.24 (from flask)
Downloading http://pypi.doubanio.com/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Werkzeug>=0.15 (from flask)
Downloading http://pypi.doubanio.com/packages/cc/94/5f7079a0e00bd6863ef8f1da638721e9da21e5bacee597595b318f71d62e/Werkzeug-1.0.1-py2.py3-none-any.whl (298kB)
100% |████████████████████████████████| 307kB 6.8MB/s
Collecting click>=5.1 (from flask)
Downloading http://pypi.doubanio.com/packages/d2/3d/fa76db83bf75c4f8d338c2fd15c8d33fdd7ad23a9b5e57eb6c5de26b430e/click-7.1.2-py2.py3-none-any.whl (82kB)
100% |████████████████████████████████| 92kB 3.1MB/s
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->flask)
Downloading http://pypi.doubanio.com/packages/12/10/8dbe995ab7e5d0471fd7aa90fdb756595ae9ef0ccf3707867b1b56254df8/MarkupSafe-1.1.1-cp35-cp35m-win_amd64.whl
Installing collected packages: MarkupSafe, Jinja2, itsdangerous, Werkzeug, click, flask
Successfully installed Jinja2-2.11.2 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 flask-1.1.2 itsdangerous-1.1.0
(python_ven_demo) F:\python_ven_demo\Scripts>
第一个hello world
# 首次使用
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'hello buddy!'
if __name__ == '__main__':
app.run()
在虚拟环境中运行:
结果:
’
把上面的 app.run()改成如下,可以开启
```python
app.run(debug=True)
Routing
Modern web applications use meaningful URLs to help users. Users are more likely to like a page and come back if the page uses a meaningful URL they can remember and use to directly visit a page.
Use the route() decorator to bind a function to a URL.
@app.route('/')
def index():
return 'Index Page'
@app.route('/hello')
def hello():
return 'Hello, World'
You can do more! You can make parts of the URL dynamic and attach multiple rules to a function.
URL dynamic
https://www.tutorialspoint.com/flask/flask_variable_rules.htm
The Variables in the flask is used to build a URL dynamically by adding the variable parts to the rule parameter. This variable part is marked as. It is passed as keyword argument. See the example below
from flask import Flask
app = Flask(__name__)
# routing the decorator function hello_name
@app.route('/hello/<name>')
def hello_name(name):
return 'Hello %s!' % name
if __name__ == '__main__':
app.run(debug = True)
Output:
Hello GeeksforGeeks!
In the above example, the parameter of route() decorator contains the variable part attached to the URL ‘/hello’ as an argument. Hence, if URL like http://localhost:5000/hello/GeeksforGeeks is entered then ‘GeeksforGeeks’ will be passed to the hello() function as an argument.
In addition to the default string variable part, other data types like int, float, and path(for directory separator channel which can take slash) are also used. The URL rules of Flask are based on Werkzeug’s routing module. This ensures that the URLs formed are unique and based on precedents laid down by Apache.
from flask import Flask
app = Flask(__name__)
@app.route('/blog/<postID>')
def show_blog(postID):
return 'Blog Number %d' % postID
@app.route('/rev/<revNo>')
def revision(revNo):
return 'Revision Number %f' % revNo
if __name__ == '__main__':
app.run()
# say the URL is http://localhost:5000/blog/555
Output :
Blog Number 555
# Enter this URL in the browser ? http://localhost:5000/rev/1.1
Revision Number: 1.100000
Building URL in FLask:
Dynamic Building of the URL for a specific function is done using url_for() function. The function accepts the name of the function as first argument, and one or more keyword arguments. See this example
from flask import Flask, redirect, url_for
app = Flask(__name__)
@app.route('/admin') #decorator for route(argument) function
def hello_admin(): #binding to hello_admin call
return 'Hello Admin'
@app.route('/guest/<guest>')
def hello_guest(guest): #binding to hello_guest call
return 'Hello %s as Guest' % guest
@app.route('/user/<name>')
def hello_user(name):
if name =='admin': #dynamic binding of URL to function
return redirect(url_for('hello_admin'))