In [8]:
%pylab inline
In [1]:
from IPython.display import display, Image, HTML
from talktools import website, nbviewer
It is 2013. In case you haven't noticed, there is now more than one programming language. We love Python, but insisting that everyone write everything in Python all the time is naive and limits the impact of this community. The reality is that most large, complex projects involve multiple languages. In fact, it is commmon for projects to use at least three classes of languages:
Despite its name and historical development, IPython is now a multi-lingual project that supports a wide range of programming languages throughout its architecture.
In the IPython architecture, the kernel is a separate process that runs the user's code and returns the output back to the frontend (Notebook, Terminal, etc.). Kernels talk to Frontends using a well documented message protocol (JSON over ZeroMQ and WebSockets). The default IPython kernel that ships with IPython, knows how to run Python code. However, there are now kernels in other languages:
There will come a day when all users of the IPython Notebook will have the option to choose what type of kernel to use for each Notebook.
The standard IPython kernel also allows running code in other languages using the %%magic
syntax.
Here is a bit of Ruby:
In [2]:
%%ruby
puts "Hello from Ruby #{RUBY_VERSION}"
And some Bash:
In [3]:
%%bash
echo "hello from $BASH"
The R magic has to be loaded from an extension:
In [4]:
%load_ext rmagic
Create two arrays in Python:
In [5]:
import numpy as np
X = np.array([0,1,2,3,4])
Y = np.array([3,5,4,6,7])
Use those arrays to make a scatter plot using Matplotlib:
In [9]:
scatter(X,Y)
Out[9]:
Pass the arrays to R and fit a linear model:
In [10]:
%%R -i X,Y -o XYcoef
XYlm = lm(Y~X)
XYcoef = coef(XYlm)
print(summary(XYlm))
par(mfrow=c(2,2))
plot(XYlm)
Here is a longer set of examples fron Fernando Perez that demonstrate the %%julia
magic.
In [11]:
nbviewer('raw.github.com/JuliaLang/IJulia.jl/master/python/doc/JuliaMagic.ipynb')
Out[11]:
Notebook documents are JSON files that store code, output, text, images, HTML, etc. The Notebook as a whole and individual cells have metadata associated with them. This metadata can be used to declare which programming language the code cells are in. All other parts of the Notebook document format (output, images, equations, etc.) are language agnostic. Thus, Notebook documents are multi-lingual.
Both nbviewer and nbconvert work (mostly) with Notebooks written in different languages.
In [12]:
nbviewer('jdj.mit.edu/~stevenj/IJulia%2520Preview.ipynb')
Out[12]:
In [13]:
%load_ext load_style
In [14]:
%load_style talk.css