Reputation: 13830
I'm attempting to fix a bug in flask-script. Django once had a similar bug, and an IPython developer recommended the use of TerminalIPythonApp instead of embed() so that extensions could be loaded correctly. However, the django shell did not allow customization of banner or namespace as flask-script does.
The user_ns and banner are normally passed to the shell's constructor, however the TerminalIPythonApp class instantiates its own shell without exposing any kwargs that might allow us to pass shell init params.
I've come up with the following solution, however it's a bit of a hacky kludge. Is there a better way?
from IPython.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=my_user_ns,
banner1=my_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()
Upvotes: 3
Views: 821
Reputation: 40390
With the new IPython 1.0 release, there's a top-level IPython.start_ipython()
function to launch the application. This will be exactly what you want, but unfortunately we slipped up for 1.0, and passing user_ns
into it currently has no effect. We'll do a bugfix release in a few weeks, probably, and then encourage people to start using the new function where possible.
Upvotes: 3