I have been introduced recently to the pleasures of IPython Notebooks. The main reason I enjoy coding on them is how easy and immediate is going from the code to the results. However, I found having to launch a terminal, cd to the directory I need, and type ‘ipython notebook’ every time quite annoying. After a bit of research, I came up with a way to directly double-click on a notebook to launch it in your default browser. This works in Ubuntu (14.04; I’m not sure about other Linux distributions).
Please make sure you understand what you’re doing. I will take no responsibility for what happens when sudoing on your machine.
Create a new MIME type.
Type the following in an empty text document:
<?xml version="1.0" encoding="UTF-8"?> <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'> <mime-type type="application/x-ipynb+json"> <comment>IPython Notebook</comment> <glob pattern="*.ipynb"/> </mime-type> </mime-info>
Now save this in a file with the “.xml” extension, for example “ipynb.xml”. Then, in a terminal, cd to the directory where the file is and type:
sudo cp ipynb.xml /usr/share/mime/packages
sudo update-mime-database /usr/share/mime
You can also set an icon for this MIME type, so that all of your .ipynb files will have a nice aspect that identifies them. Here are the instructions, and the .svg icon for notebooks can be found in the official IPython distribution.
Write a script to launch notebooks
Now you need an application that takes care of opening the notebook files. What I did is the following:
#!/bin/bash netstat -tln |grep "8902" # if not found - equals to 1, start it if [ $? -eq 1 ] then ipython notebook / --no-browser --port=8902 & sleep .5 fi xdg-open http://localhost:8902/notebooks$1
When this script is called, it checks whether anything is running on port 8902 (one of the ones ipython normally uses, but not among the first, so that it won’t conflict with other running notebook servers). If there is something, it just opens the URL of the notebook we are trying to launch on the notebook server. If there is no server on port 8902, it starts it.
A known problem is that this won’t work if there are spaces (and maybe other special characters) in the path of the file. Also, the server is never shut down, but I don’t think this is a problem (it doesn’t open more than one at a time anyway).
Before using this, saved in the file “ipynb” (no extensions!), we need to make it accessible by allowing everybody to run it, and placing it in /usr/local/bin:
chmod +x ipynb
sudo cp ipynb /usr/local/bin/
Setting the default application
We want our “ipynb” to open all files ending by “.ipynb” by default. This is best done using ubuntu-tweak. In the admin panel, select “File types management” (or something like that). Untick “show only types with associated apps” and look for “IPython Notebook”. This is the MIME type we created. When adding a default application, open “personalized command” and type in “/usr/local/bin/ipynb”.
Done. Hope this is useful. Let me know if it works.