Skip to content
Myr

shell.rst

This file

Path

docs/shell.rst

100 lines · 3.6 KiB

Last changed by

Merge branch '2.0.x'David Lordbefore adoption5684f48

History of this file

Take it

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

Merge pull request #3916 from greyli/fix-extdev-docsDavid Lordbefore adoptionaee3f3f

lines 1–23why, one step back
1Working with the Shell
2======================34.. versionadded:: 0.356One of the reasons everybody loves Python is the interactive shell. It7basically allows you to execute Python commands in real time and8immediately get results back. Flask itself does not come with an9interactive shell, because it does not require any specific setup upfront,10just import your application and start playing around.1112There are however some handy helpers to make playing around in the shell a13more pleasant experience. The main issue with interactive console14sessions is that you're not triggering a request like a browser does which15means that :data:`~flask.g`, :data:`~flask.request` and others are not16available. But the code you want to test might depend on them, so what17can you do?1819This is where some helper functions come in handy. Keep in mind however20that these functions are not only there for interactive shell usage, but21also for unit testing and other situations that require a faked request22context.23

Merge branch '2.0.x'David Lordbefore adoption5684f48

line 24why, one step back
24Generally it's recommended that you read :doc:`reqcontext` first.

Merge pull request #3916 from greyli/fix-extdev-docsDavid Lordbefore adoptionaee3f3f

lines 25–100why, one step back
25
26Command Line Interface27----------------------2829Starting with Flask 0.11 the recommended way to work with the shell is the30``flask shell`` command which does a lot of this automatically for you.31For instance the shell is automatically initialized with a loaded32application context.3334For more information see :doc:`/cli`.3536Creating a Request Context37--------------------------3839The easiest way to create a proper request context from the shell is by40using the :attr:`~flask.Flask.test_request_context` method which creates41us a :class:`~flask.ctx.RequestContext`:4243>>> ctx = app.test_request_context()4445Normally you would use the ``with`` statement to make this request object46active, but in the shell it's easier to use the47:meth:`~flask.ctx.RequestContext.push` and48:meth:`~flask.ctx.RequestContext.pop` methods by hand:4950>>> ctx.push()5152From that point onwards you can work with the request object until you53call `pop`:5455>>> ctx.pop()5657Firing Before/After Request58---------------------------5960By just creating a request context, you still don't have run the code that61is normally run before a request. This might result in your database62being unavailable if you are connecting to the database in a63before-request callback or the current user not being stored on the64:data:`~flask.g` object etc.6566This however can easily be done yourself. Just call67:meth:`~flask.Flask.preprocess_request`:6869>>> ctx = app.test_request_context()70>>> ctx.push()71>>> app.preprocess_request()7273Keep in mind that the :meth:`~flask.Flask.preprocess_request` function74might return a response object, in that case just ignore it.7576To shutdown a request, you need to trick a bit before the after request77functions (triggered by :meth:`~flask.Flask.process_response`) operate on78a response object:7980>>> app.process_response(app.response_class())81<Response 0 bytes [200 OK]>82>>> ctx.pop()8384The functions registered as :meth:`~flask.Flask.teardown_request` are85automatically called when the context is popped. So this is the perfect86place to automatically tear down resources that were needed by the request87context (such as database connections).888990Further Improving the Shell Experience91--------------------------------------9293If you like the idea of experimenting in a shell, create yourself a module94with stuff you want to star import into your interactive session. There95you could also define some more helper methods for common things such as96initializing the database, dropping tables etc.9798Just put them into a module (like `shelltools`) and import from there:99100>>> from shelltools import *