[IPython-dev] User Namespace

Fernando Perez fperez.net at gmail.com
Tue Jan 3 23:49:15 EST 2012


Hi Madhu,

On Sun, Jan 1, 2012 at 11:34 PM, Madhusudan C.S <madhusudancs at gmail.com> wrote:
> Hi,
>    It is common to use Django and other such frameworks
> on the console mode with all the Django/framework related
> API libraries already imported to the namespace. This
> simplifies a lot of stuff when experimenting. Django provides
> support for IPython. I work on Melange project (which is a tool
> that runs the Google Summer of Code and Google Code-in
> programs), for which I have extended this to work with the
> Django, Google Appengine and Melange's API itself. It is
> a simple and usual embed TerminalInteractiveShell instantiation
> with all these APIs imported. Nothing special or fancy about
> it. The snippet is like this:
>
> from IPython.frontend.terminal.embed import TerminalInteractiveShell
> shell = TerminalInteractiveShell(user_ns=context)
> shell.mainloop()
>
> I would like to extend the same for an IPython qtconsole or the
> notebook. How do I import the namespace to these console's?

The following code works, but the api for this is certainly
sub-optimal right now:

###
from IPython.zmq.ipkernel import IPKernelApp

namespace = dict(z=1010)

kapp = IPKernelApp.instance()
kapp.initialize()

# Update the ns we want with special variables auto-created by the kernel
namespace.update(kapp.shell.user_ns)
# Now set the kernel's ns to be ours
kapp.shell.user_ns = namespace

kapp.start()

###

Furthermore, there's the problem that the above needs to be killed
manually b/c it disables SIGINT.

Min can correct me if there's a cleaner way to achieve this, and if
there isn't we should improve the user-facing api to acccept a
namespace at initialization time, like the old classes did (and also
to make shutdown easier when a kernel is directly started).

Cheers,

f



More information about the IPython-dev mailing list