from random import randint
from IPython.display import Markdown
Dynamic markdown content in Jupyter Lab
markdown
jupyter
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:
- Using the IPython
Markdown
display object and f-strings. ⬇️ - If rendering a notebook with Quarto, you can use the Quarto inline code syntax. ⬇️
- Using the
jupyterlab-myst
plugin/syntax. (Not included below, see docs)
IPython display Markdown object
= randint(0, 100)
dynamic_content
f"""This is the dynamic content in bold: **{dynamic_content=}**
Markdown(""")
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