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.

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.

image-1.png

For an image hosted on the web, you would use the URL of the image:

image-2.png

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.

drop-and-drag.gif (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:

table.png

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:

    code-snippet-1.png

    This will render as:

    code-snippet-2.png
  • Specifying a language (Python in this case):

    code-snippet-3.png

    This will render as:

    code-snippet-4.png

You can also use inline code snippets in your markdown. For that, enclose your code in a single backtick (`).

For example:

code-snippet-5.png

This will render as:

code-snippet-6.png
Back to top