Databricks Notebook Markdown Cheat Sheet

Databricks Notebook Markdown Cheat Sheet

Databricks notebook can include text documentation by changing a cell to a markdown cell using the %md magic command. Most of the markdown syntax works for Databricks, but some do not. This tutorial talks about the commonly used markdown syntax for Databricks notebook. We will cover:

  • How to format text?
  • How to create an item list and checklist?
  • How to create mathematical equations?
  • How to display an image?
  • How to link to Databricks notebooks and folders?

Resources for this post:

Databricks Mardown Cheat Sheet – GrabNGoInfo.com

Section 1: Format Text

We listed commonly used text styles and the corresponding markdown syntax in the format text section.

  • Headings are created by adding a pound sign (#) at the beginning.# Heading 1 ## Heading 2 ### Heading 3
  • Bold is achieved by adding two stars (**) at both ends. **This is bold**
  • Italic is achieved by adding one star (*) at both ends. *This is italicized*
  • Strikethrough is achieved by adding two tildes (~~) at both ends. ~~This is a strikethrough~~
  • Blockquote is achieved by adding one greater than sign (>) at the beginning. > This is a blockquote
  • Inline code is achieved by adding one backtick () at both ends. `This is an inline code`
  • To escape the backtick in the inline code, use double backticks (`` ). `` `This escapes backtick` ``
  • code block is achieved by adding a triple backtick (``` ) at both ends.``` This is a code block ```
  • To escape the triple backticks in the code blocks, wrap with four backticks (```` ) or three tildes (~~~).
  • Link is created by wrapping the text with square brackets and the link with parenthesis. [GrabNGoInfo](https://grabngoinfo.com/)
  • Horizontal rule is created by three dashes (---).
  • Highlight is achieved by setting the background color in the html span tag. This is <span style="background-color: #FFFF00">highlighted</span>
  • Color is achieved by setting the color in the html span tag. This is <span style="color:red">red</span>
  • To add space between lines, we can use the html br tag. Line above space <br/><br/> Line below space
  • Databricks notebook does not support emoji shortcode such as :heart:, but we can copy the emoji image and paste it directly to the markdown cell. For example, GrabNGoInfo is awesome! ❤️
  • Subscript is achieved by adding underscore in curly braces. \\(A{_i}{_j}\\)
  • Superscript is achieved by adding caret in curly braces. \\(A{^i}{^j}\\)
  • Table is achieved using the combination of pipes and dashes.
| Column 1 | Column 2 | 
| ----------- | ----------- | 
| Row 1 | Value 1 | 
| Row 2 | Value 2 |

Below is a list of example markdown code

%md
# Heading 1
## Heading 2
### Heading 3

**This is bold**

*This is italicized*

~~This is a strikethrough~~

> This is a blockquote

`This is an inline code`

`` `This escapes backtick` ``

```
This 
is
a
code
block
```

[GrabNGoInfo](https://grabngoinfo.com/)

---

This is <span style="background-color: #FFFF00">highlighted</span>

This is <span style="color:red">red</span>

Line above space <br/><br/> Line below space

GrabNGoInfo is awesome! ❤️

\\(A{_i}{_j}\\)

\\(A{^i}{^j}\\)

| Column 1 | Column 2 |
| ----------- | ----------- |
| Row 1 | Value 1 |
| Row 2 | Value 2 |

The code renders to the output below

Databricks Notebook Markdown Cheat Sheet
Databricks Markdown Text Format – GrabNGoInfo.com

Section 2: Item List

In the item list section, we will talk about creating an ordered list, unordered list, and checklist.

An ordered list is created by adding numbers at the beginning.

1. ordered item 1
2. ordered item 2
3. ordered item 3

An unordered bullet point list is created by adding a dash (-) or a star (*) at the beginning.

- bullet point 1
- bullet point  2
- bullet point  3
* bullet point 4
* bullet point 5
* bullet point 6

A checklist can be created by adding cross (&cross;), check (&check;), and underscore (_) at the beginning.

- &cross; bullet point 1
- _ bullet point  2
- &check; bullet point  3

The code renders to the output below.

  1. ordered item 1
  2. ordered item 2
  3. ordered item 3
  • bullet point 1
  • bullet point 2
  • bullet point 3
  • bullet point 4
  • bullet point 5
  • bullet point 6
  • ✗ bullet point 1
  • _ bullet point 2
  • ✓ bullet point 3

To create a list in the cell of a table, use HTML.

| Column 1 | Column 2 | Column 3 |
| ----------- | ----------- | ------------|
| Row 1 | Value 1 | <li> item 1 </li>  <li> item 2 </li>
| Row 2 | Value 2 | <ol> <li> item 1 </li>  <li> item 2 </li> </ol>

The code gives us a bullet point list and an ordered list in a table.

Databricks Notebook Item List In Table – GrabNGoInfo.com

To create a nested list, add two spaces in front of a dash (-) or a star (*)

- bullet point 1
  - nested bullet point 1
  - nested bullet point 2    
* bullet point  2
  * nested bullet point 1
  * nested bullet point 2

The code generates the nested list below.

Databricks Markdown Nested List – GrabNGoInfo.com

Section 3: Mathematical Equations

Databricks notebook supports KaTex. Below are examples from the Databricks documentation.

\\(c = \\pm\\sqrt{a^2 + b^2} \\)

\\(A{_i}{_j}=B{_i}{_j}\\)

$$c = \\pm\\sqrt{a^2 + b^2}$$

\\[A{_i}{_j}=B{_i}{_j}\\]

\\( f(\beta)= -Y_t^T X_t \beta + \sum log( 1+{e}^{X_t\bullet\beta}) + \frac{1}{2}\delta^t S_t^{-1}\delta\\)

where \\(\delta=(\beta - \mu_{t-1})\\)

This renders to the equations below

Databricks Notebook Markdown Cheat Sheet - Math Equation
Databricks Math Markdown – GrabNGoInfo.com

Section 4: Image

Databricks markdown can take images from a URL or FileStore.

Access Image Via URL

To read an image from a URL, we can use an exclamation mark, followed by the same syntax as creating a link. Recall that a link is created by wrapping the text with square brackets and the URL with parenthesis. For example, the code ![GrabNGoInfo Logo](https://grabngoinfo.com/wp-content/uploads/2021/09/cropped-Converted-PNG-768x219.png) renders to the image below.

Databricks Notebook Markdown Cheat Sheet - Image URL
Databricks Markdown Image URL – GrabNGoInfo.com

We can also format the appearance of the image using HTML. For example, the code below specifies the image alignment, line height, padding, and image size.

<div style="text-align: center; line-height: 10; padding-top: 30px;  padding-bottom: 30px;">
  <img src="https://grabngoinfo.com/wp-content/uploads/2021/09/cropped-Converted-PNG-768x219.png" alt='GrabNGoInfo Logo' style="width: 500px" >
</div>

It renders a smaller image in the center of the cell with some padding spaces.

Databricks Notebook Markdown Cheat Sheet - Image URL in HTML
Databricks Markdown Image URL in HTML – GrabNGoInfo.com

The image can also be displayed with text using displayHTML()

html = """ <img src="https://grabngoinfo.com/wp-content/uploads/2021/09/cropped-Converted-PNG-768x219.png" alt="GrabNGoInfo logo" style="width: 200px"> <h1 style="color:#EE563F;text-align:center;font-family:Courier">GrabNGoInfo Machine Learning Tutorials</h1>"""
displayHTML(html)
Databricks display HTML – GrabNGoInfo.com

To have the image and text in the same cell, just add the image link. For instance, the following code adds an image in front of the heading one text.

%md
# ![GrabNGoInfo Logo](https://yt3.ggpht.com/7E9faspWAL4myE_59Cz1bPNlDxXDZLNsu8MluRLcIFljXk72GyqZ7Pyn8MdFhimUGN7KaBGN=s176-c-k-c0x00ffffff-no-rj) GrabNGoInfo Machine Learning Tutorials
Databricks markdown image and text in the same cell – GrabNGoInfo.com

Access Image Via FileStore

To access an image through FileStore, we need first to upload the image. Using %fs ls, we can see that the image was saved under dbfs:/FileStore/tables/.

%fs ls FileStore/tables
Databricks Notebook Markdown Cheat Sheet - FileStore Image
Databricks Markdown Image FileStore Location – GrabNGoInfo.com

The code ![GrabNGoInfo Logo](files/tables/Converted_PNG.png) renders to the image below.

Databricks Notebook Markdown Cheat Sheet - Image FileStore
Databricks Markdown Image FileStore – GrabNGoInfo.com

We can also format the appearance of the image using HTML.

<div style="text-align: center; line-height: 10; padding-top: 30px;  padding-bottom: 30px;">
  <img src="files/tables/Converted_PNG.png" alt='GrabNGoInfo Logo' style="width: 500px" >
</div>
Databricks Notebook Markdown Cheat Sheet - Image FileStore in HTML
Databricks Markdown Image FileStore in HTML – GrabNGoInfo.com

The code below can be used to link to other notebooks or folders using relative paths.

<a href="$./myNotebook">Link to notebook in same folder as current notebook</a>
<a href="$../myFolder">Link to folder in parent folder of current notebook</a>
<a href="$./myFolder2/myNotebook2">Link to nested notebook</a>

For example, the code <a href="$./DB01_Databricks Mount To AWS S3 And Import Data">Databricks Mount To AWS S3 And Import Data</a> render to the link to the notebook called ‘DB01_Databricks Mount To AWS S3 And Import Data’.

Databricks Mount To AWS S3 And Import Data

Summary

This tutorial talked about the commonly used markdown syntax for Databricks notebook. We covered:

  • How to format text?
  • How to create an item list and checklist?
  • How to create mathematical equations?
  • How to display an image?
  • How to link to Databricks notebooks and folders?

For more information about data science and machine learning, please check out my YouTube channel and Medium Page or follow me on LinkedIn.

References

2 thoughts on “Databricks Notebook Markdown Cheat Sheet”

Leave a Comment

Your email address will not be published. Required fields are marked *