(1)安装:
pip3 install flask
(2)简单实例:
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
①导入flask包
②引入app:app=Flask(__name__)
③指定url:@app.route('/')
④指定主函数,在本机运行,其中可以指定IP和端口号(IP默认本机,PORT默认为5000)
app.run(host='0.0.0.0',port=8888)
⑤关闭服务器:ctrl+c
(3)变量规则:
通过向规格参数添加变量,变量标记为<variable-name>,也可以在规则里指定变量类型(int,float,path):<converter:variable-name> 传递给相关函数,如<int:port_id>:
@app.route('/user/<username>')
def show_user_profile(username):
# show the user profile for that user
return 'User %s' % username
@app.route('/post/<int:post_id>')
def show_post(post_id):
# show the post with the given id, the id is an integer
return 'Post %d' % post_id
参考:
https://www.cnblogs.com/ygh1229/p/6683229.html
flask学习手册:http://docs.jinkan.org/docs/flask/index.html
https://www.w3cschool.cn/flask/flask_variable_rules.html