After installing the necessary packages for your purposes, you can experience the high-performance Julia language 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.
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
.# like this:
defaults = [
'sh',
'bash',
'perl',
'ruby',
'python',
'python2',
'python3',
'pypy',
'julia', # add your own magic
]
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:
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
Use the shell mode syntax in a notebook cell:
!julia my_script.jl
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)
Use the shell mode syntax in a notebook cell:
;python my_script.py
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With