Exemplo n.º 1
0
def shell_command(plain=False):
    """Run a Python shell in the Flask application context."""
    try:
        import IPython
    except ImportError:
        IPython = None

    if IPython is not None and not plain:
        IPython.embed(banner1='', user_ns=current_app.make_shell_context())
    else:
        import code

        code.interact(banner='', local=current_app.make_shell_context())
Exemplo n.º 2
0
def shell():
    """Run a Python shell in the app context."""

    try:
        import IPython
    except ImportError:
        IPython = None

    if IPython is not None:
        IPython.embed(banner1="", user_ns=current_app.make_shell_context())
    else:
        import code

        code.interact(banner="", local=current_app.make_shell_context())
Exemplo n.º 3
0
def shell_command(plain=False):
    """Run a Python shell in the app context."""

    try:
        import IPython
    except ImportError:
        IPython = None

    if IPython is not None and not plain:
        IPython.embed(banner1='', user_ns=current_app.make_shell_context())
    else:
        import code

        code.interact(banner='', local=current_app.make_shell_context())
Exemplo n.º 4
0
def shell_command():
    """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.

    This code snippet is taken from Flask"s cli module and modified to
    run IPython and falls back to the normal shell if IPython is not
    available.
    """
    import code
    banner = "Python %s on %s\nInstance Path: %s" % (
        sys.version,
        sys.platform,
        current_app.instance_path,
    )
    ctx = {"db": db}

    # Support the regular Python interpreter startup script if someone
    # is using it.
    startup = os.environ.get("PYTHONSTARTUP")
    if startup and os.path.isfile(startup):
        with open(startup, "r") as f:
            eval(compile(f.read(), startup, "exec"), ctx)

    ctx.update(current_app.make_shell_context())

    try:
        import IPython
        IPython.embed(banner1=banner, user_ns=ctx)
    except ImportError:
        code.interact(banner=banner, local=ctx)
Exemplo n.º 5
0
def ipython_shell():
	"""Run the IPython shell.

	This handles the condition of IPython not being installed gracefully.

	Returns
	-------
	bool
		True if IPython was successfully imported and the shell run, False otherwise.
	"""
	try:
		from IPython import start_ipython
	except ImportError as exc:
		click.echo('Error importing IPython, falling back to builtin shell: %s' % exc)
		return False

	ctx = current_app.make_shell_context()

	from traitlets.config import Config
	c = Config()
	c.InteractiveShell.banner2 = "App: %s [%s]\nInstance: %s" % (
		current_app.import_name,
		current_app.env,
		current_app.instance_path,
	)

	start_ipython(argv=[], user_ns=ctx, config=c)

	return True
def shell_cmd(verbose, with_req_context):
    try:
        from IPython.terminal.ipapp import TerminalIPythonApp
    except ImportError:
        click.echo(
            cformat(
                '%{red!}You need to `pip install ipython` to use the SNMS shell'
            ))
        sys.exit(1)

    current_app.config['REPL'] = True  # disables e.g. memoize_request
    request_stats_request_started()
    context, info = _make_shell_context()
    banner = cformat('%{yellow!}SNMS v{} is ready for your commands').format(
        snms.__version__)
    if verbose:
        banner = '\n'.join(info + ['', banner])
    ctx = current_app.make_shell_context()
    ctx.update(context)
    # clearCache()
    stack = ExitStack()
    if with_req_context:
        stack.enter_context(
            current_app.test_request_context(base_url=config.BASE_URL))
    with stack:
        ipython_app = TerminalIPythonApp.instance(user_ns=ctx,
                                                  display_banner=False)
        ipython_app.initialize(argv=[])
        ipython_app.shell.show_banner(banner)
        ipython_app.start()
Exemplo n.º 7
0
def shell_command():
    """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.

    This code snippet is taken from Flask"s cli module and modified to
    run IPython and falls back to the normal shell if IPython is not
    available.
    """
    import code
    banner = "Python %s on %s\nInstance Path: %s" % (
        sys.version,
        sys.platform,
        current_app.instance_path,
    )
    ctx = {"db": db}

    # Support the regular Python interpreter startup script if someone
    # is using it.
    startup = os.environ.get("PYTHONSTARTUP")
    if startup and os.path.isfile(startup):
        with open(startup, "r") as f:
            eval(compile(f.read(), startup, "exec"), ctx)

    ctx.update(current_app.make_shell_context())

    try:
        import IPython
        IPython.embed(banner1=banner, user_ns=ctx)
    except ImportError:
        code.interact(banner=banner, local=ctx)
Exemplo n.º 8
0
def shell_command():
    ctx = current_app.make_shell_context()
    try:
        from IPython import start_ipython
        start_ipython(argv=(), user_ns=ctx)
    except ImportError:
        from code import interact
        interact(local=ctx)
Exemplo n.º 9
0
Arquivo: cli.py Projeto: sayoun/kort
def shell_command():
    """Starts an interactive shell."""
    ctx = current_app.make_shell_context()
    from kort.models import Links, DBSession  # noqa
    session = DBSession()  # noqa

    try:
        from IPython import embed
        from IPython.config.loader import Config
        cfg = Config()
        cfg.InteractiveShellEmbed.confirm_exit = False
        embed(config=cfg, banner1="Welcome to kort shell.")
    except ImportError:
        import code
        code.interact("kort shell", local=locals())
Exemplo n.º 10
0
def shell():
    """
    Attempts to run a shell using ipython/jupyter if installed, otherwise falls
    back to the regular python shell.
    """
    import code
    banner = f"Python {sys.version} on {sys.platform}\nInstance path: {current_app.instance_path}"  # noqa
    ctx = {"db": db}

    # Support the regular Python interpreter startup script if someone
    # is using it.
    startup = os.environ.get("PYTHONSTARTUP")
    if startup and os.path.isfile(startup):
        with open(startup, "r") as f:
            eval(compile(f.read(), startup, "exec"), ctx)

    ctx.update(current_app.make_shell_context())
    try:
        import IPython
        IPython.embed(banner1=banner, user_ns=ctx)
    except ImportError:
        code.interact(banner=banner, local=ctx)
Exemplo n.º 11
0
def shell(ipython_args):
    """Run 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.
    """
    # See: https://github.com/ei-grad/flask-shell-ipython
    config = load_default_config()
    config.TerminalInteractiveShell.banner1 = (
        f"Python {sys.version} on {sys.platform}\n"
        f"IPython: {IPython.__version__}\n"
        f"App: {app.import_name} [{app.env}]\n"
        f"Instance: {app.instance_path}\n")

    IPython.start_ipython(
        argv=ipython_args,
        user_ns=app.make_shell_context(),
        config=config,
    )
Exemplo n.º 12
0
def shell_cmd(verbose, with_req_context):
    try:
        from IPython.terminal.ipapp import TerminalIPythonApp
    except ImportError:
        click.echo(cformat('%{red!}You need to `pip install ipython` to use the Indico shell'))
        sys.exit(1)

    current_app.config['REPL'] = True  # disables e.g. memoize_request
    request_stats_request_started()
    context, info = _make_shell_context()
    banner = cformat('%{yellow!}Indico v{} is ready for your commands').format(indico.__version__)
    if verbose:
        banner = '\n'.join(info + ['', banner])
    ctx = current_app.make_shell_context()
    ctx.update(context)
    clearCache()
    stack = ExitStack()
    if with_req_context:
        stack.enter_context(current_app.test_request_context(base_url=config.BASE_URL))
    with stack:
        ipython_app = TerminalIPythonApp.instance(user_ns=ctx, display_banner=False)
        ipython_app.initialize(argv=[])
        ipython_app.shell.show_banner(banner)
        ipython_app.start()
Exemplo n.º 13
0
def shell_command():
    ctx = current_app.make_shell_context()
    interact(local=ctx)
Exemplo n.º 14
0
def shell():
    import IPython
    IPython.embed(user_ns=current_app.make_shell_context())
Exemplo n.º 15
0
def shell_command():
    ctx = current_app.make_shell_context()
    interact(local=ctx)