[IPython-dev] How to preconfigure notebook environment in v1.1?
Kyle Rawlins
rawlins at gmail.com
Thu Jan 23 15:49:40 EST 2014
Hi all,
Just wanted to pick up on this thread from back in the fall and see if there were any further answers -- the question asked then was how to do preconfiguration of notebooks as part of a specific "branded" application, and there wasn't really a conclusive answer; I have a very similar scenario. Basically I have a set of modules (and other things) that I want to be pre-imported into the namespace of any notebook that my users load.
I did find a different (and perhaps less messy?) way of doing this than what is hinted at below in Adam's email, by using "exec_lines" as an argv option to initialize() in a wrapper script that starts the notebook server. It needed to be in argv, because as far as I understand it, configuration options set directly on the NotebookApp _won't_ get passed along to kernels, and argv will -- this is probably obvious to experts but it took me quite a while to understand this part so I thought it worth mentioning explicitly. ("exec_files" should presumably work too but I couldn't get it to for some reason.) I'm still not sure that this is "right" in the long run, though; for example it seems like this might override per-user configuration. I'm guessing a solution that involves writing a subclassed kernel as in Adam's solution could be more general in this respect, but I'm pretty hesitant to mess with the internals to that degree (yet). Any further suggestions for better ways to do this sort of application-level preconfiguration would be helpful.
As a reminder (this came up in the old discussion), changing the configuration files in the profile directory isn't the right way to go, as my project involves a "branded" version of the notebook tailored to a specific application and should ideally be able to inject its modules / functions / etc independent of the user's configuration.
For reference, here's a miniaturized version of what I came up with that demonstrates this procedure by injecting "test_var" into the namespace for any notebook loaded from the server it starts. One can obviously be much more elaborate, and call other files, etc. Note that this way of starting a notebook server programmatically I got from the branded notebook recipe (written by Brian Granger) in the cookbook, which is a bit old; it still seems to work fine but I really have no idea if this is still "right" either. But that is perhaps a separate question.
"""
import IPython
from IPython.html import notebookapp
def launch_specialized_notebook(args):
app = notebookapp.NotebookApp()
injection_opt = '--IPKernelApp.exec_lines=["get_ipython().user_ns[\\"test_var\\"] = 23"]'
app.initialize([injection_opt] + args[1:])
app.start()
launch_specialized_notebook([])
"""
Thanks,
Kyle
On Nov 15, 2013, at 9:02 AM, ipython-dev-request at scipy.org wrote:
> Date: Thu, 14 Nov 2013 12:32:09 -0800
> From: Adam Sadovsky <asadovsky at gmail.com>
> Subject: Re: [IPython-dev] How to preconfigure notebook environment in
> v1.1?
> To: IPython developers list <ipython-dev at scipy.org>
> Message-ID:
> <CADh3F_XNB1a1S6=cT-JY3_0wsOMhDXO=tadz6r5AB3q+o=04AQ at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> For the record, I ended up achieving this by modifying KernelManager to
> invoke the original script in a subprocess (rather than just calling zmq
> main), and then configuring that binary, when called in kernel mode (i.e.
> with "kernel" argument), to call IPython.start_kernel with the desired
> user_ns passed in.
>
> Is there any documentation for developers who want to better understand the
> internal IPython APIs, specifically around kernels, applications, etc.? I
> felt myself digging around somewhat blindly in order to figure this out,
> and I'm sure there are better ways to do it (that don't require modifying
> IPython internals).
>
>
> --Adam
>
>
> On Wed, Oct 30, 2013 at 10:32 PM, Adam Sadovsky <asadovsky at gmail.com> wrote:
>
>> Hi Paul,
>>
>> Thanks for the reply! The reason I'd like to avoid startup files is that
>> I'm trying to provide a library for others to use. As part of this library,
>> I want it to be possible for people to start a notebook server where any
>> new notebook has a certain set of things pre-imported for convenience. I
>> suppose I could provide a startup file for users to place in their own
>> startup dirs, but I'd like to avoid making users do any extra work. Ideally
>> I'd like an option similar to what's offered in the interactive shell API,
>> which allows users to pass in a user_ns object. Does that make sense?
>>
>> I should say, I'm relatively new to IPython/notebook and not very familiar
>> with its architecture, so I certainly may be missing something obvious. :)
>>
>>
>> --Adam
>>
>>
>> On Wed, Oct 30, 2013 at 9:11 PM, Paul Ivanov <pi at berkeley.edu> wrote:
>>
>>> Adam Sadovsky, on 2013-10-30 20:28, wrote:
>>>> I recently upgraded from IPython 0.13 to 1.1.
>>>> In 0.13, I was able to execute code in each notebook at startup, but the
>>>> kernel code has changed and I can't figure out how to do it anymore.
>>> Could
>>>> someone point me in the right direction?
>>>
>>> Hi Adam,
>>>
>>> why not just use a file in the startup directory?
>>>
>>> $ echo `ipython locate profile`/startup/
>>> /home/pi/.ipython/profile_default/startup/
>>> $ cat `ipython locate profile`/startup/README
>>> This is the IPython startup directory
>>>
>>> .py and .ipy files in this directory will be run *prior* to any
>>> code or files specified
>>> via the exec_lines or exec_files configurables whenever you load
>>> this profile.
>>>
>>> Files will be run in lexicographical order, so you can control
>>> the execution order of files
>>> with a prefix, e.g.::
>>>
>>> 00-first.py
>>> 50-middle.py
>>> 99-last.ipy
>>>
>>> best,
>>> --
>>> _
>>> / \
>>> A* \^ -
>>> ,./ _.`\\ / \
>>> / ,--.S \/ \
>>> / `"~,_ \ \
>>> __o ?
>>> _ \<,_ /:\
>>> --(_)/-(_)----.../ | \
>>> --------------.......J
>>> Paul Ivanov
>>> http://pirsquared.org
>>> _______________________________________________
>>> IPython-dev mailing list
>>> IPython-dev at scipy.org
>>> http://mail.scipy.org/mailman/listinfo/ipython-dev
More information about the IPython-dev
mailing list