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

The walk stopped after 200 commits: lines older than 7464e17 point at it.

Merge branch '2.2.x'David Lordbefore adoption7464e17

lines 1–3why, one step back
1Working with the Shell
2======================3

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

lines 4–7why, one step back
4One of the reasons everybody loves Python is the interactive shell. It allows
5you 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 branch '2.2.x'David Lordbefore adoption7464e17

line 8why, one step back
8

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

line 9why, one step back
9.. code-block:: text

Merge branch '2.2.x'David Lordbefore adoption7464e17

line 10why, one step back
10

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

line 11why, one step back
11 $ flask shell

Merge branch '2.2.x'David Lordbefore adoption7464e17

lines 12–15why, one step back
12
13Creating a Request Context14--------------------------15

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

lines 16–20why, one step back
16``flask shell`` pushes an app context automatically, so :data:`.current_app` and
17:data:`.g` are already available. However, there is no HTTP request being18handled in the shell, so :data:`.request` and :data:`.session` are not yet19available.20

Merge branch '2.2.x'David Lordbefore adoption7464e17

lines 21–26why, one step back
21The easiest way to create a proper request context from the shell is by
22using the :attr:`~flask.Flask.test_request_context` method which creates23us a :class:`~flask.ctx.RequestContext`:2425>>> ctx = app.test_request_context()26

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

lines 27–29why, one step back
27Normally you would use the ``with`` statement to make this context active, but
28in the shell it's easier to call :meth:`~.RequestContext.push` and29:meth:`~.RequestContext.pop` manually:

Merge branch '2.2.x'David Lordbefore adoption7464e17

lines 30–32why, one step back
30
31>>> ctx.push()32

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

lines 33–34why, one step back
33From that point onwards you can work with the request object until you call
34``pop``:

Merge branch '2.2.x'David Lordbefore adoption7464e17

lines 35–81why, one step back
35
36>>> 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 *