Jupyter Notebook Introduction

History

IPython

  • Initial Release: 2001
  • "Command shell for interactive computing in multiple programming languages." Wikipedia.org
  • Interactive, parallelism, history, environment, etc...

Notebook

Modes:

  • Command
  • Edit

Shortcuts

Key(s) Action
H show shortcut menu
A add cell above
B add cell below
M markdown cell
Y code cell
DD delete cell
Shift + Enter run cell on move to next
Ctrl + Enter run cell on remain
Alt + Enter run cell and move to newly created cell

Editing

Key(s) Action
Enter go into insert mode
Escape exit insert mode
Tab code completion
Shift + Tab tooltips (3 versions)
Ctrl + S save

Markdown:

  • Headings (#)

  • Lists (*)

  • Blockquote

    This is an example.

  • Horizontal Rule (---)


  • Embedded code (three backticks) --> this text is inline code
def my_function():
    """My syntax is highlighted for the Python language."""
    pass
  • LaTeX $e^{i\pi} + 1 = 0$

$F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$

  • External links: [name](https://some_website.com)
  • Internal links: [name](#the_last_cell) Example

Raw:

this is a raw cell `~!@#$%^&*()[]\{}|;':",./<>?

Code:


In [35]:
some_global_variable = 6

def my_function(arg):
    """This is a docstring."""
    some_global_variable = 1
        
    return some_global_variable

In [36]:
print(my_function(5))


1

In [37]:
some_global_variable


Out[37]:
6

Magic

  • % or %%
  • debug, time, timeit, run, and more...

In [4]:
%time some_list = [x**x for x in range(9001)]


Wall time: 5.52 s

In [12]:
!sudo python3.6 -m pip install matplotlib


[sudo] password for r: 


In [39]:
%matplotlib notebook
import matplotlib, numpy, pandas, seaborn
from matplotlib import pyplot as plt

ts = pd.Series(numpy.random.randn(1000), index=pandas.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
df = pandas.DataFrame(numpy.random.randn(1000, 4), index=ts.index,
                  columns=['A', 'B', 'C', 'D'])
df = df.cumsum()
df.plot(); plt.legend(loc='best')


Out[39]:
<matplotlib.legend.Legend at 0x7fc12340a358>

Bash


In [7]:
!dir


 Volume in drive C is Windows
 Volume Serial Number is 6A27-6402

 Directory of C:\Users\randall.white\Notebooks\DFW_Pythoneers

02/20/2017  12:41 PM    <DIR>          .
02/20/2017  12:41 PM    <DIR>          ..
02/15/2017  08:21 AM    <DIR>          .ipynb_checkpoints
02/15/2017  08:21 AM             5,677 2017-02-15-TUTORIAL-Lexical-Closing.ipynb
02/20/2017  12:41 PM            31,411 2017-03-Presentation-Jupyter-Notebook.ipynb
               2 File(s)         37,088 bytes
               3 Dir(s)  173,638,197,248 bytes free

HTML


In [16]:
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))



In [17]:
from IPython.core.display import HTML
css = open('style-table.css').read() + open('style-notebook.css').read()
HTML('<style>{}</style>'.format(css))


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-17-6fab06cda22c> in <module>()
      1 from IPython.core.display import HTML
----> 2 css = open('style-table.css').read() + open('style-notebook.css').read()
      3 HTML('<style>{}</style>'.format(css))

FileNotFoundError: [Errno 2] No such file or directory: 'style-table.css'

Embed YouTube videos.


In [18]:
from IPython.display import YouTubeVideo
YouTubeVideo('HW29067qVWk')


Out[18]:

Configuration / Environment


In [19]:
!ipython profile locate


/home/r/.ipython/profile_default

In [5]:
# Otherwise run: !ipython profile create

# Sample output:

# [ProfileCreate] Generating default config file: 'C:\\Users\\randall.white\\.ipython\\profile_default\\ipython_config.py'
# [ProfileCreate] Generating default config file: 'C:\\Users\\randall.white\\.ipython\\profile_default\\ipython_kernel_config.py'

Notebook specific configuration values

  • Example location in Windows:

C:\Users\randall.white.jupyter\jupyter_notebook_config.py




In [ ]:


In [20]:
import time

In [22]:
for i in range(5):
    time.sleep(.5)
    print(i)


0
1
2
3
4
5
6
7
8
9

In [23]:
time.sleep(10)
print("I'm waiting to see this...")


---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-23-0149fd37a56b> in <module>()
----> 1 time.sleep(10)
      2 print("I'm waiting to see this...")

KeyboardInterrupt: 

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


The cell below this contains "" which becomes invisible when rendered in Markdown. This cell is of type RAW, so I can type anything here and not worry about escaping it.