<p><spanclass="versionmodified added">Added in version 0.3.</span></p>
</div>
</details><p>One of the reasons everybody loves Python is the interactive shell. It
basically allows you to execute Python commands in real time and
immediately get results back. Flask itself does not come with an
interactive shell, because it does not require any specific setup upfront,
just import your application and start playing around.</p>
<p>There are however some handy helpers to make playing around in the shell a
more pleasant experience. The main issue with interactive console
sessions is that you’re not triggering a request like a browser does which
means that <aclass="reference internal"href="api.html#flask.g"title="flask.g"><codeclass="xref py py-data docutils literal notranslate"><spanclass="pre">g</span></code></a>, <aclass="reference internal"href="api.html#flask.request"title="flask.request"><codeclass="xref py py-data docutils literal notranslate"><spanclass="pre">request</span></code></a> and others are not
available. But the code you want to test might depend on them, so what
can you do?</p>
<p>This is where some helper functions come in handy. Keep in mind however
that these functions are not only there for interactive shell usage, but
also for unit testing and other situations that require a faked request
context.</p>
<p>Generally it’s recommended that you read <aclass="reference internal"href="reqcontext.html"><spanclass="doc">The Request Context</span></a> first.</p>
<sectionid="command-line-interface">
<h2>Command Line Interface<aclass="headerlink"href="#command-line-interface"title="Link to this heading">¶</a></h2>
<p>Starting with Flask 0.11 the recommended way to work with the shell is the
<codeclass="docutils literal notranslate"><spanclass="pre">flask</span><spanclass="pre">shell</span></code> command which does a lot of this automatically for you.
For instance the shell is automatically initialized with a loaded
application context.</p>
<p>For more information see <aclass="reference internal"href="cli.html"><spanclass="doc">Command Line Interface</span></a>.</p>
</section>
<sectionid="creating-a-request-context">
<h2>Creating a Request Context<aclass="headerlink"href="#creating-a-request-context"title="Link to this heading">¶</a></h2>
<p>The easiest way to create a proper request context from the shell is by
using the <aclass="reference internal"href="api.html#flask.Flask.test_request_context"title="flask.Flask.test_request_context"><codeclass="xref py py-attr docutils literal notranslate"><spanclass="pre">test_request_context</span></code></a> method which creates
us a <aclass="reference internal"href="api.html#flask.ctx.RequestContext"title="flask.ctx.RequestContext"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">RequestContext</span></code></a>:</p>
<p>Keep in mind that the <aclass="reference internal"href="api.html#flask.Flask.preprocess_request"title="flask.Flask.preprocess_request"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">preprocess_request()</span></code></a> function
might return a response object, in that case just ignore it.</p>
<p>To shutdown a request, you need to trick a bit before the after request
functions (triggered by <aclass="reference internal"href="api.html#flask.Flask.process_response"title="flask.Flask.process_response"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">process_response()</span></code></a>) operate on
<h2>Further Improving the Shell Experience<aclass="headerlink"href="#further-improving-the-shell-experience"title="Link to this heading">¶</a></h2>
<p>If you like the idea of experimenting in a shell, create yourself a module
with stuff you want to star import into your interactive session. There
you could also define some more helper methods for common things such as
initializing the database, dropping tables etc.</p>
<p>Just put them into a module (like <codeclass="code docutils literal notranslate"><spanclass="pre">shelltools</span></code>) and import from there:</p>