Skip to content
Myr

shell.rst

This file

Path

docs/shell.rst

81 lines · 2.8 KiB

Last changed by

merge app and request context (#5812)David Lordbefore adoptionadf3636

History of this file

Take it

1Working with the Shell2======================34One of the reasons everybody loves Python is the interactive shell. It allows5you to play around with code in real time and immediately get results back.6Flask provides the ``flask shell`` CLI command to start an interactive Python7shell with some setup done to make working with the Flask app easier.89.. code-block:: text1011    $ flask shell1213Creating a Request Context14--------------------------1516``flask shell`` pushes an app context automatically, so :data:`.current_app` and17:data:`.g` are already available. However, there is no HTTP request being18handled in the shell, so :data:`.request` and :data:`.session` are not yet19available.2021The easiest way to create a proper request context from the shell is by22using the :attr:`~flask.Flask.test_request_context` method which creates23us a :class:`~flask.ctx.RequestContext`:2425>>> ctx = app.test_request_context()2627Normally you would use the ``with`` statement to make this context active, but28in the shell it's easier to call :meth:`~.RequestContext.push` and29:meth:`~.RequestContext.pop` manually:3031>>> ctx.push()3233From that point onwards you can work with the request object until you call34``pop``:3536>>> ctx.pop()3738Firing Before/After Request39---------------------------4041By just creating a request context, you still don't have run the code that42is normally run before a request.  This might result in your database43being unavailable if you are connecting to the database in a44before-request callback or the current user not being stored on the45:data:`~flask.g` object etc.4647This however can easily be done yourself.  Just call48:meth:`~flask.Flask.preprocess_request`:4950>>> ctx = app.test_request_context()51>>> ctx.push()52>>> app.preprocess_request()5354Keep in mind that the :meth:`~flask.Flask.preprocess_request` function55might return a response object, in that case just ignore it.5657To shutdown a request, you need to trick a bit before the after request58functions (triggered by :meth:`~flask.Flask.process_response`) operate on59a response object:6061>>> app.process_response(app.response_class())62<Response 0 bytes [200 OK]>63>>> ctx.pop()6465The functions registered as :meth:`~flask.Flask.teardown_request` are66automatically called when the context is popped.  So this is the perfect67place to automatically tear down resources that were needed by the request68context (such as database connections).697071Further Improving the Shell Experience72--------------------------------------7374If you like the idea of experimenting in a shell, create yourself a module75with stuff you want to star import into your interactive session.  There76you could also define some more helper methods for common things such as77initializing the database, dropping tables etc.7879Just put them into a module (like `shelltools`) and import from there:8081>>> from shelltools import *