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 fileTake it
The walk stopped after 200 commits: lines older than c9a5660 point at it.
Merge pull request #5054 from pallets/session-domainDavid Lordbefore adoptionc9a5660
lines 1–3why, one step back1Working with the Shell2======================3merge app and request context (#5812)David Lordbefore adoptionadf3636
lines 4–7why, one step back4One 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.Merge pull request #5054 from pallets/session-domainDavid Lordbefore adoptionc9a5660
line 8why, one step back8merge app and request context (#5812)David Lordbefore adoptionadf3636
line 9why, one step back9.. code-block:: textMerge pull request #5054 from pallets/session-domainDavid Lordbefore adoptionc9a5660
line 10why, one step back10merge app and request context (#5812)David Lordbefore adoptionadf3636
line 11why, one step back11 $ flask shellMerge pull request #5054 from pallets/session-domainDavid Lordbefore adoptionc9a5660
lines 12–15why, one step back1213Creating a Request Context14--------------------------15merge app and request context (#5812)David Lordbefore adoptionadf3636
lines 16–20why, one step back16``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.20Merge pull request #5054 from pallets/session-domainDavid Lordbefore adoptionc9a5660
lines 21–26why, one step back21The 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()26merge app and request context (#5812)David Lordbefore adoptionadf3636
lines 27–29why, one step back27Normally 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:Merge pull request #5054 from pallets/session-domainDavid Lordbefore adoptionc9a5660
lines 30–32why, one step back3031>>> ctx.push()32merge app and request context (#5812)David Lordbefore adoptionadf3636
lines 33–34why, one step back33From that point onwards you can work with the request object until you call34``pop``:Merge pull request #5054 from pallets/session-domainDavid Lordbefore adoptionc9a5660
lines 35–81why, one step back3536>>> 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 *