FMP AudioLabs
B

Jupyter Notebook


In this notebook, we cover some basics on Jupyter and its usage.

Jupyter is a non-profit, open-source project, born out of the IPython project. It exists to develop open-source software, open-standards, and services for interactive computing across dozens of programming languages. The Jupyter notebook framework allows for keeping code, images, comments, formulas and plots together. Here are some further links:

  • For details we refer to the Jupyter Notebook QuickStart.
  • Some more advanced hints can be found ad the post 28 Jupyter Notebook tips, tricks, and shortcuts.
  • To start the notebook server from the command line, use the command jupyter notebook. This opens the default web browser at the URL http://localhost:8888
  • You may configure jupyter notebook in various ways. For example, to disable cross-site-request-forgery protection (allowing remote content to be shown) you may use the command
    jupyter notebook --NotebookApp.disable_check_xsrf=True

Markdown

Text can be added to Jupyter notebooks using markdown cells. In these cells, one can use traditional HTML, certain LaTeX commands, and also the popular text-to-HTML conversion language Markdown. Here are some examples as used in the FMP notebooks:

  • Headers are marked by hashes (#, ##, ###, ...).
  • Unordered (bulleted) lists use asterisks, pluses, and hyphens (*, +, and -) as list markers.
  • This is an example list.
  • Markdown uses asterisks and underscores to indicate spans of emphasis.
  • This are examples for emphasized or strongly emphasized words.
  • Blockquotes are marked with a > at the beginning of the line.

This is a blockquote. A linebreak is encoded by two blanks at the end of a line.
This is the next line.

This is the next paragraph.
Instead of >, one can also use <blockquote> and </blockquote>

  • Instead of the <code> element, one can use the grave accent (`) to indicate a code fragment
  1. Ordered (numbered) lists use regular numbers, followed by periods, as list markers.
  2. This is an example of a numbered list.
  3. Inline-style links use parentheses immediately after the link text.
    This is an example for a link

Keyboard Shortcuts

Keyboard shortcuts save lots of time. To access these shortcuts, note that Jupyter notebook operates in two modes:

  • In the edit mode (indicated by a green cell border) one can type into the cell in the usual way.
  • In the command mode (indicated by a gray cell border with a blue left margin) one is able to edit the notebook using keyboard shortcuts.

To enter the command mode, one can either press Esc or use the mouse to click outside a cell's editor area. Being in command mode, one can use the following keyboard shortcuts:

  • Esc: switch to command mode
  • H: access help menue with keyboard shortcuts
  • P: open the command palette
  • Ctrl + Enter: run selected cells
  • Shift + Enter: run cell, select below
  • A: insert a new cell above current cell
  • B: insert a new cell below current cell
  • Ctrl + Shift + -: split cell at current cursor position
  • Y: change cell to code
  • M: change cell to markdown
  • D + D (press the key twice): delete current cell
  • Z: undo cell deletion
  • Shift + J or Shift + Down: select cells downwards
    Shift + K or Shift + Up: select cells upwards
    Once cells are selected, one can delete/copy/cut/paste/run them as a batch.
  • Shift + M: merge selected cells
  • X: cut selected cells
  • C: copy selected cells
  • V: paste cells below

Copy multiple cells from one notebook to another

  • Select Cell and press Esc to go to command mode
  • Hit Shift + Up or Shift + Down to select multiple cells
  • Copy with Ctrl + C
  • Paste with Ctrl + V (also possible in different notebook, make sure to be in command mode)
  • You maybe asked to repeat Ctrl + V

Column editing for text cells in edit mode

  • Press Alt button and keep holding it. The cursor should change its shape into a big plus sign.
  • Using the mouse, point to the beginning of the first line and while holding the Alt button and pull down the mouse until the last line.
  • Release the Alt button and edit in the column mode.
  • For example, use the # character to comment multiple lines.

Accessing the Python Help Function

There is an easy way within a Jupyter notebook session to call help for a Python function in order to see the function arguments. Being in a code cell, just place the cursor on the Python function in question and press shift-Tab. If you press shift-Tab several times, the entire documentation string is opened. For example, in the case of the built-in function max, this is equivalent to help(max) or print(max.__doc__).

In [1]:
print(max(2, 3, 4, 5))
# Placing cursor on function name 'max' and press shift-Tab. 
# Then you obtain the information as with help(max).
help(max)
5
Help on built-in function max in module builtins:

max(...)
    max(iterable, *[, default=obj, key=func]) -> value
    max(arg1, arg2, *args, *[, key=func]) -> value
    
    With a single iterable argument, return its biggest item. The
    default keyword-only argument specifies an object to return if
    the provided iterable is empty.
    With two or more arguments, return the largest argument.

Further Notes

  • Sometimes the HTML export of a notebook looks different to how the notebook looks within Jupyer. This is a known (and yet unsolved) issue in Jupyter.
Acknowledgment: This notebook was created by Frank Zalkow and Meinard Müller.
C0 C1 C2 C3 C4 C5 C6 C7 C8