It is a tedious task to import Python-related objects (DB instances and models) every time a shell session is started. To avoid repeating the import all the time, we can do some configuration so that Flask-script's shell commands are automatically imported into specific objects.
Flask's Development Web server supports many startup settings options, but can only be passed as a parameter to the App.run () function in the script. This approach is not very convenient, and the ideal way to pass the setup options is to use command-line arguments.
Flask-script is a Flask extension that adds a command-line parser to the Flask program. Flask-script comes with a set of common options, and also supports custom commands.
Flask-script extensions use PIP installation:
(flask)$ pip install flask-script
Creating an Flask-script instance
First, create a Python module to run your command script. Can be arbitrarily named, such as manage.py.
In your manage.py file, you must have a manager instance. The manager class will track the invocation of all commands and processes invoked at the command line:
from flask.ext.script import Managerapp = Flask(__name__)# configure your appmanager = Manager(app)if __name__ == "__main__": manager.run()
Run the Flask-script command
Calling Manager.run () launches the manager instance to receive commands from the command line.
Running manage.py now displays a usage message:
(flask)$ python manage.py usage: manage.py [-?] {test,shell,db,runserver} ...positional arguments: {test,shell,db,runserver} shell Runs a Python shell inside Flask application context. (在Flask应用上下文中运行Python shell) db Perform database migrations runserver Runs the Flask development server i.e. app.run() (运行Flask开发服务器: app.run())optional arguments: -?, --help show this help message and exit
Shell commands are used to start a Python shell session in the context of a program. You can use this session to run maintenance tasks or tests, and you can debug exceptions.
Start the Web server
(flask) $ python manage.py runserver--helpusage:manage.py runserver [-?] [-H HOST] [-P PORT] [--threaded] [--processes processes] [--passthrough-errors] [-d] [-d] [-R] [-R] Runs the Flask development server i.e. App.run () (Running Flask Development Server: App.run ()) Optional arguments:-?,--Help show thisHelp Message andExit-h HOST,--host host-p port,--port port--threaded--processes processes--passthrough-errors-d,--debugEnable the Werkzeug debuggerIn production code)-D,--no-debugDisable the Werkzeug debugger-r,--reload monitor Python filesFor changes (not 100{' Const ': True,' Help ':' Monitor Python files for changes (not 100% safe for production use) ', ' option_strings ': [ '-R ', '--reload '], ' dest ': ' Use_reloader ', ' Nargs ': 0, ' choices ': None, ' default ': None, ' prog ': ' manage.py Runserver ', Span class= "hljs-string" > ' container ': <argparse._argumentgroup object at 0x7f6c48b20150>, ' type ': None, ' Metavar ': None}afe for production Use)-R,--no-reload do not monitor Python files for changes
The--host parameter is a useful option that tells the Web server on which network interface to listen for connections from the client.
The following command allows the Web server to listen for connections on the public network interface, allowing other computers in the same network to connect to the server:
$python manage.py runserver --host 0.0.0.0 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) * Restarting with stat
The Web server can now be accessed using any computer in the Http://a.b.c.d:5000/network, where "A.B.C.D" is the network IP address of the computer on which the server resides.
If you use Pycharm to program, then you need to set the Pycharm startup item. Run->edit configurations, pop up the following window and follow the contents of the window.
Links: http://www.jianshu.com/p/ee500190dc97
Source: Pinterest
Integrated Python Shell