Print Variable In Jupyter Notebook Markdown Cell Python

Question:

Can I print the value of a variable in Markdown Cell Jupyter Notebook?

Tried Code:

value = 5.3

Markdown cell --> Value is {{ value }} 

I want that the Markdown cell should display the value of variable

SCREENSHOT

Screenshot for Code

Asked By: nilansh bansal

||

Answers:

So After going through all the links I was able to resolve the problem by referring to nbextension jupyter notebook docs : https://github.com/ipython-contrib/jupyter_contrib_nbextensions

Steps Taken:

  1. pip install jupyter_contrib_nbextensions
  2. jupyter contrib nbextension install –user
  3. jupyter nbextension enable python-markdown/main

After the above commands started a jupyter notebook and to print the value of a variable in the markdown cells works like charm!

You just have to use {{ ac_score }} within a markdown cell.

Screenshot

enter image description here

Thanks!

Answered By: nilansh bansal

@nilansh bansal’s answer works great for Jupyter Notebooks. Unfortunately, it doesn’t work for JupyterLab because the plugin is no longer supported (as is the case for all nbextension plugins). Since JupyterLab gains popularity, I wanted to complement the answers so far because it took me quite some time to find a solution. This is because until now there is no plugin compatible with JupyterLab. I have found the following solution for myself by combining this and this SO answers:

from IPython.display import Markdown as md
# Instead of setting the cell to Markdown, create Markdown from withnin a code cell!
# We can just use python variable replacement syntax to make the text dynamic
n = 10
md("The data consists of {} observations. Bla, Bla, ....".format(n))

Alternatively, the last line can be simplified as suggested by @Igor Fobia for Python >3.6:

md(f"The data consists of {n} observations. Bla, Bla, ....")

This leads to the desired output. However, it has the huge disadvantage that the code cell will still be visible when exporting the NB. This can be solved though:

  1. Add a tag to the code cell, i.e. name it "hide"
  2. Configure nbconvert to ignore the tagged cells, e.g. by adding this c.TagRemovePreprocessor.remove_input_tags = {"hide"} to your ~/.jupyter/jupyter_notebook_config.py config file

I have written a detailed blog-post about how I implemented this solution for publishing Notebooks on my blog. If you use JupyterLab < 2.0, you could install the jupyterlab-celltags plugin for JupyterLab to simplify the cell tagging.

Answered By: mc51

You can overwrite %%markdown IPython magic to substitute variables from the global environment:

from IPython.display import Markdown
from IPython.core.magic import register_cell_magic


@register_cell_magic
def markdown(line, cell):
    return Markdown(cell.format(**globals()))

This has the advantage of allowing for Markdown linting when used with JupyterLab-LSP.

If developing a documentation with nbsphinx you can hide the input (source of the cell) by setting {"hide_input": true} in cell metadata:

enter image description here
(note the Jupyter[Lab] and LSP underlined as those are not in the English dictionary – linting works!)

which results in smooth docs like this:

enter image description here

For a better experience when in JupyterLab you can always collapse the input cell by clicking on the blue bar to the left of it (visible when you hover over the cell).

Answered By: krassowski
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.