Introduction to Jupyter Notebook

Copyright : GNU General Public License v2

Author : Adrien Leger / aleg@ebi.ac.uk / Github

Date : 17/11/2016

Source : https://jupyter.org/

Generic imports and notebook configuration + reveal configuration


In [1]:
from IPython.core.display import display, HTML, Image

display(HTML("<style>.container { width:80% !important; }</style>"))



In [2]:
from traitlets.config.manager import BaseJSONConfigManager
path = "/home/damian/miniconda3/envs/rise_latest/etc/jupyter/nbconfig"
cm = BaseJSONConfigManager(config_dir="/home/aleg/.jupyter/nbconfig/")
cm.update(
    'livereveal', {
        'theme': 'simple',
        'transition': 'zoom',
        'transition-speed' :'fast',
        'start_slideshow_at': 'selected',
        'scroll': True,})


Out[2]:
{u'scroll': True,
 u'start_slideshow_at': 'selected',
 u'theme': 'simple',
 u'transition': 'zoom',
 u'transition-speed': 'fast'}

Jupyter notebook, spiritual son of Galileo

I therefore concluded and decided unhesitatingly, that there are three stars in the heavens moving about Jupiter, as Venus and Mercury round the Sun; which at length was established as clear as daylight by numerous subsequent observations. These observations also established that there are not only three, but four, erratic sidereal bodies performing their revolutions round Jupiter...the revolutions are so swift that an observer may generally get differences of position every hour.

Galileo trans Carlos, Sidereus Nuncius (1610), 1880, p47.

>>>jupyter notebook

  • Open source, data science and scientific computing
  • A web application that run in your favorite browser
  • Create and share documents that contain live code, equations, visualizations and explanatory text
  • Example of application :
    • Data cleaning and transformation
    • Numerical simulation
    • Statistical modeling
    • Machine learning
    • ...

Preview of the Jupyter command panel

Preview of a jupyter notebook

Why using Jupyter ?

Choice of language

  • Support for over 40 programming languages such as Python, Bash, R, Julia ... (Jupyter = Julia + Python + R)
  • Different optional Kernel can run interactive code in a particular programming language and return output to the user.

Readable code and easy to share with others

  • .ipyn = Open document format based on JSON
  • Can be export in many formats (html, latex, pdf...)
  • Contain a complete record of the user's sessions (embed code, narrative text, equations and rich output)
  • Can be put under version control
  • Easy online sharing with Jupyter Notebook Viewer

Rich inline output and interactive widgets

  • Code can produce HTML, images, videos, LaTeX, and JavaScript
  • Interactive widgets to manipulate and visualize data in real time

Big data integration

  • Leverage big data tools, such as Apache Spark, from Python and R
  • Integration of data analysis and data visualization packages (pandas, scikit-learn, matplotlib, ggplot2, dplyr...)
  • Can be ran from a computational node and forwarded in your local browser

The notebook is divided in “cells”

  • Markdown (for descriptions)

  • Code

  • Output (text, graphics, ...)

Markdown cells

Using directly Markdown synthax

See synthax at daringfireball

italic / bold / italic and bold

Blockquote.

Second paragraph in the blockquote

  • Unordered list item
    1. ordered sub-item
    2. second sub-item

Title1

Title2

Title 3

Using HMTL formatted text

Sans serif red

Bold Italic Underligned

title 1

Terminal like

Display mathematics with MathJax

When $a \ne 0$ there are two solutions to $ax^2 + bx + c = 0$

$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}$$

Or mix everything together

This line mixes markdown formatting with HTML formatting and even MathJax mathematics

  • $a \sqrt 2$
  • $b^2-4ac$

Code cells

The default language in a cell depends on the active kernel


  • Python3 kernel

  • Julia kernel

  • R kernel

Run a cell

Select a cell and press CTRL + ENTER


In [ ]:
for i in range (5):
    print (i)

In [ ]:
from random import choice
"".join(choice(["A","T","C","G"]) for i in range(100))

Easy Shell invocation

Independent of the kernel selected some common shell commands can be invoked directly

  • alias
  • cat
  • cd
  • clear
  • cp
  • less
  • ls
  • man
  • mkdir
  • more
  • mv
  • pushd
  • pwd
  • rm
  • rmdir

Examples of direct shell commands


In [ ]:
ls ../

In [ ]:
pwd

In [ ]:
man ls

By adding the magic "!" any bash command can be run in Jupyter


In [ ]:
!for l in *.ipynb; do echo $l; done

In [ ]:
!cat "./[0]-Introduction_to_Jupyter_Notebook.ipynb" | grep "cell_type" | head

You can even mix bash and python in the same cell


In [ ]:
# Attribute a bash output to a python variable
a = !ls

# Loop with python over the lines returned previously
for file in a:
    # print with bash
    !echo {file}

More languages

You can also use special python Magics to run whole cells under other language interpreters


In [ ]:
%%bash
echo -e "Bash kernel!\n"

In [ ]:
%%perl
print "PERL Kernel\n";

In [ ]:
%%python3
print ("Python3 kernel")

In [ ]:
%%R
cat ("R kernel")

In [ ]:
%%javascript
alert("Javascript kernel");

Now it is you turn to get your hands dirty

  • First open a terminal

  • Go to a working directory (with cd and mkdir)

  • Launch the notebook server with:

jupyter notebook

  • A tab will be open in you default web browser

  • If not, or if you want to use a different browser, open the browser and type the following URL :

http://localhost:8888/

  • Explore the tabs of the control panel

  • Navigate in the files/folders in the files tab

  • You can create new, files, folder or run a terminal

  • Create a new python3 notebook

    Handy shortcuts

    A : insert cell above

    B : insert cell below

    X : cut selected cells

    C : copy selected cells

    Y : convert cell type to code

    M : convert cell type to markdown

    R : convert cell type to raw

  • Now you can play with markdown and code cells...


Presentation powered by Jupyter Notebook with RISE ("Live" Reveal.js Jupyter/IPython Slideshow Extension)