Working with the Shell

Changelog

New in version 0.3.

One of the reasons everybody loves Python is the interactive shell. Itbasically allows you to execute Python commands in real time andimmediately get results back. Flask itself does not come with aninteractive shell, because it does not require any specific setup upfront,just import your application and start playing around.

There are however some handy helpers to make playing around in the shell amore pleasant experience. The main issue with interactive consolesessions is that you’re not triggering a request like a browser does whichmeans that g, request and others are notavailable. But the code you want to test might depend on them, so whatcan you do?

This is where some helper functions come in handy. Keep in mind howeverthat these functions are not only there for interactive shell usage, butalso for unit testing and other situations that require a faked requestcontext.

Generally it’s recommended that you read the The Request Contextchapter of the documentation first.

Command Line Interface

Starting with Flask 0.11 the recommended way to work with the shell is theflask shell command which does a lot of this automatically for you.For instance the shell is automatically initialized with a loadedapplication context.

For more information see Command Line Interface.

Creating a Request Context

The easiest way to create a proper request context from the shell is byusing the test_request_context method which createsus a RequestContext:

  1. >>> ctx = app.test_request_context()

Normally you would use the with statement to make this request objectactive, but in the shell it’s easier to use thepush() andpop() methods by hand:

  1. >>> ctx.push()

From that point onwards you can work with the request object until youcall pop:

  1. >>> ctx.pop()

Firing Before/After Request

By just creating a request context, you still don’t have run the code thatis normally run before a request. This might result in your databasebeing unavailable if you are connecting to the database in abefore-request callback or the current user not being stored on theg object etc.

This however can easily be done yourself. Just callpreprocess_request():

  1. >>> ctx = app.test_request_context()
  2. >>> ctx.push()
  3. >>> app.preprocess_request()

Keep in mind that the preprocess_request() functionmight return a response object, in that case just ignore it.

To shutdown a request, you need to trick a bit before the after requestfunctions (triggered by process_response()) operate ona response object:

  1. >>> app.process_response(app.response_class())
  2. <Response 0 bytes [200 OK]>
  3. >>> ctx.pop()

The functions registered as teardown_request() areautomatically called when the context is popped. So this is the perfectplace to automatically tear down resources that were needed by the requestcontext (such as database connections).

Further Improving the Shell Experience

If you like the idea of experimenting in a shell, create yourself a modulewith stuff you want to star import into your interactive session. Thereyou could also define some more helper methods for common things such asinitializing the database, dropping tables etc.

Just put them into a module (like shelltools) and import from there:

  1. >>> from shelltools import *