Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to run Julia code in an IPython notebook (or Python code in an IJulia notebook)

People also ask

Can I run Julia on Jupyter Notebook?

After installing the necessary packages for your purposes, you can experience the high-performance Julia language in Jupyter notebook.

Can I run Python code in Jupyter Notebook?

First and foremost, the Jupyter Notebook is an interactive environment for writing and running code. The notebook is capable of running code in a wide range of languages. However, each notebook is associated with a single kernel. This notebook is associated with the IPython kernel, therefore runs Python code.


Run Julia inside an IPython notebook


Hack

In order to run Julia snippets (or other language) inside an IPython notebook, I just append the string 'julia' to the default list in the _script_magics_default method from the ScriptMagics class in:

  • /usr/lib/python3.4/site-packages/IPython/core/magics/script.py or
  • /usr/lib/python2.7/site-packages/IPython/core/magics/script.py.

Example:

# like this:
defaults = [
    'sh',
    'bash',
    'perl',
    'ruby',
    'python',
    'python2',
    'python3',
    'pypy',
    'julia', # add your own magic
]
  • Example notebook (using Python3)

IPython *Julia magic*

Julia Magic (Bi-directional)

To use %load_ext julia.magic, you would need to run the setup.py here:

Update (09/04/2014): the setup.py file has been moved to pyjulia.jl:

  • https://github.com/JuliaLang/pyjulia

Which you get when Pkg.add("IJulia") clones the repo in your filesystem:

cd ~/.julia/v0.3/IJulia/python/
sudo python2 setup.py install

Currently this only works for me in Python2. Python3 complains about:

ImportError: No module named 'core'

when I try to load the extention, but installs without complains.

After installing it you can also do this from inside Python2:

from julia import Julia
j = Julia()
arr = j.run('[1:10]')
type(arr) # numpy.ndarray
  • http://blog.leahhanson.us/julia-calling-python-calling-julia.html

Runing a script from your system shell

Use the shell mode syntax in a notebook cell:

!julia my_script.jl



Run Python inside IJulia notebook


Using PyCall

It's not really running python code in the context you want, but you can also use Python libraries from within Julia:

using PyCall
@pyimport math
println(math.pi)
  • https://github.com/stevengj/PyCall.jl

Runing a script from your system shell

Use the shell mode syntax in a notebook cell:

;python my_script.py
  • http://julia.readthedocs.org/en/latest/manual/interacting-with-julia/?highlight=shell#shell-mode

Another option is to use Beaker. They have a tutorial notebook available that mixes R and Python; using Julia is just as easy.


If you want to run a whole notebook of only julia (or where you call other languages only from julia), then there is a cleaner solution. First, launch julia and do

Pkg.add("IJulia")

to get the IJulia package. Then you can

ipython notebook --profile julia

and your notebook will have julia as the native (default) language.

h/t to David Sanders and his excellent julia tutorials that are written in IPython notebooks; video here.


To complete this good answer, Without any hack, and without modifying any system file, a simple solution is with the %%script magic:

In [1]: %%script julia
   ...: println("Hi from a Julia sub-process")
   ...: a = [1:10]

Be cautious that the cell was run in a sub-process, so whatever was done in it is not accessible by the rest of the IPython session:

In [2]: print("Hi from the main IPython process")
   ...: print(a)  # <-- not available from the Julia code, will fail !
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-c5a4f3535135> in <module>()
----> 1 print(a)

NameError: name 'a' is not defined