Learning Objectives
After completing this page, you will be able to:
Surprisingly, Jupyter Notebooks do not support the inclusion of variables in Markdown Cells out of the box. If you still use Jupyter Notebooks there is a readily solution: the Python Markdown extension. It is part of the nbextensions package which is easy to install and configure. Here's how to format Markdown cells in Jupyter notebooks: Headings. Use the number sign (#) followed by a blank space for notebook titles and section headings: # for titles. ## for major headings. ### for subheadings. #### for 4th level subheadings. Use the following code to emphasize text. “First and foremost, the Jupyter Notebook is an interactive environment for writing and running code. The notebook is capable of running code in a wide range of languages. However, each notebook is associated with a single kernel. This notebook is associated with the IPython kernel, therefore runs Python code.”.
- Create new Code and
Markdown
cells withinJupyter Notebook
. - Run Code and
Markdown
cells withinJupyter Notebook
to executePython
code and renderMarkdown
text. - List useful shortcuts for common tasks in
Jupyter Notebook
.
Work With Python Code and Markdown Cells in Jupyter Notebook
Recall that a Jupyter Notebook
file consists of a set of cells that can store text or code.
- Text Cells: Text cells allow you to write and render
Markdown
syntax. This is where you can describe and document your workflow. - Code Cells: Code cells allow you to write and run programming code (e.g.
Python
).
Create New Cells
You can use either the Menu tools or Keyboard Shortcuts to create new cells.
Function | Keyboard Shortcut | Menu Tools |
---|---|---|
Create new cell | Esc + a (above), Esc + b (below) | Insert→ Insert Cell Above OR Insert → Insert Cell Below |
Copy Cell | c | Copy Key |
Paste Cell | v | Paste Key |
While the default cell type for new cells is Code, you can change the cell type of any existing cell by clicking in the cell and selecting a new cell type (e.g. Markdown
) in the cell type menu in the toolbar.
Cell type options include Code, Markdown, Raw NBConvert (for text to remain unmodified by nbconvert), and Heading.
To use the Keyboard Shortcuts, hit the esc
key. After that, you can change a cell to Markdown by hitting the m
key, or you can change a cell to Code by hitting the y
key.
Run Cells
Python Code Cells
You can run any cell in Jupyter Notebook
(regardless of whether it contains Code or Markdown
) using the Menu tools or Keyboard Shortcuts.
Function | Keyboard Shortcut | Menu Tools |
---|---|---|
Run Cell | Ctrl + enter | Cell → Run Cell |
For example, you can add a new Code cell and then run the following Python
code (e.g. 3 + 4
). Your result, or output, will be displayed below the Code cell that you run.
Markdown Cells
You can run Markdown
cells in the same way that you can run code cells. However, when you run a Markdown
cell, the text formatted using Markdown
syntax will be rendered as stylized text.
This means that headings are larger and bold, bulleted lists have bullets next to them instead of *
, and regular text looks normal. No outputs will appear below the Markdown cell.
For example, the Markdown
syntax below represents 3 headers. You can double-click in any Markdown
cell to see the raw Markdown
syntax, which for the cell below would appear like this raw Markdown
syntax:
To see the Markdown
as stylized text, run the cell. It should look like the text printed below:
This is a subtitle in Markdown
This is a smaller subtitle
This is an even smaller subtitle
Rearrange Cells in a Jupyter Notebook
You can change the order of cells within Jupyter Notebook
using the up arrow
and down arrow
buttons on the menu bar. To do this, click inside the cell that you want to move and then press the desired arrow as many times as you need to move the Cell to the desired location.
Clear Results in Jupyter Notebook
Sometimes, you may want to clear any output results that have been produced. You can do this using the Menu:
Menu Tools |
---|
Cell -> Current Outputs -> Clear |
This will clear the current cell that you are working in, which you can activate by clicking in a cell.
You can also clear all of the output using the Menu Tools.
Menu Tools |
---|
Cell -> All Output -> Clear |
You can write content in regular Markdown files (e.g., files ending in .md
).Jupyter Book supports any Markdown syntax that is supported by Jupyter notebooks.Jupyter Notebook Markdown is an extension of a flavour of Markdown calledCommonMark Markdown.It has many elements for standard text processing, though it lacks a lot of features used forpublishing and documentation.
Note
If you’d like a more in-depth overview and guide to CommonMark Markdown, seethe CommonMark Markdown tutorial.
This page describes some basic features of the Jupyter Notebook Markdown, and how toinclude them with your book.
Embedding media¶
Adding images¶
You can reference external media like images from your Markdown file. If you userelative paths, then they will continue to work when the Markdown files are copied over,so long as they point to a file that’s inside of the repository.
Here’s an image relative to the book content root
It was generated with this code:
See also
Images and figures for more information.
Adding movies¶
You can even embed references to movies on the web! For example, here’s a little GIF for you!
This will be included in your book when it is built.
Mathematics¶
For HTML outputs, Jupyter Book uses the excellent MathJax library,along with the default Jupyter Notebook configuration, for rendering mathematics from LaTeX-style syntax.
For example, here’s a mathematical expression rendered with MathJax:
Block-level mathematics¶
You can include block-level mathematics by wrapping your formulas in $$
characters.For example, the following block:
Results in this output:
You can also include math blocks by using LaTeX-style syntax using begin{align*}
.For example, the following block:
Jupyter Notebook Markdown
Results in:
Important
Jupyter Notebook Markdown Image
This requires the amsmath
MyST extension to be enabled.
Extended Markdown with MyST Markdown¶
Jupyter Notebook Markdown Cheat Sheet
In addition to CommonMark Markdown, Jupyter Book also supports a more fully-featured version of Markdown called MyST Markdown.This is a superset of CommonMark that includes syntactic pieces that are useful for publishing computational narratives.For more information about MyST Markdown, see MyST Markdown overview.