I am trying to convert my IPython notebook to an html file so that I can put it on my wordpress blog. I have used the following command to generate an html file for the notebook
ipython nbconvert notebook.ipynb
then I copied the html code and pasted it into the ‘text’ tab. The resulting blog post sort of looks like the ipython notebook, however the problem is that the markdown equations do not show up and the headings look strange. Has anyone managed to display an IPython notebook in a wordpress blog post successfully? If so, how?
In this November 2013 blog article http://www.josephhardinee.com/blog/?p=46, the author goes quickly through the conversion process.
He mentions the need to install the Simple Mathjax plugin to make equation display work.
Now, what I have tested to work on my self-hosted WordPress blog:
- Copy paste the html output of nbconvert (only what is inside the
<body>tag) in the “Text” tab.
- disable the Worpress html code parsing because otherwise images do not display (as explained in the blog post). See below for two possible methods.
- Activate Mathjax: either with a plugin or manually in the post code
Mathjax With plugin
I have not tested the
Simple Mathjax plugin, but I have
LaTeX for WordPress which works for me.
Manual Mathjax activation
Copy paste from nbconvert output the two
<script> tags that activate Mathjax:
1) Load the library:
2) Launch it:
Disabling code HTML parsing
The blog post suggests to activating the PS Disable Auto Formatting plugin to make the notebook images work. I have tested it successfully but it has one drawback: it messes up with the rendering of all the other posts… that’s quite an issue!
I have tested instead the Raw HTML plugin which enable a per-post tuning. I’ve made images work by selecting the
Disable automatic paragraphs option (the plugin creates a new box in the post editor).
while the notebooks should display fine with this method, there is still work to get the syntax highlighting of code cells to display properly. However the Python source code is already parsed by CodeMirror, so it should just be about loading the appropriate CSS code.
One approach is to embed the notebook using an iframe. This original idea came from the blog post http://onsnetwork.org/eimd/2014/08/08/how-to-enter-ipython-notebooks-to-wordpress/, but I’ve made several improvements. The straightforward way to do this is to:
- Install the
Raw HTMLplugin to WordPress. This only needs to be done once.
- Convert the notebook to HTML
ipython nbconvert YOUR_NOTEBOOK.ipynb
- Upload the resulting HTML to WordPress as a media file. Take note of the URL where it is uploaded to.
Insert the iframe between
rawtags in your post. For example:
<iframe id="ipython_notebook_frame" style="height: 500px; width: 100%; padding: 0; border: none;" src="URL_OF_NOTEBOOK"> </iframe>
For a little more detail on this, as well as an example you can take a look at this post: http://www.bitsofbits.com/2015/01/19/ipython-notebooks-in-wordpress