昨天在Flask 初始化数据的时候,想使用命令的Flask命令添加自己想要的命令进行初始化数据库操作,却怎么都无法在flask命令添加自己的自定义命令,网上搜索的很多办法,最后自己看了N多文章才找到方法,下面总结一下:
1、编写自己的启动flask app的 .py文件
2、运行,启动flask app
3、把自己的flask app加入到环境变量中
export FLASK_APP=你自己启动flask app 的py文件的绝对路径
4、命令行执行
flask --help
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 run
Options:
--version Show the flask version
--help Show this message and exit.
Commands:
init Initialize Albumy.
initdb Initialize the database.
routes Show the routes for the app.
run Runs a development server.
shell Runs a shell in the app context.
就会发现 Commands 出现了自己添加的自定义命令