Dynamic markdown content in Jupyter Lab

markdown
jupyter
Author

ChuckPR

Published

January 23, 2025

from random import randint

from IPython.display import Markdown

Sometimes I want to put the value of a variable into a markdown cell. I know of three ways to inject variable values in Jupyter Lab markdown:

  1. Using the IPython Markdown display object and f-strings. ⬇️
  2. If rendering a notebook with Quarto, you can use the Quarto inline code syntax. ⬇️
  3. Using the jupyterlab-myst plugin/syntax. (Not included below, see docs)

IPython display Markdown object

dynamic_content = randint(0, 100)

Markdown(f"""This is the dynamic content in bold: **{dynamic_content=}**
""")

This is the dynamic content in bold: dynamic_content=93

You could hide the code from a document rendered by Quarto with by adding an #| echo: false comment to the code cell.

Quarto inline code syntax

Important

You must turn on notebook execution when rendering with quarto for the following to work.

E.g. you can add,

execute: 
    enabled: true 

to your notebook front matter.

The Quarto syntax for injecting the value of a variable into markdown content looks like this:

This is the dynamic content in bold `{python} f'{dynamic_content=}'

And here ⬇️ is what the output looks like when I actually use that syntax in a markdown cell.

This is the dynamic content in bold dynamic_content=93