
Flask--简介
发布日期:2021-05-14 12:20:45
浏览次数:6
分类:精选文章
本文共 3723 字,大约阅读时间需要 12 分钟。
文章目录
1. flask简介
- 简介
作者是Armin Ronacher,出生于2010年,本来这个项目只是作者在愚人节的一个玩笑,后来由于非常受欢迎,进而成为一个正式的项目
- 特点
- 微框架、简洁、只做他需要做的,给开发者提供了很大的扩展性
- Flask和相应的插件写得很好,用起来很爽。比如使用SQLAlchemy的ORM操作数据库
- Flask的灵活度非常之高,他不会帮你做太多的决策,一些你都可以按照自己的意愿进行更改。比如:使用Flask开发数据库的时候,具体是使用SQLAlchemy还是MongoEngine,选择权完全掌握在你自己的手中,区别于Django。把默认的Jinija2模板引擎替换成其他模板引擎都是非常容易的。
- 官方文档
- http://www.pythondoc.com/flask/
2. Hello World
- flask安装
# 激活虚拟环境cd Virtualenv\flask-env\Scriptsactivate# 安装flaskpip install flask# 查看版本1.输入python 2.输入import flask3.输入flask.__version__
- 第一个flask项目
from flask import Flask# 传入__name__初始化一个Flask应用app = Flask(__name__)# 注册路由@app.route('/')def index(): return "Hello World"if __name__ == '__main__': # 启动flask服务 app.run()
此时即可在浏览器输入127.0.0.1:5000进行访问。
3. 启动flask服务
- 3步实现flask服务
# 导入flask包from flask import Flask# 通过实例化Flask的方式,实现一个WSGI应用app = Flask(__name__)# 通过WSGI协议实现flask应用和服务的的通信,启动一个服务app.run()
此时访问127.0.0.1:5000,服务会报404。是因为服务虽然开启了,但是浏览器找不到资源,此时就需要配置路由。
4. Flask实例化参数
通过ctrl + 左键点击Flask类进入源码,内容如下
class Flask(_PackageBoundObject): def __init__( self, import_name, # 包或者模块的名,但一般都是传递__name__(系统变量,指本py文件的文件名),让flask.helpers.get_root_path函数通过传入的这个名字确定程序的根目录,以便获得静态文件和模板文件的目录 static_url_path=None, # 静态文件目录的url路径 默认不写是与static_folder同名,远程静态文件时使用 static_folder="static", # 静态文件目录的路径 默认当前项目中的static目录 static_host=None, # 远程静态文件所用的Host地址,默认为空 host_matching=False, subdomain_matching=False, template_folder="templates", # template模板目录, 默认当前项目中的 templates 目录 instance_path=None, # 指向另一个Flask实例的路径 instance_relative_config=False, # 是否加载另一个实例的配置 root_path=None, # 主模块所在的目录的绝对路径,默认项目目录 ): pass
5. flask运行方式
- flask run
# demo.pyapp.run() # 默认(host=127.0.0.1, port=5000, DEBUG=False)等价于在控制台运行python demo.py
- flask 命令行
export FLASK_APP=demo.pyexport FLASK_ENV=developmentflask run
终端输入 flask ,即可显示命令参数。
Usage: flask [OPTIONS] COMMAND [ARGS]... A general utility script for Flask applications. Provides commands from Flask, extensions, and the application. Loads the application defined in the FLASK_APP environment variable, or from a wsgi.py file. Setting the FLASK_ENV environment variable to 'development' will enable debug mode. $ export FLASK_APP=hello.py $ export FLASK_ENV=development $ flask runOptions: --version Show the flask version --help Show this message and exit.Commands: routes Show the routes for the app. run Run a development server. shell Run a shell in the app context.
终端输入 flask run --help ,即可显示命令参数,如下
Options: -h, --host TEXT The interface to bind to. -p, --port INTEGER The port to bind to. --cert PATH Specify a certificate file to use HTTPS. --key FILE The key file to use when specifying a certificate. --reload / --no-reload Enable or disable the reloader. By default the reloader is active if debug is enabled. --debugger / --no-debugger Enable or disable the debugger. By default the debugger is active if debug is enabled. --eager-loading / --lazy-loader Enable or disable eager loading. By default eager loading is enabled if the reloader is disabled. --with-threads / --without-threads Enable or disable multithreading. --extra-files PATH Extra files that trigger a reload on change. Multiple paths are separated by ':'. --help Show this message and exit.
发表评论
最新留言
不错!
[***.144.177.141]2025年04月05日 04时20分55秒