Flask-Script
Flask-Script is a Python extension for Flask that provided support for writing external scripts, including running a development server, a customized Python shell, database setup scripts, and other command-line tasks. The project's last release was in September 2017 (version 2.0.6), and its maintainers are no longer actively developing new features, only merging pull requests. It has largely been superseded by Flask's built-in command-line interface (Flask CLI), which uses the Click library.
Warnings
- breaking Flask-Script is explicitly not compatible with Flask 2.0 and later versions, leading to import errors or unexpected behavior.
- deprecated The Flask-Script library is no longer actively maintained; the last release was in 2017. Flask itself has provided a built-in command-line interface (Flask CLI) since version 0.11 (and extensively with Flask 1.0 and Click) that offers superior functionality and active development.
- deprecated The import path `flask.ext.script` is deprecated. While it might still work in some legacy setups, it signals an outdated approach.
- gotcha Using Flask-Script's `Server` command for a development server with automatic reloading can lead to "bizarre side-effects" like code executing twice or silent crashes. Flask's built-in CLI offers a more robust development server.
Install
-
pip install Flask-Script
Imports
- Manager
from flask.ext.script import Manager
from flask_script import Manager
- Command
from flask_script import Command
- Server
from flask_script import Server
- Shell
from flask_script import Shell
Quickstart
from flask import Flask
from flask_script import Manager, Command
app = Flask(__name__)
class HelloCommand(Command):
"""Says hello."""
def run(self):
print("Hello, world!")
manager = Manager(app)
manager.add_command("hello", HelloCommand())
if __name__ == '__main__':
manager.run()