Getting More out of Jupyter Notebook

Tips + Tricks for Jupyter Notebook.

Intro

One of the benefits of working in Jupyter Notebook is that you can have much more functionality and expression than when working directly in your terminal. This includes adding markdown to customize and style text outside of code blocks, adding mathematical equations with LaTeX, using hotkeys for shortcuts, and lastly using magic commands that come with IPython that yield greater efficiency for common tasks.

Markdown in Jupyter Notebook

Markdown was created in 2004 to allow users to create rich text using plain text which is easy to read and write. What is rich text vs plain text? The difference is that rich text is a file format that allows for formatting and styling whereas plain text contains no formatting, being made up solely of text, line breaks, and spacing.

Markdown is a great technology because you can type plain text and it will get rendered into styled text without having to click any icons, and it’ll be done programmatically. We’ll dive into the markdown syntax that you can add to markdown chunks in your notebook.

Markdown Cells

When you create a new cell, you have a few options in the drop down menu. When working with Markdown, make sure your cell is of the Markdown type, and not of the others.

Image of the Jupyter notebook cell types accessed from the dropdown menu.

Headings

There are six levels of headings available. These are useful for organizing sections in your code. You use the octothorp, also called hashtag # and add the number of them that correspond to the heading level.

Image demonstrating code for the six heading sizes in a markdown cell and an example of the resulting output.

Styling

You can add some styling with Strong and Emphasized text. In HTML there are the <strong> and <em> tags that denote different messages. Strong text shows strong importance for its contents, while emphasis is used to change the tone of a word or phrase as it might be emphasized in speech. Example (“I hate cilantro” vs. “I hate cilantro”).

You can get emphasized text with either one asterisk * or underscore _ around the text. For Strong styling use two asterisks ** or underscores __ around the text.

Image demonstrating code for the Strong and Emphasized text styling in a markdown cell and an example of the resulting output.

Code (monospace)

If you want to make monospace text which is good for denoting code, use a single ` called a back tick, back quote, or left quote around the text.

Image demonstrating code for a monospace in a markdown cell and an example of the resulting output.

Line Breaks

You can create new lines either by including a new line in the code, or using <br> inline with text. Using *** will give you a horizontal line.

Image demonstrating code for a line break in a markdown cell and an example of the resulting output.

Lists (Unordered and Ordered)

For unordered lists use a hyphen - or asterisk * with a space before the text. If you want a sub bullet add a tab before the hyphen - or asterisk *.

Image demonstrating code for an unordered list in a markdown cell and an example of the resulting output.

For ordered lists, use numbers followed by a period for the items like so 1.. Tabs still work for sub ordered lists.

Image demonstrating code for an ordered list in a markdown cell and an example of the resulting output.

Images

You can add images from your directory or from the web ![title](img/picture.png). In between the two brackets our image name will take the place of title, and within the parentheses, the file path or url will take the place of img/picture.png.

Image demonstrating code for adding an image in a markdown cell and an example of the resulting output of the codecademy logo.

Links (External and Internal)

Adding external links are just like images without the exclamation point. Here is how you do it [link text](http://url):

Image demonstrating code for an external link in a markdown cell and an example of the resulting output.

Adding internal sections are even easier just use [section title](#section-title):

Image demonstrating code for an external link in a markdown cell and an example of the resulting output.

Hotkeys in Jupyter Notebook

Hotkeys or keyboard shortcuts can improve your efficiency and save time when you’re working with a new program. Instead of using your mouse which takes your hands off of the keyboard, you can access many of the common functionality with a combination of keys. Jupyter Notebook keyboard shortcuts can be accessed through help>keyboard shortcuts. You can also access them through pressing the Cmd + Shift + P keys for Mac or Ctrl + Shift + P for Linux/Windows to open the command palette. Through the palette you can search for keywords and find the command or use commands that don’t have a keyboard shortcut.

Basic Commands
  • Basic navigation: Enter, Shift-Enter, Up/k, Down/j.
  • Saving the notebook: s.
  • Change Cell types: m to change the current cell to Markdown, y to change it back to code.
  • Cell creation: a to insert a new cell above the current cell, b to insert a new cell below.
  • Cell editing: x, c, v, d, z
  • Delete Current Cell: d + d (press the key twice).
  • Kernel operations: i, 0 (press twice).
  • Split the current cell into two: Ctrl + Shift + -.
  • Find and replace on your code: Esc + f
  • Toggle cell output: Esc + O

LaTeX in Jupyter Notebook

LaTeX is useful for mathematical and scientific writing. For example, showing equations through plain text is difficult and not easily readable. LaTeX allows users to create math equations like you see in textbooks or when you write them on a chalkboard.

Inputting LaTeX

In the markdown cell, you can add LaTeX code between $ for inline outputs. You can also use two $$ to create its own centered paragraph in display mode.

Image demonstrating code for the Classification Rate formual described in LaTeX in a markdown cell and an example of the resulting output.

Important Notes:
  • To add little spacing in display mode use \,
  • To add a new line when in math display mode use \\
  • To display fraction use \frac{arg 1}{arg 2}
  • For power (superscripts text) use ^{}
  • For indices (subscripts) use _{}
  • For roots use \sqrt[n]{arg}
  • The [n] is optional.
Greek Letters

Add \ to create greek letters. Capitalization also works.

Mathematical Symbols

Image demonstrating the various mathematical symbols that can be called in Jupyter notebook.

Magic Commands in Jupyter Notebook

There are also magic commands that can be used to when conducting data analysis in Ipython that can be highly effective when put to use. Magic commands will help you complete commonly executed tasks in your notebook, and come in two varieties; line magics and cell magics. Line magics contain a single percent symbol % prefix and work on a single line of input. Cell magics contain a double percent sign %% prefix and work on multiple lines of input. Here are some examples of magic commands:

  • %lsmagic - Return a list all magic command
  • %run: Execute external python script
  • %load: Load in a local file, URL, function, or class
  • %who: Return a list of any variables that have a certain type within the notebook.
  • %matplotlib notebook: Allow you to interactively work with plots from matplotlib.
  • %matplotlib inline: Allow you to disable interactivity with plots.
  • %%time: Show the time it took to execute the line of code. Good for checking efficiency.

Other Tips

Pretty Displays

Displays all variables and statements on its own line

from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = "all"
Help

A ? before the method or variable will print the help documentation.

Suppressing output of a function with ;

For plots ending with ; will suppress output and show only the plot.

Multi-Cursor with Alt + Mouse

If you want to select more than one row, multicursor functionality can be accessed with alt + mouse click.

Summary

There are numerous ways to enhance your Jupyter Notebook and make it more readable and standardized. We have covered some of the methods, but there are many others that can be found in the Jupyter Notebook User’s Manual.