After some sleuthing and reflection, I decided the best solution is to let IPython start notebook itself. So the problem is to simulate what happens when calling ipython from the command line:
% ipython notebook <directory>
IPython is started from the command line via a short python script. I import it but make it think it's run as the __main__
module:
import sys, imp
ipython_path = r"/path/to/ipython-script"
sys.argv = [ ipython_path, "notebook" ]
_ipython = imp.load_source('__main__', ipython_path)
To serve a directory different from the current directory, just add it to sys.argv
as an additional argument:
sys.argv = [ ipython_path, "notebook", "path/to/notebooks" ]
Where is the commandline ipython script?
On Windows, IPython is launched with the help of ipython_script.py
in the Scripts
directory (e.g., C:\Python27\Scripts\ipython_script.py
). On OS X, it can be launched by the python script Library/Python/2.7/bin/ipython
. (I installed IPython via easy_install; I suppose there might be other configurations.) You can track down your python installation like this:
import IPython
import inspect
inspect.getsourcefile(IPython)
This is the path to the module, not the launcher script. The script will be in a nearby directory.