forked from ei-grad/flask-shell-ipython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask_shell_ipython.py
More file actions
45 lines (37 loc) · 1.34 KB
/
flask_shell_ipython.py
File metadata and controls
45 lines (37 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import sys
import click
from flask.cli import with_appcontext
@click.command(context_settings=dict(ignore_unknown_options=True))
@click.argument('ipython_args', nargs=-1, type=click.UNPROCESSED)
@with_appcontext
def shell(ipython_args):
"""Runs a shell in the app context.
Runs an interactive Python shell in the context of a given
Flask application. The application will populate the default
namespace of this shell according to it's configuration.
This is useful for executing small snippets of management code
without having to manually configuring the application.
"""
import IPython
from IPython.terminal.ipapp import load_default_config
from traitlets.config.loader import Config
from flask.globals import _app_ctx_stack
app = _app_ctx_stack.top.app
if 'IPYTHON_CONFIG' in app.config:
config = Config(app.config['IPYTHON_CONFIG'])
else:
config = load_default_config()
config.TerminalInteractiveShell.banner1 = '''Python %s on %s
IPython: %s
App: %s [%s]
Instance: %s''' % (sys.version,
sys.platform,
IPython.__version__,
app.import_name,
app.env,
app.instance_path)
IPython.start_ipython(
argv=ipython_args,
user_ns=app.make_shell_context(),
config=config,
)