MCPcopy
hub / github.com/pallets/flask / shell_command

Function shell_command

src/flask/cli.py:1009–1053  ·  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

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