Attribute: Multi-lingual


In [8]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [1]:
from IPython.display import display, Image, HTML
from talktools import website, nbviewer

Overview

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:

  • Low-level (C, C++, Java, Fortran)
  • High-level (Python, Ruby, Perl, Julia)
  • Web-based (JavaScript, CoffeeScript, HTML, CSS)

Despite its name and historical development, IPython is now a multi-lingual project that supports a wide range of programming languages throughout its architecture.

Kernel 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.

Magic commands

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}"


Hello from Ruby 2.0.0

And some Bash:


In [3]:
%%bash
echo "hello from $BASH"


hello from /bin/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]:
<matplotlib.collections.PathCollection at 0x10d6b43d0>

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)


Call:
lm(formula = Y ~ X)

Residuals:
   1    2    3    4    5 
-0.2  0.9 -1.0  0.1  0.2 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)  
(Intercept)   3.2000     0.6164   5.191   0.0139 *
X             0.9000     0.2517   3.576   0.0374 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.7958 on 3 degrees of freedom
Multiple R-squared:   0.81,	Adjusted R-squared:  0.7467 
F-statistic: 12.79 on 1 and 3 DF,  p-value: 0.03739

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

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.

nbviewer/nbconvert

Both nbviewer and nbconvert work (mostly) with Notebooks written in different languages.


In [12]:
nbviewer('jdj.mit.edu/~stevenj/IJulia%2520Preview.ipynb')


Out[12]:

Styling


In [13]:
%load_ext load_style

In [14]:
%load_style talk.css