A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company. Jupyter Notebook supports Markdown. This lesson will cover some of the things you can do using Markdown to interlace your thoughts and code in Notebooks to tell compelling stories about your data. You’ll see how to format text, add syntax highlighting, make lists, and other ways to make text presentable.
When a new Jupyter notebook opens, you will see the Jupyter notebook interface. Across the top of the notebook you see the Jupyter icon and the notebook name. You can click on the notebook name field and change the name of the notebook. Note that the file extension .ipynb
is not printed in the file name field, but if you look in the Home tab, you will see that the notebook is saved with the .ipynb
extension.
A Jupyter notebook is comprised of a bunch of cells which are arrayed one after another in boxes below the menu items and buttons. There are three main types of cells: code cells, output cells, and markdown cells.
In code cells, you can write Python code, then execute the Python code and see the resulting output. An example of a code cell is shown below.
You can tell you are typing in a code cell because In [ ]:
is shown to the left of the cell and the cell-type drop-down menu shows Code.
To run the Python code in a code cell push the [Run] button or type [Shift]+[Enter]. Far commander for mac. Hitting [Enter] when the cursor is inside a code cell brings the cursor down to a new line.
After a code cell is run, an output cell can be produced below the code cell. The output cell contains the output from the code cell above it. Not all code produces output, so not all code cells produce output cells. The results in output cells can't be edited. If a code cell produces plots, charts or images, these outputs are shown in output cells.
You can clear all the output cells and re-run code cells by selecting [Kernal] --> [Restart Kernal and Clear Output].
Markdown cells don't contain Python code. Markdown cells contain text written in Markdown format. Text in markdown cells can be formatted to show bold or italic text. Tables, images, and lists can also be included in markdown cells.
Markdown cells are used for documentation and explaining your code. The text in a markdown cell is not executed. Markdown cells can be formatted with a few special characters.
Markdown cells are run like code cells. The difference is that when markdown cells are run, the text is formatted (when code cells run, code is executed). Markdown cells are run by clicking the [Run] button or by pressing [Shift]
+ [Enter]
.
Text in markdown cells can be formatted using markdown syntax. An example of markdown syntax is putting an underscore before and after a word to cause the word to be formatted in italics.
Headings are created in markdown cells using the hash symbol #
. One #
is the largest heading. Four hashes ####
is the smallest heading.
Code blocks can be inserted in Jupyter notebook markdown cells. For inline code blocks use the ` left quote character, the character to the left of the number [1]
and above [Tab]
on most keyboards.
This is inline code: ` ` ` Inline code block ` ` ` within a paragraph
For a separated code block use three ` left quote characters on one line, followed by the code block on separate lines. Terminate the separate code block with a line of three ` left quote characters.
```
Separated code block
```
The code in markdown cell code blocks do not execute when the markdown cell is run. A code block in a markdown cell is formatted when the markdown cell executes.
Bold and italic font is displayed by surrounding text with a double asterisk for **bold**
and a single underscore for _italics_
**bold**
produces bold Scanners and printers for mac os.
_italics_
produces italics
**_bold and italic_**
produces bold and italic
Tables are displayed using the pipe |
character, which is [Shift]
+ []
on most keyboards. Columns are separated by pipes |
and rows are separated by lines. After the header row, a row of pipes and dashes ---
are needed to define the table.
produces:
header1 | header 2 | header 3 |
---|---|---|
col 1 | col 2 | col 3 |
col 1 | col 2 | col 3 |
Bullet points are produced using the asterisk character *
produces
Numbered lists are produced using sequential numbers followed by a dot. Indent sub-items with two spaces.
produces
A horizontal rule is specified with three asterisks ***
on a single line.
produces
Hyperlinks are specified using a set of square brackets [ ]
followed by a pair of parenthesis ( )
The text inside the square brackets will be the link, the link address goes in the parenthesis.
produces
Images are embedded in Jupyter Notebook markdown using the exclamation point and square brackets ![ ]
, followed by the image file path in parenthesis ( )
. If the image can not be displayed, the text in square brackets will be shown. The image can be in the same directory as the notebook, or a relative path can be specified. In this case, the image engineering.png
is stored in the images
directory, which is a subdirectory of the directory the notebook is saved in.
displays the image
LaTeX Math equations and symbols are rendered by markdown cells. A more extensive list of LaTeX commands can be found in the appendix.
produces
Because Jupyter notebooks are rendered by web browsers, just about any HTML tag can be included in the markdown portion of a notebook. An example of an HTML tag is the <sup>
</sup>
tags that surround superscript text.
produces
x2
Text can be colored using html <font>
</font>
tags
produces
Red Text
bootstrap style warning boxes can be included in Jupyter notebook markdown using <div>
tags
produces
You can create a new cell in a Jupyter Notebook by clicking the [+] button in the upper menu. Clicking the [+] button produces a new code cell below the active cell.
You can also create a new cell using Insert --> Insert Cell Above or Insert Cell Below. You can choose to insert a cell above or below the active cell.
The type of cell: code cell or markdown cell, is changed by clicking on a cell and selecting the cell type from the drop-down menu. Typing [Esc]
+ [m]
changes the cell type to a markdown cell. Typing [Esc]
+ [y]
changes the cell type to a code cell.
Jupyter notebooks can be saved using the save icon in the upper menu or by pressing [Ctrl] + [s].
Jupyter notebooks can also be saved as a copy, similar to the Save As command common in many programs. To save a copy of a Jupyter notebook use File --> Make a Copy..
Jupyter notebooks are renamed by clicking on the notebook name above the upper menu and typing a new name into the dialog box.
Jupyter notebooks can be downloaded and saved using File --> Download As --> Notebook (.ipynb). Selecting this menu option will download the notebook as a .ipynb
file.
Note that when a .ipynb
file is viewed in a text editor like notepad, the notebook is unformatted and looks like a confusing jumble of text. The notebook needs to be opened in a Jupyter notebook file browser in order for the code in the notebook to run and the markdown text to render.
Jupyter notebooks can be saved in other formats besides the native .ipynb
format. These formats can be accessed using the [File] --> [Download As] menu.
The available file download types are:
When a notebook is saved as a .py
file, all text in markdown cells is converted to comments, and any code cells stay intact as Python code.