MCPcopy Create free account
hub / github.com/pallets/flask / shell_command

Function shell_command

src/flask/cli.py:1007–1051  ·  view source on GitHub ↗

Run an interactive Python shell in the context of a given Flask application. The application will populate the default namespace of this shell according to its configuration. This is useful for executing small snippets of management code without having to manually configure the app

()

Source from the content-addressed store, hash-verified

1005@click.command("shell", short_help="Run a shell in the app context.")
1006@with_appcontext
1007def shell_command() -> None:
1008 """Run an interactive Python shell in the context of a given
1009 Flask application. The application will populate the default
1010 namespace of this shell according to its configuration.
1011
1012 This is useful for executing small snippets of management code
1013 without having to manually configure the application.
1014 """
1015 import code
1016
1017 banner = (
1018 f"Python {sys.version} on {sys.platform}\n"
1019 f"App: {current_app.import_name}\n"
1020 f"Instance: {current_app.instance_path}"
1021 )
1022 ctx: dict[str, t.Any] = {}
1023
1024 # Support the regular Python interpreter startup script if someone
1025 # is using it.
1026 startup = os.environ.get("PYTHONSTARTUP")
1027 if startup and os.path.isfile(startup):
1028 with open(startup) as f:
1029 eval(compile(f.read(), startup, "exec"), ctx)
1030
1031 ctx.update(current_app.make_shell_context())
1032
1033 # Site, customize, or startup script can set a hook to call when
1034 # entering interactive mode. The default one sets up readline with
1035 # tab and history completion.
1036 interactive_hook = getattr(sys, "__interactivehook__", None)
1037
1038 if interactive_hook is not None:
1039 try:
1040 import readline
1041 from rlcompleter import Completer
1042 except ImportError:
1043 pass
1044 else:
1045 # rlcompleter uses __main__.__dict__ by default, which is
1046 # flask.__main__. Use the shell context instead.
1047 readline.set_completer(Completer(ctx).complete)
1048
1049 interactive_hook()
1050
1051 code.interact(banner=banner, local=ctx)
1052
1053
1054@click.command("routes", short_help="Show the routes for the app.")

Callers

nothing calls this directly

Calls 2

make_shell_contextMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected