diff --git a/flask_script/commands.py b/flask_script/commands.py index b0b3dee..bb27eab 100644 --- a/flask_script/commands.py +++ b/flask_script/commands.py @@ -230,6 +230,35 @@ def get_context(self): """ return self.make_context() + def ipython(self, context): + try: + # 0.10.x + from IPython.Shell import IPShellEmbed + ipshell = IPShellEmbed(banner=self.banner) + ipshell(global_ns=dict(), local_ns=context) + except ImportError: + # 0.12+ + try: + from IPython.terminal import ipapp + except ImportError: + from IPython.frontend.terminal import ipapp + app = ipapp.TerminalIPythonApp.instance() + shell = ipapp.TerminalInteractiveShell.instance( + parent=app, + display_banner=False, + profile_dir=app.profile_dir, + ipython_dir=app.ipython_dir, + user_ns=context, + banner1=self.banner) + shell.configurables.append(app) + app.shell = shell + # shell has already been initialized, so we have to monkeypatch + # app.init_shell() to act as no-op + app.init_shell = lambda: None + app.initialize(argv=[]) + app.start() + + def run(self, no_ipython, no_bpython): """ Runs the shell. If no_bpython is False or use_bpython is True, then @@ -249,17 +278,8 @@ def run(self, no_ipython, no_bpython): pass if not no_ipython: - # Try IPython try: - try: - # 0.10.x - from IPython.Shell import IPShellEmbed - ipshell = IPShellEmbed(banner=self.banner) - ipshell(global_ns=dict(), local_ns=context) - except ImportError: - # 0.12+ - from IPython import embed - embed(banner1=self.banner, user_ns=context) + self.ipython(context) return except ImportError: pass