Markdown and Formatting
Markdown is a lightweight and popular Markup language which is a writing standard for data scientists and analysts. It is often converted into the HTML format for web viewing. Jupyter Notebook recognizes markdown and renders markdown instructions into rich text.
Markdown allows you to write using an easy-to-read and easy-to-write plain text format, then convert it to structurally valid XHTML (or HTML). It’s very simple to format words as bold or italic, include links, create lists, and so on.
Remember, in Jupyter Notebook, you need to execute the cell (press Shift + Enter
) to apply the markdown formatting.
Basic markdown syntax for headings, emphasis, lists, and links
Headings
Headings are created by using the #
symbol before your heading text. The number of #
you use will determine the size of the heading. There are six sizes you can create, similar to HTML heading tags <h1>
- <h6>
.
Emphasis
Emphasis, such as bold and italic, can be added by wrapping text in *
or _
characters.
Lists
Markdown supports ordered and unordered lists.
To create an unordered list, you can use asterisks *
, hyphens -
, or plus +
.
For an ordered list, you simply number your items.
Links
You can create an inline link by wrapping the link text in brackets [ ]
, and then wrapping the URL in parentheses ( )
.
To create a reference link, you include a labeled reference in brackets [ ]
, followed by a reference in brackets. Later, you define the URL of the reference like so:
You can also use a shortcut reference link, which is similar to a standard reference link, but you only have to include the reference, not the label.
Inserting images and tables
Inserting images
Inserting images in Jupyter Notebook using markdown is quite simple. The syntax is similar to creating a link - you just need to add an exclamation mark !
before the square brackets [ ]
. Inside the square brackets, you place an alternative text for your image (displayed when the image can’t be loaded), and in the parentheses ( ), you add the URL or path of the image.
For an image hosted on the web, you would use the URL of the image:
Or you can use drag and drop the image into the notebook. It’s possible only to Markdown cells. The image is encoded with Base64, and its text representation is included in the ipynb file.
(source: mljar)
Tables
Creating tables in markdown is also straightforward, although it can be a bit finicky with the alignment of columns. You use a combination of |
to denote columns and -
to denote the heading row of the table.
Below is the syntax for a basic table:
This will render as:
Column 1 | Column 2 | Column 3 |
---|---|---|
Data 1 | Data 2 | Data 3 |
Data 4 | Data 5 | Data 6 |
In this table:
- The first row is the table header.
- The second row contains a series of dashes
---
used to separate the table header from the table body. - Rows three and four contain the table data.
When creating tables, the cells need not align perfectly in the raw markdown. As long as they are separated by |
, the table will render correctly.
Adding code snippets in markdown cells
In Jupyter Notebook, you can insert code snippets into the markdown cells. This is useful for explaining code, displaying the syntax of a specific language, or preventing the code from being executed.
To add a code snippet in a markdown cell, you need to wrap your code inside a pair of three backticks (```). Additionally, you can specify the programming language directly after the opening backticks to enable syntax highlighting.
Without specifying a language:
This will render as:
Specifying a language (Python in this case):
This will render as:
You can also use inline code snippets in your markdown. For that, enclose your code in a single backtick (`).
For example:
This will render as: