Using music21 with the Jupyter (iPython) Notebook

Setting up the Jupyter environment

To get music21 to work it’s best to have a good pip installation first. Then you should be able to simply install jupyter (and IPython if you don’t have it):

$ sudo pip install jupyter $ sudo pip install ipython

Using the web-based Jupyter Notebook

Start jupyter in a directory you want to save files by typing.

`$ jupyter notebook`

Now in the webbrowser you can type commands such as the ones below:

from music21 import *
c = chord.Chord("C4 E4 G4")

All other normal music21 commands will work as they should

Displaying graphics inline

By default, you cannot just call .show() because you’ll open up your MusicXML reader locally...
 <music21.ipython21.objects.IPythonPNGObject at 0x1054dcf60>

Nor does just generating a lilypond PNG or PDF work as you’d like – this will display it on the screen but not in your browser:'lily.pdf')

Instead do this:

%load_ext music21.ipython21

Now this will work:

If you don’t want to do that, then instead do this...

from IPython.core.display import Image

SVG is much faster, but it doesn’t work, sadly...


ValueError                                Traceback (most recent call last)

<ipython-input-9-a78ad09b705c> in <module>()
----> 1 Image(filename=c.write('lily.svg'))

/Users/cuthbert/anaconda/lib/python3.4/site-packages/IPython/core/ in __init__(self, data, url, filename, format, embed, width, height, retina, unconfined, metadata)
    732         if self.embed and self.format not in self._ACCEPTABLE_EMBEDDINGS:
--> 733             raise ValueError("Cannot embed the '%s' image format" % (self.format))
    734         self.width = width
    735         self.height = height

ValueError: Cannot embed the 'svg' image format

Whole pieces will show properly also now that the extension module is loaded

b = corpus.parse('bach/bwv66.6')

And that is basically it! We use sphinx for interwiki links, which, sadly, won’t show up on the notebook, but should render properly.

But I did make a sphinx-markdown Notebook extension. When it’s ready, I’ll put it up. Make a directory “sphinx-markdown” in a “usability” directory in “nbextensions” (mine is at “~/Library/Jupyter/nbextensions”). And put this file there as “main.js”: []

Check to see if it exists:

import notebook
notebook.nbextensions.check_nbextension('usability/sphinx-markdown', user=True)

Load it with:

E = notebook.nbextensions.EnableNBExtensionApp()

Save, and reload. Then test it with a markdown cell like:

For more information, see, installing IPython/Jupyter for Music21.

And you should get this output:

For more information, see, installing IPython/Jupyter for Music21. :-)