Linux command
flask 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Run the development server
flask run
Run with debug mode
flask run --debug
Run on a specific host and port
flask run --host [0.0.0.0] --port [8080]
Start an interactive Python shell
flask shell
List all routes
flask routes
Initialize a database
flask db init
Run database migrations
flask db upgrade
Set the application module
FLASK_APP=[app.py] flask run
说明
flask is the command-line interface for Flask, a popular Python web microframework. It provides commands for running development servers, opening application shells, and executing custom commands defined by Flask extensions or the application. The CLI discovers the Flask application through the FLASK_APP environment variable or --app option. It supports loading from modules (app), module:app patterns (myapp:create_app()), or factory functions. The development server (flask run) includes an auto-reloader that restarts when code changes and an interactive debugger that appears in the browser on errors. These features should only be used in development, never in production. Flask extensions often add their own CLI commands. Flask-Migrate adds flask db commands for database migrations, Flask-Admin might add admin commands, etc. Applications can define custom commands using the @app.cli.command() decorator. The shell command provides an interactive Python session with the application and its context pre-loaded, useful for testing and debugging.
参数
- --app _module_
- Specify the Flask application module (or set FLASK_APP environment variable).
- --debug / --no-debug
- Enable/disable debug mode with auto-reloader and debugger.
- run _--host host_ _--port port_
- Run the development server.
- shell
- Open an interactive Python shell with application context.
- routes _--sort endpoint|methods|rule_
- Show all registered URL rules.
- --version
- Show Flask version.
- --help
- Show help for a command.
FAQ
What is the flask command used for?
flask is the command-line interface for Flask, a popular Python web microframework. It provides commands for running development servers, opening application shells, and executing custom commands defined by Flask extensions or the application. The CLI discovers the Flask application through the FLASK_APP environment variable or --app option. It supports loading from modules (app), module:app patterns (myapp:create_app()), or factory functions. The development server (flask run) includes an auto-reloader that restarts when code changes and an interactive debugger that appears in the browser on errors. These features should only be used in development, never in production. Flask extensions often add their own CLI commands. Flask-Migrate adds flask db commands for database migrations, Flask-Admin might add admin commands, etc. Applications can define custom commands using the @app.cli.command() decorator. The shell command provides an interactive Python session with the application and its context pre-loaded, useful for testing and debugging.
How do I run a basic flask example?
Run `flask run` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does --app _module_ do in flask?
Specify the Flask application module (or set FLASK_APP environment variable).