knowledge-kitchen

Jupyter Notebooks

Database Design

  1. Overview
  2. Setup
  3. Cells
  4. Code
  5. IPython.display
  6. Tooltips
  7. Shortcut Cheat Sheet
  8. Conclusions

Overview

Concept

Project Jupyter is a web-based publishing system that can incorporate Python and many other language interpreters into a web pages.

It is extremely handy for sharing code and including documentation and notes for better reproducibility, collaboration, and education.

Key Features

Features of most interest to users:

Setup

Install

JupyterLab - an application that allows you to edit and write Jupyter Notebooks - may be installed on your own computer via the command line, using either of the popular Python package managers, pip or conda.

pip install jupyterlab # try 'pip3' instead of 'pip' if your system requires it

Run

Run from the command line:

jupyter-lab

Anaconda Navigator

JupyterLab can alternatively be launched from Anaconda Navigator, which contains a bundle of common data science tools.

JupyterLab in Anaconda Navigator

Visual Studio Code

As an alternative to JupyterLab, Visual Studio Code includes Jupyter Notebook integration. Simply open up any file with the .ipynb extension, and you will be able to start working in the notebook.

GitHub

While GitHub does not include an execution environment for Jupyter Notebooks, it does allow visitors to view the notebooks. While the output of the notebook will be displayed, it is not possible to modify or re-run the code.

Binder

The Jupyter project includes a notebook execution environment called Binder. Binder allows users to edit and execute notebooks, and includes the ability to load and edit any notebook hosted in a public GitHub repository. Changes made to the notebook are not saved to GitHub, but can be saved locally.

More

Given its popularity within the data science, scientific research, and Python communities, there are even more execution environment options for Jupyter notebooks, including:

Cells

Concept

Notebooks are a collection of individual cells that can be edited and executed. There are two types of cells:

Activating cells

Any cell that has previously been placed in the notebook may be activated by double-clicking on it.

Switching between cell types

Cells are by default Code cells. Use shortcuts to switch the type of a cell:

Inserting new cells

There are two useful shortcuts for creating new cells:

Deleting cells

To delete the currently-active cell, use the Esc-x shortcut.

Executing cells

To execute the contents of a cell, press one either Shift-Enter to execute and move to the next cell below, or Control-Enter to execute but keep the current cell selected.

Code

Concept

A few notes about writing code in Jupyter Notebooks…

Output expressions as the last line of code in a cell

If the last statement in a cell is an expression of some kind, the value of that expression will be output in the notebookwithout the need for print().

For example:

# function definition that returns a value
def return_something():
    return 'This will be output without print!'

# calling it as the last line in the cell will output the return value
return_something()

Outputs the following: 'This will be output without print!'

Output expressions as the last line of code in a cell (continued)

The code:

# a simple variable assignment
x = 10

# referencing it as the last line in the cell will output the value
x

Outputs the following: 10

Output expressions as the last line of code in a cell (continued again)

The code:

# a boolean expression
(True and False) and (not True)

Outputs the following: False

While the code:

# a literal
4

Outputs the following: 4

Output values in the middle of a cell using print()

Should you need to output values in the middle of a cell, you may use print().

For example:

for i in range(5):
    print(i)
print("Done")

Outputs the following:

0
1
2
3
4
Done

IPython.display

The problem with print()

While print() works well for simple primitive values, it is really better used on the command line, not in a notebook.

Outputting more complex data structures

For example:

from IPython.display import display, Image, Markdown

my_list = [ {"foo", "bar"}, {"baz": "bum"} , {"biddle": "diddle"} , {"dum": "fum"} , {"gram": "pram"} ]
display( my_list )

Outputs the following:

[{'bar', 'foo'},
 {'baz': 'bum'},
 {'biddle': 'diddle'},
 {'dum': 'fum'},
 {'gram': 'pram'}]

Rendering markdown code

It is possible to programmatically render Markdown code in a notebook. For example:

# some Markdown code, stored in a variable
my_markdown = '''
A simple Markdown list:
- first item
- second item, with some **bold text**
- third item, with a [link](https://knowledge.kitchen)
'''

# render the markdown
Markdown(my_markdown)

Outputs the following:

A simple Markdown list:

Rendering images

It is also possible to render an image into a notebook. For example, the following code…

from IPython.display import display, Image, Markdown

Image("https://knowledge.kitchen/content/courses/database-design/slides/assets/jupyter/donkey.png")

… will render the image from the referenced URL or file path:

Donkey

Tooltips

Autocomplete

While writing code, trigger an autocomplete suggestion box to appear at any time by typing the Tab key.

Function documentation

To view the documentation of any function, place the cursor between the opening and closing parentheses of the function in your code, and press the shortcut, Shift-Tab.

Shortcut Cheat Sheet

Recap

Here are the keyboard shortcuts we have covered:

Conclusions

Thank you. Bye.