UPDATE 8/26/2014:Thanks to Daan Debie, you no longer have to do any of the below if you're using the pelican-bootstrap3 theme. Simply include the liquid-tags.notebook
plugin in your pelican plugin list and you're good to go!
In this post, I'm (selfishly) testing the integration between pelican and IPython
notebooks.
I've been meaning to do this for awhile, since I have quite a few R
and Python
tutorials on a variety of subjects written up as IPython
notebooks, my distribution platform of choice.
I was having trouble getting the pelican-bootstrap3
theme to play nicely with the liquid-tags
pelican plugin. I have to give a hat-tip to Kyle Cranmer who posted his solution (involves chopping up the _nb_header.html
and creating a _nb_header_minimal.html
, which is referred to in your pelicanconf.py
as EXTRA_HEADER
instead).
It's crazy to see someone else with a similar workflow! I only discovered his work since I was having the worst time ever trying to get pelican to play nicely with notebooks.
His hack does the job, and now the notebooks are more nicely styled than without the additional css, but it makes me feel a little leery since it has the feel of something that will break with an update. Oh well; that's what you get when you use open-source projects.
Below the line is an example of an ipython notebook embedded in a pelican article. It's as simple as creating a notebook and referring to {% notebook demonstration.ipynb %} in the article post.
Simple examples of IPython notebook features
I wanted to provide some quick examples of how easily IPython notebook allows people to write code to do analysis and share it on the web. This was a simple demonstration notebook that incorporates examples from around the web (with appropriate links leading back to the origin of the example code).
But this is hardly scratching the surface of what these tools offer. Primarily, this notebook was created to demonstrate how easy a static site generator like Pelican can incorporate notebooks [relatively] seamlessly in a page.
total = 2 + 2
total
Some $\LaTeX$ using IPython magic (more examples can be found here)
%%latex
\begin{align}
a^2 + b^2 = c^2
\end{align}
A matplotlib
demo (taken directly from the source)
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 3*np.pi, 500)
plt.plot(x, np.sin(x**2))
plt.title('A simple chirp');
A ggplot
example courtesy of $\hat{y}$hat
from ggplot import *
ggplot(mtcars, aes('mpg', 'qsec')) + \
geom_point(colour='steelblue') + \
scale_x_continuous(breaks=[10,20,30], \
labels=["horrible", "ok", "awesome"])
Pandas
import pandas
import pandas.io.data
from pandas import Series, DataFrame
pandas.set_option('display.notebook_repr_html', True)# Allows for pretty html output of data frames
labels = ['a', 'b', 'c', 'd', 'e']
Series([1, 2, 3, 4, 5], index=labels)
from IPython.display import display
from IPython.display import HTML
DataFrame.from_items([('A', [1, 2, 3]), ('B', [4, 5, 6])])
Statsmodels (simple OLS example taken from the source).
import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt
from statsmodels.sandbox.regression.predstd import wls_prediction_std
np.random.seed(9876789)
# Artificial data
nsample = 100
x = np.linspace(0, 10, 100)
X = np.column_stack((x, x**2))
beta = np.array([1, 0.1, 10])
e = np.random.normal(size=nsample)
# Add intercept
X = sm.add_constant(X)
y = np.dot(X, beta) + e
# Inspect
X = sm.add_constant(X)
y = np.dot(X, beta) + e
# Fit and summary
model = sm.OLS(y, X)
results = model.fit()
print results.summary()
# Object types?
import pprint
types = [
type(model),
type(results)
]
pprint.pprint(types)
print "What properties and methods does the statsmodels OLS object have?"
pprint.pprint(dir(model))
print "What properties and methods does the statsmodels OLS fitted model have?"
pprint.pprint(dir(results))
# What modules are loaded into the local namespace?
import sys
print [key for key in locals().keys()
if isinstance(locals()[key], type(sys)) and not key.startswith('__')]
Final note
For reproducibility reasons, I'm using pkg_resources
to reveal the version numbers of imported modules (since all of these were installed from PyPI). This is a pretty heavy handed way to do this; if you're using virtualenv
and pip
(and you are, right?), it's conventionally considered better practice to pip freeze
a requirements.txt
file rather than manually printing out each version using pkg_resources
.
For the sanity of other readers, folks, please reveal version numbers of the software you use somewhere in your documentation! Not enough people do this, and not only is it an incredible waste of time to try and walk through someone's example code and fail constantly because you have different versions insalled, it discourages people from following your work!
import pkg_resources
print 'Pandas = ' + pkg_resources.get_distribution("pandas").version
print 'Matplotlib = ' + pkg_resources.get_distribution("matplotlib").version
print 'Scipy = ' + pkg_resources.get_distribution("scipy").version
print 'Numpy = ' + pkg_resources.get_distribution("numpy").version
print 'ggplot = ' + pkg_resources.get_distribution("ggplot").version
print 'Statsmodels = ' + pkg_resources.get_distribution("statsmodels").version
print 'IPython = ' + pkg_resources.get_distribution("ipython").version
Comments
comments powered by Disqus