91

I have an IPython notebook and I would like to use one of my variables inside a markup cell. Is this even possible? If so, how do you do it?

Eric Leung
  • 2,354
  • 12
  • 25
Juan M. Caicedo
  • 1,066
  • 1
  • 7
  • 6

4 Answers4

83

If you don't mind a code cell that does the job, there is a possibility without adding any extensions.

from IPython.display import Markdown as md

fr=2 #GHz

md("$f_r = %i$ GHz"%(fr))

This will show a markdown cell in a nicely LaTeX formatted output

AS1
  • 1,115
  • 1
  • 9
  • 7
  • +1, more exactly, this shows [the code *and* its result](https://i.stack.imgur.com/3YB2b.png) as output. A cell [may be hidden](https://stackoverflow.com/questions/31517194/how-to-hide-one-specific-cell-input-or-output-in-ipython-notebook), but that doesn't seem easier than including a variable in markdown. – mins Oct 21 '20 at 10:34
  • It does not show the code if you ... convert the notebook to a report with no input 2021. This is an amazingly good answer! – AturSams Jul 21 '21 at 07:52
  • actually the better way to do this is by wrapping the thing with display( md(...) ) since md() will return the "value" of that latex image.. and display() will print that to the notebook. if you only have single equation than empty md() is equivalent to writing "fr" which prints the value to console as well.. – Yarden Cohen Aug 03 '23 at 19:14
32

Currently, this is not possible, however there is a large discussion on this topic here https://github.com/ipython/ipython/pull/2592. The PR is currently closed, but a corresponding issue is opened https://github.com/ipython/ipython/issues/2958 and marked as wishlist.

Update

In the meantime an IPython extension has appeared which allows to render python variables in markdown cells. This extension is part of the IPython notebook extensions and works with IPython 2.x and 3.x. For a detailed description see the wiki page.

Jakob
  • 19,815
  • 6
  • 75
  • 94
  • Do you know if this also works if I use a non-Python kernel (e.g. R or MATLAB)? – Verena Haunschmid Jan 10 '17 at 06:45
  • @VerenaHaunschmid I never tried. But you might ask this question in the github issues of this package. – Jakob Jan 10 '17 at 22:02
  • 6
    The extension needed for python variables being rendered inside markdown cells is called `Python Markdown`. For enabling it, once [Jupyter notebook extensions](https://github.com/ipython-contrib/jupyter_contrib_nbextensions) have been installed, it's recommendable to also install [jupyter_nbextensions_configurator](https://github.com/Jupyter-contrib/jupyter_nbextensions_configurator), and then check it in the configuration frontend (i.e. http://localhost:8888/nbextensions). – Takeshi Gitano Feb 28 '19 at 11:04
  • 1
    This answer could be improved. State what the nbextension is called. The second link is also dead. – Martin Thøgersen Mar 06 '20 at 09:56
23

It is not officially supported, but installing the python markdown extension will allow you to do so. It is part of the nbextensions, for which you will find installation instructions on their github page. Make sure you'll enable the python markdown extension using a jupyter command or the extension configurator.

Calling python variables then should work with the {{var-name}} syntax, which is described in the readme of the corresponding github page (linked in the wiki):

For example: If you set variable a in Python

a = 1.23

and write the following line in a markdown cell:

a is {{a}}

It will be displayed as:

a is 1.23

Further info on this functionality being integrated into ipython/jupyter is discussed in the issue trackers for ipython and jupyter.

Honeybear
  • 2,928
  • 2
  • 28
  • 47
  • If it is required to select the number of significant figures to display, the following trick may be helpful: {{'%.3f' % np.pi}} which gives 3.142 as output. – Luis Jose Jun 10 '20 at 14:12
  • Will it work in env without the extension, e.g. when I would send my notebook to someone? – maciejwww Mar 22 '21 at 00:03
  • 1
    Note that the notebook has to be trusted, as per another answer. I needed to do this manually. – piedpiper Feb 04 '22 at 10:48
11

The link: installing notebook extention

gives a clear description of what is necessary to enable the use of variables in markdown cells. Following it, performed the following actions to realize it:

conda install -c conda-forge jupyter_contrib_nbextensions
jupyter contrib nbextension install --user

after a successful completion of the above command I enabled the python markup extension, from jupyter dashboard, as per the following illustration: selection for markup extension

check it is selected

Last but not least!!! The NOTEBOOK HAS TO BE TRUSTED to make the markup extension works with python variables and it worked for me!

Community
  • 1
  • 1
OPMendeavor
  • 430
  • 6
  • 14