shell.rst
This file
Path
docs/shell.rst
101 lines · 3.6 KiB
Last changed by
remove unused ref directivesDavid Lordbefore adoption171aabc
History of this fileTake it
1Working with the Shell2======================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.2324Generally it's recommended that you read the :doc:`reqcontext`25chapter of the documentation first.2627Command Line Interface28----------------------2930Starting with Flask 0.11 the recommended way to work with the shell is the31``flask shell`` command which does a lot of this automatically for you.32For instance the shell is automatically initialized with a loaded33application context.3435For more information see :doc:`/cli`.3637Creating a Request Context38--------------------------3940The easiest way to create a proper request context from the shell is by41using the :attr:`~flask.Flask.test_request_context` method which creates42us a :class:`~flask.ctx.RequestContext`:4344>>> ctx = app.test_request_context()4546Normally you would use the ``with`` statement to make this request object47active, but in the shell it's easier to use the48:meth:`~flask.ctx.RequestContext.push` and49:meth:`~flask.ctx.RequestContext.pop` methods by hand:5051>>> ctx.push()5253From that point onwards you can work with the request object until you54call `pop`:5556>>> ctx.pop()5758Firing Before/After Request59---------------------------6061By just creating a request context, you still don't have run the code that62is normally run before a request. This might result in your database63being unavailable if you are connecting to the database in a64before-request callback or the current user not being stored on the65:data:`~flask.g` object etc.6667This however can easily be done yourself. Just call68:meth:`~flask.Flask.preprocess_request`:6970>>> ctx = app.test_request_context()71>>> ctx.push()72>>> app.preprocess_request()7374Keep in mind that the :meth:`~flask.Flask.preprocess_request` function75might return a response object, in that case just ignore it.7677To shutdown a request, you need to trick a bit before the after request78functions (triggered by :meth:`~flask.Flask.process_response`) operate on79a response object:8081>>> app.process_response(app.response_class())82<Response 0 bytes [200 OK]>83>>> ctx.pop()8485The functions registered as :meth:`~flask.Flask.teardown_request` are86automatically called when the context is popped. So this is the perfect87place to automatically tear down resources that were needed by the request88context (such as database connections).899091Further Improving the Shell Experience92--------------------------------------9394If you like the idea of experimenting in a shell, create yourself a module95with stuff you want to star import into your interactive session. There96you could also define some more helper methods for common things such as97initializing the database, dropping tables etc.9899Just put them into a module (like `shelltools`) and import from there:100101>>> from shelltools import *