Jupyter-Notebook

a.k.a

IPython-Notebook

- Suraj Deshmukh

@surajssd009005

IPython-Interpreter

  • IPython is a command shell for interactive computing in multiple programming languages
  • It provides a rich architecture for interactive computing
  • It comes with advanced features, which default python interpreter lacks
  • It offers introspection, rich media, shell syntax, tab completion, and history.

IPython-Notebook

  • IPython Notebook is a web-based interactive computational environment for creating IPython notebooks
  • It's an interactive computational environment, in which you can combine code execution, rich text, mathematics, plots and rich media
  • A browser-based notebook with support for code, text, mathematical expressions, inline plots and other media

  • It started as a Python specific tool
  • People started writing kernel for other languages
  • Also they wanted to make it language agnostic so the project was renamed
  • Julia + Python + R = Jupyter not limited to these three languages, but supports more than 40 programming languages

Features:

  • Execellent tool that combines code and documentation
  • So can be called as dynamic documentation tool
  • Tool for doing reproducible research
  • Insert images, text, graphs, etc.
  • Convert the notebook to HTML, Markdown, RST, PDF, Latex, etc.
  • Display rich data representations (e.g. HTML / LaTeX / SVG) in the browser as a result of computations.
  • Compose rich text using Markdown and HTML.
  • Include mathematical equations, rendered directly in the browser by MathJax.
  • Import standard Python scripts.
  • In-browser editing, syntax highlighting, tab completion and autoindentation.
  • Inline figures rendered by the matplotlib library with publication quality, in a range of formats (SVG / PDF / PNG).

Working:

Modes of execution

  • Command mode
  • Editing mode

Pressing esc in editing mode will take you to command mode. Pressing enter on any cell will take you into editing mode.

Sample Python

Python Interpreter behavior on Notebook

In [36]:
print 'Hello World'
1 + 3
Hello World
Out[36]:
4
  • Press Ctrl + Enter to execute current cell
  • Press Shift + Enter to execute current cell and select below
In [2]:
22 * 36
Out[2]:
792
In [1]:
ans = raw_input('Do you find IPython Awesome? : ')
print ans
Do you find IPython Awesome? : yay
yay

Stop execution in the middle using I + I

In [3]:
import time
for _ in xrange(1000):
    time.sleep(10)
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-3-61e98a0e0bd2> in <module>()
      1 import time
      2 for _ in xrange(1000):
----> 3     time.sleep(10)

KeyboardInterrupt: 

Specialities of IPython

Auto-complete

Press tab, when you are stuck and it will auto-complete.

In [4]:
import random
In [ ]:
random.randint

Auto-complete also works with file names

In [5]:
open('IPython Notebook.ipynb')
Out[5]:
<open file 'IPython Notebook.ipynb', mode 'r' at 0x7f6924fe8ae0>

Getting help in IPython

List IPython Features

In [6]:
?
In [7]:
import time

Get help related to the module

In [8]:
time?

Get function help

In [9]:
time.sleep?

Open 'source code' of the module or function

In [10]:
random??
In [11]:
random.randint??

Using OS commands in IPython

Use '!'(bang) infront of the shell command and execute it.

In [12]:
!pwd
/home/hummer/Study/talk/IPython
In [13]:
for _ in xrange(2):
    !espeak 'Hello World'

Mix python code and shell code

In [14]:
files = !ls
print files
['data.txt', 'demo.ipynb', 'file.txt', 'ipython-in-depth', 'IPython Notebook.ipynb', 'jupyter_logo.svg', 'Jupyter.png', 'kernel_workflow.png', 'Talk.ipynb', 'test.py', 'untitled.txt']

Add python code in shell code using curly braces

In [16]:
files = !ls
print files
print
!echo {files[0].upper()}
['data.txt', 'demo.ipynb', 'file.txt', 'ipython-in-depth', 'IPython Notebook.ipynb', 'jupyter_logo.svg', 'Jupyter.png', 'kernel_workflow.png', 'Talk.ipynb', 'test.py', 'untitled.txt']

DATA.TXT

Other features

Wildcard search

In [17]:
import os
os.*path*?

Output history

In [20]:
Out
Out[20]:
{2: 792,
 5: <open file 'IPython Notebook.ipynb', mode 'r' at 0x7f6924fe8ae0>,
 19: 792}

Input History

In [23]:
In
Out[23]:
10

History in bash style

In [ ]:
%history -n

It can also interpret commands copied from interpreter

In [24]:
>>> from collections import defaultdict
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
>>> d = defaultdict(list)
>>> for k, v in s:
...     d[k].append(v)
...
>>> d.items()
Out[24]:
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]

IPython Magics

The magic function system provides a series of functions which allow you to control the behavior of IPython itself, plus a lot of system-type features.

Ref: %magic

Open IPython quick reference

In [25]:
%quickref

Show help for all IPython Magic functions

In [26]:
%magic

List currently available magic functions.

In [27]:
%lsmagic
Out[27]:
Available line magics:
%alias  %alias_magic  %autocall  %automagic  %autosave  %bookmark  %cat  %cd  %clear  %colors  %config  %connect_info  %cp  %debug  %dhist  %dirs  %doctest_mode  %ed  %edit  %env  %gui  %hist  %history  %install_default_config  %install_ext  %install_profiles  %killbgscripts  %ldir  %less  %lf  %lk  %ll  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %lx  %macro  %magic  %man  %matplotlib  %mkdir  %more  %mv  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %popd  %pprint  %precision  %profile  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %rep  %rerun  %reset  %reset_selective  %rm  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%debug  %%file  %%html  %%javascript  %%latex  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.

Create and Edit files in Notebook

Using cell magic to add text to a file

In [28]:
%%file file.txt

You can create a new file in this way.
Just include the syntax above and then write the content below it and file will be created in the server
directory.
Writing file.txt

Reading file the python way

In [30]:
#print open('file.txt').read()
!cat file.txt
You can create a new file in this way.
Just include the syntax above and then write the content below it and file will be created in the server
directory.

Timeit python functions

Time execution of a Python statement or expression (This is line magic)

In [31]:
%timeit range(100)
The slowest run took 4.78 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 798 ns per loop
In [32]:
%timeit xrange(100)
The slowest run took 36.86 times longer than the fastest. This could mean that an intermediate result is being cached 
10000000 loops, best of 3: 136 ns per loop

This is a cell magic

In [33]:
%%timeit range(100)
range(1000) 
100000 loops, best of 3: 9.47 ┬Ás per loop

Run pure shell script

In [34]:
%%bash
echo 'I am in :' $PWD
echo 'Name of this pc is '
whoami
echo 'Files and directories in current directory include: '
ls
I am in : /home/hummer/Study/talk/IPython
Name of this pc is 
hummer
Files and directories in current directory include: 
data.txt
demo.ipynb
file.txt
ipython-in-depth
IPython Notebook.ipynb
jupyter_logo.svg
Jupyter.png
kernel_workflow.png
Talk.ipynb
test.py
untitled.txt

IPython Exception Handling

In [ ]:
def a():
    p = 1
    q = 'hi'
    print p + q
    
def b():
    a()

b()

This command will make trace more verbose than it is.

In [ ]:
%xmode verbose
In [ ]:
def a():
    p = 1
    q = 'hi'
    print p + q
    
def b():
    a()

b()

This is default mode of the trace.

In [ ]:
%xmode context

Thanks !!

  • github: surajssd
  • bitbucket: suraj_deshmukh
  • twitter: surajssd009005
  • email: [email protected]
  • site: https://deshmukhsuraj.wordpress.com/

QA