Monday, April 13, 2015

How to create a new mime-type for IPython notebook files in Ubuntu

Installing IPython adds a very useful format for scientific calculations called the IPython notebook. These are text files with JSON (JavaScript Object Notation) code with the .ipynb file ending. It makes sense to open these with the IPython notebook, but under Ubuntu, they are opened by the default text editor (Gedit).

I would like to be able to open them by double clicking in the file manager and also having a nice icon for the files.

To achieve this I needed to create a new mime-type for .ipynb files

Creating a new mime type 

There are many ways to create mime-types in Ubuntu, but this is what I did. I used this blog post by termueske as a guide.

I created a text file called ipynb.xml on my desktop with the following content:

<?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>

I then opened a terminal in desktop and typed the following command:

bjorn@bjorn-UL30A:~/Desktop$ xdg-mime install ipynb.xml --novendor

If there is no response, everything is ok. Then I updated the mime database:

bjorn@bjorn-UL30A:~/Desktop$ update-mime-database /home/bjorn/.local/share/mime/

My username is bjorn and my home folder is /home/bjorn/ so the path above has to be changed for other user names.

That should be it. Check properties of a .ipynb file to see the new mime type.

Obtaining a nice icon

I downloaded "ipython-3.1.0.zip" from https://pypi.python.org/pypi/ipython/3.1.0

inside the zip file (/ipython-3.1.0/docs/resources/) there is a nice svg icon called "ipynb_icon_512x512.svg"


Adding an icon the the new mime type

I renamed the icon file above to:

application-x-ipynb+json.svg

This is the mime-type in the ipynb.xml file above, but with the slash substituted by a minus sign (dash).

Now put this file where you file manager can find it. I tried with:

/home/bjorn/.local/share/icons/hicolor/scalable/mimetypes



I had to create the sub folders /scalable/mimetypes/ 

Then I simply turned off my file manager in the terminal:

bjorn@bjorn-UL30A:~/Desktop$ nemo -qq

I use Nemo, but if you use Nautilus, type instead 
nautilus -qq

Then restart Nemo using the "Files" icon in the unity launcher.

This worked well for me, this is a notebook on my desktop:



Right clicking on this file gives:


Why?

Why did I choose /home/bjorn/.local/share/icons/hicolor/scalable/mimetypes
as the location for the icon?

Trial and error!

They say here that the "hicolor" icon theme is a fallback theme for icons. I looked in  /home/bjorn/.local/share/icons/ and I had only a folder there called hicolor. I wanted to put the icon in my home folder (see below).

Icon themes have an index.theme file that determines where they look for the actual icon files. This file is located in /usr/share/icons/hicolor 



There is an entry in this file called "Directory=" which contains all icon lookup directories. This is a very long comma separated list on my computer.

As my icon is a scalable (svg) I looked for and found the entry "scalable/mimetypes", so it would make sense to use this location. 

I didnt want to put the icon in /usr/share/icons/hicolor/scalable/mimetypes since I would loose the icon when I upgrade or reinstall Ubuntu.

Apparently /home/bjorn/.local/share/icons/hicolor/scalable/mimetypes works just as well.

Hope this can help !

Other useful resources: