昨天在Flask 初始化數(shù)據(jù)的時候茵瀑,想使用命令的Flask命令添加自己想要的命令進行初始化數(shù)據(jù)庫操作指蚁,卻怎么都無法在flask命令添加自己的自定義命令,網(wǎng)上搜索的很多辦法灾螃,最后自己看了N多文章才找到方法,下面總結(jié)一下:
1宽涌、編寫自己的啟動flask app的 .py文件
2平夜、運行,啟動flask app
3卸亮、把自己的flask app加入到環(huán)境變量中
export FLASK_APP=你自己啟動flask app 的py文件的絕對路徑
4忽妒、命令行執(zhí)行
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.
就會發(fā)現(xiàn) Commands 出現(xiàn)了自己添加的自定義命令