What the Carp?!http://jmcarp.github.io/2013-07-07T08:00:00-07:00Hello, world! Getting started with Pelican and IPython Notebooks2013-07-07T08:00:00-07:00Joshua Carptag:jmcarp.github.io,2013-07-07:blog/2013/07/07/hello-world/<p>I've been hearing great things about blogging with IPython notebooks from folks like Jake Vanderplas and Fernando Perez, so I thought I'd give it a try myself. Jake has put together some great tools for dropping notebooks into blog posts built using <a href="http://blog.getpelican.com/">Pelican</a>, and documented them in a <a href="http://jakevdp.github.io/blog/2013/05/07/migrating-from-octopress-to-pelican/">blog post</a>. So I figured getting a notebook into a blogpost would take a few minutes--half an hour, max.</p>
<p>Several hours later, I had a working example. Needless to say, getting this working was more involved than I had expected. I gather this is because IPython, the notebook converter (nbconvert), and the Pelican plugins are all somewhat in flux--meaning that tools that worked together a few weeks ago might be incompatible today. Once IPython hits 1.0 <a href="https://github.com/ipython/ipython/wiki/Roadmap:-IPython">in a few weeks</a>, hopefully these issues will be resolved.</p>
<p>Until then, here's a quick guide to setting up a Pelican blog with IPython notebooks, using the software available at this moment. To make things really simple, this guide will use Jake Vanderplas's blog, the source for which is available on <a href="https://github.com/jakevdp/PythonicPerambulations">his GitHub</a>.</p>
<p>I assume you'll use a virtual environment for your blog. I use Doug Hellman's <a href="http://www.doughellmann.com/projects/virtualenvwrapper/">virtual environment wrapper library</a>, but the standard virtual environment package or pythonbrew are fine too. First things first: create a new environment and install the required packages.</p>
<p><code>mkvirtualenvironment blog
sudo pip install tornado pyzmq ipython pelican markdown</code></p>
<p>Next, clone the Pythonic Perambulations blog:</p>
<p><code>git clone https://github.com/jakevdp/PythonicPerambulations.git</code></p>
<p>And the Octopress theme for Pelican, written by Maurizio Sambati:</p>
<p><code>git clone https://github.com/duilio/pelican-octopress-theme.git</code></p>
<p>We'll also need Jake Vanderplas's liquid tags plugin for Pelican. The plugin is in the liquid_tags branch of Jake's fork of the pelican-plugins repo, so we need to check it out after cloning the repo.</p>
<p><code>git clone https://github.com/jakevdp/pelican-plugins.git
(cd pelican-plugins && git checkout liquid_tags)</code></p>
<p>We also need nbconvert to convert IPython notebooks to Markdown. To get this set up, clone the nbconvert repo, then add the nbconvert/utils and nbconvert/nbconvert1 directories to your pythonpath:</p>
<p><code>git clone https://github.com/ipython/nbconvert.git
PYTHONPATH="/path/to/nbconvert/nbconvert/utils:$PYTHONPATH"
PYTHONPATH="/path/to/nbconvert/nbconvert1:$PYTHONPATH"
export PYTHONPATH</code></p>
<p>We're almost done. Rendering IPython Notebooks requires a file called style.min.css, which the liquid tags plugin expects to live in .../site-packages/IPython/frontend/html/notebook/static/css. That file actually doesn't exist in the virtual environment at this point, but we can get a copy from the IPython repo on GitHub. To do this, we can clone the repo and copy the css file to the location where the plugin expects it to be:</p>
<p><code>git clone https://github.com/ipython/ipython.git
cp ipython/IPython/html/static/style/style.min.css /path/to/venv/lib/python2.7/site-packages/IPython/frontend/html/notebook/static/css</code></p>
<p>Next, we'll create a directory for the Pelican output:</p>
<p><code>mkdir PythonicPerambulations/output</code></p>
<p>And update the paths in the Pelican configuration file, pelicanconf.py. Since we cloned jakevdp's blog, we'll also need to change the AUTHOR, SITENAME, and a bunch of other parameters in this file, but that can wait.</p>
<p><code>THEME = '/path/to/octopress/pelican/theme'
PLUGIN_PATH = '/path/to/forked/pelican/plugins'</code></p>
<p>Now everything should work. Let's see if it does:</p>
<p><code>(cd output && python -m SimpleHTTPServer)
Browse to localhost:8000</code></p>
<p>And to prove that this works--at least using the current versions of the Pelican plugin, IPython, nbconvert, etc., as of this exact moment--here's a notebook:</p>
<div class="ipynb">
<div class="cell border-box-sizing code_cell vbox">
<div class="input hbox">
<div class="prompt input_prompt">In [1]:</div>
<div class="input_area box-flex1">
<div class="highlight-ipynb"><pre class="ipynb"><span class="kn">import</span> <span class="nn">this</span>
</pre></div>
</div>
</div>
<div class="vbox output_wrapper">
<div class="output vbox">
<div class="hbox output_area">
<div class="prompt output_prompt"></div>
<div class="output_subarea output_stream output_stdout">
<pre class="ipynb">The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
</pre>
</div>
</div>
</div>
</div>
</div>
</div>