Open In Colab

Jupyter Notebooks

Throughout the semester, we will work on exercises using the Python programming langugage and Jupyter Notebooks.

What are Jupyter Notebooks?

From Jupyer Notebook: An Introduction

The Jupyter Notebook is an open source web application that you can use to create and share documents that contain live code, equations, visualizations, and text. Jupyter Notebook is maintained by the people at Project Jupyter.

Jupyter Notebooks are a spin-off project from the IPython project, which used to have an IPython Notebook project itself. The name, Jupyter, comes from the core supported programming languages that it supports: Julia, Python, and R. Jupyter ships with the IPython kernel, which allows you to write your programs in Python, but there are currently over 100 other kernels that you can also use.

Anaconda (which we’ve just downloaded) comes with many scientific libraries preinstalled, including the Jupyter Notebook!

Starting a Jupyter Notebook server

  1. Open your terminal application (Terminal on MacOS, Anaconda Prompt on Windows)

  2. Change your current directory to a location on your computer where you would like to save your notebook (for example: cd /Users/home/notebooks)

  3. Run the command jupyter notebook which will start your default browser (or open a new tab) to the following URL: http://localhost:8888/tree

OpenIPYNB

Interactive Coding!!

We will use Jupyter Notebooks to interface with Python. Rather than keeping code scripts code and execution separate, Jupyter integrates both together using the concept of cells. Two main types of cells are code cells and markdown cells. Cells pair “snippets” of code with the output obtained from running them and can contain plots/figures inline alongside code required to generate them.

Code cells contain actual code that you want to run. You can specify a cell as a code cell using the pulldown menu in the toolbar in your Jupyter notebook. Otherwise, you can can hit esc and then y (denoted “esc, y”) while a cell is selected to specify that it is a code cell. Note that you will have to hit enter after doing this to start editing it. If you want to execute the code in a code cell, hit “shift + enter”. Note that code cells are executed in the order you execute them. That is to say, the ordering of the cells for which you hit “shift + enter” is the order in which the code is executed. If you did not explicitly execute a cell early in the document, its results are now known to the Python interpreter.

Markdown cells contain text. The text is written in Markdown, a lightweight markup language. You can read about its syntax here. Note that you can also insert HTML into markdown cells, and this will be rendered properly. As you are typing the contents of these cells, the results appear as text. Hitting “shift + enter” renders the text in the formatting you specify. You can specify a cell as being a markdown cell in the Jupyter toolbar, or by hitting “esc, m” in the cell. Again, you have to hit enter after using the quick keys to bring the cell into edit mode.

In general, when you want to add a new cell, you can use the “Insert” pulldown menu from the Jupyter toolbar. The shortcut to insert a cell below is “esc, b” and to insert a cell above is “esc, a”. Alternatively, you can execute a cell and automatically add a new one below it by hitting “alt + enter”.

# This is a code cell
# Put some code here and get some output below!
x = 10
print(x)
10

Notebooks of the Future

Not just for code:

  • Markdown, HTML, LaTeX integration

  • Slide shows

  • Keep your notes alongside your analysis routines

  • Embed images, videos, anything (it’s all just HTML + javascript)

Markdown

See this comprehensive guide to Markdown or tutorial. Here are some examples:

Emphasis

Here is one way to add emphasis to to Markdown cell text.

Emphasis, aka italics, with *asterisks*.

Emphasis, aka italics, with asterisks.

Here is another way to add emphasis to to Markdown cell text.

Strong emphasis, aka bold, with **asterisks**.

Strong emphasis, aka bold, with asterisks.

Here is another way to add emphasis to to Markdown cell text.

Combined emphasis with ***asterisks***.

Combined emphasis with asterisks.

Here is one way to add strikethough to Markdown cell text.

Strikethrough uses two tildes. ~~Scratch this.~~

Strikethrough uses two tildes. ~~Scratch this.~~

Tables

Colons can be used to align columns (note that this may not render properly in the Jupyter Book website).

| Tables        | Are           | Cool  |
| :------------ |:-------------:| -----:|
| col 1 is      | left-aligned  |     A |
| col 3 is      | right-aligned |     B |
| col 2 is      | centered      |     C |

Tables

Are

Cool

col 1 is

left-aligned

A

col 3 is

right-aligned

B

col 2 is

centered

C

The outer pipes (|) are optional, and you don’t need to make the raw Markdown pretty. You can also use inline Markdown.

Markdown | Less | Pretty
--- | --- | ---
*Still* | `renders` | **nicely**
1 | 2 | 3

Markdown

Less

Pretty

Still

renders

nicely

1

2

3

Inserting images from web

Let’s include the course logo below.

![logo](https://github.com/shawnrhoads/gu-psyc-347/raw/master/docs/static/images/logo-background.jpg)

logo

HTML/CSS Integration:

Here’s an example using HTML!

<font color=red>This is a</font> *markdown* **cell**!

This is a markdown cell!

LaTeX Integration:

Here’s an example using LaTeX

$\sum_{n=1}^{5}n$

\(\sum_{n=1}^{5}n\)

Multilingual!

Jupyter Notebooks support over 150+ languages!

These include:

  • Julia

  • Python

  • R

  • Javascript

Save notebooks in other formats and put them online

Customize your notebook experience with extensions

  • Table of Contents

  • Execution time/Profiling

  • Scratch Space

  • Code/Section folding

  • Look and feel (CSS+Javascript)

  • Other notebook extensions