Loading iPython Notebook

Posted on October 5, 2012
Filed Under Python | 5 Comments

NotebookIn the spring I’d mentioned the recent cool changes to iPython.1 If you use the notebook feature then in a browser you get a very Mathematica like interactive Python environment. It’s not perfect but it is pretty useful. Where it becomes even more useful is when you use it with NumPy and SciPy to do data analysis. You end up with something like the old IDL system I used to use on old Sun workstations back in the day.

I find that using Chrome rather than Safari for it works better. For one it means that if something goes wrong and Safari crashes or gets restarted my iPython session is unaffected. Normally I just use Chrome for going to USPS (since printing your mailing labels doesn’t work with Safari) so it’s usually ready to be used. 

The one hassle is starting the notebook. You basically run the iPython command and specify a notebook which starts up a web server on port 8888. However what happens if you close your browser window? Well the server keeps running. Ideally what you’d like is a command that starts up the iPython notebook server or attaches to it if it’s already running. That’s what this script does. 

It’s a slight variation on the script from my original post. I use appscript to open up a Chrome window and set its URL.2 To call the shell commands I use plumbum. I find plumbum is just the easiest way to call shell commands. If you haven’t tried it I suggest checking it out. It’s vastly better than the alternatives I’ve seen including the standard process libraries.

The check to see if ipython notebook is running is fairly trivial. I just list the processes and grep for the ipython notebook. About the only trick is to check the result with the full path (since otherwise you pickup your grep process). 

#!/usr/bin/env python
 
import sys
from appscript import *
import time
from plumbum import BG, local
from plumbum.cmd import grep, ps
 
def check_notebook():
    chain = ps["aux"] | grep["ipython notebook"]
    result = chain()
 
    if "/usr/local/bin/ipython notebook" in result:
        return True
 
    return False
 
def start_notebook():
    #command = "/usr/local/bin/ipython notebook --pylab inline --no-browser --NotebookManager.notebook_dir='/Users/clarkgoble/projects'&"
 
    ipython = local["/usr/local/bin/ipython"]
    ipython["notebook", "--pylab", "--no-browser", "--NotebookManager.notebook_dir='/Users/clarkgoble/projects'"] & BG
 
    # takes a little bit of time to startup - want to pause before attaching
    time.sleep(2)
 
def attach_notebook():
    C = app(u'/Applications/Google Chrome.app')
    C.make(new=k.window)
 
    C.windows[1].active_tab.URL.set("http://127.0.0.1:8888")
    C.activate()
 
 
def main():
    if not check_notebook():
        start_notebook()
 
    attach_notebook()
 
if __name__=='__main__':
    sys.exit(main())

You can run this via Automator or assign it to a macro program like Quickeys or Keyboard Maestro.

For quitting the server I use the following script.

#!/usr/bin/env python
 
import sys
from plumbum.cmd import grep, ps, awk, kill
from plumbum import BG
 
def quit_notebook():
    # get the process number excluding the process of this command
    chain = ps["aux"] | grep["-v","grep"] | grep["ipython notebook"] | awk["{print $2}"]
    result = chain().split()
 
    for o in result:
        (kill[o] ) & BG
 
 
 
def main():
    quit_notebook()
 
if __name__=='__main__':
    sys.exit(main())
  1. To use the notebook feature you do have to install a few extra libraries. I’m not sure all of them but you’ll definitely want tornado and pyzmq along with obviously ipython. Just use easy_install to put these on your system.
  2. Yes I know appscript is deprecated but it’s still the best way to script in my opinion. I’ll keep with it until Apple’s strategy here becomes more apparent.

Comments

5 Responses to “Loading iPython Notebook”
1 Wes Campaigne on October 8th, 2012 1:28 pm

In the case of just opening a web page, appscript is overkill. Calling

open -a ‘Google Chrome’ http://127.0.0.1:8888

seems like a more direct (and less likely to break) way to load it.

The reason I use appscript is in practice I also move the window, resize it and do a few other things. I just didn’t put those complexities in the post. I also often have a worksheet I use primarily and I can set it to open up via appscript. Those will be in a future post.

Generally with scripts I try and write them for maximum flexibility so I can modify them as the need arises. With a narrower constraint there are simpler solutions but those solutions aren’t as flexible for future expansion. I tend to modify all my scripts regularly for particular periods of workflow.

3 Wes Campaigne on October 8th, 2012 4:19 pm

Ahh, that makes sense. Appscript certainly does bring with it a lot of flexibility and power. I look forward to those future posts.

Glad you asked though. I should have explained why I was doing things in a more complex way. I’ve discussed it in the past but probably I have a lot of new readers not familiar with my overall scripting strategy. I’m sure everyone scripts in a different way. I just find myself modifying scripts on the fly for specific needs. It’s planning for the future to make them flexible for such events.

[...] notebook so it really only is helpful if you have iPython and the notebook stuff installed. (See this post of mine and this one) If you do have it installed this lets you easily make xkcd like [...]

Leave a Reply