Can I use variables on an IPython notebook markup cell?
Jupyter NotebookIpythonJupyter Notebook Problem Overview
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?
Jupyter Notebook Solutions
Solution 1 - Jupyter Notebook
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
Solution 2 - Jupyter Notebook
Currently, this is not possible, however there is a large discussion on this topic here https://github.com/ipython/ipython/pull/2592.</strike> 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.
Solution 3 - Jupyter Notebook
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.
Solution 4 - Jupyter Notebook
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:
Last but not least!!! The NOTEBOOK HAS TO BE TRUSTED to make the markup extension works with python variables and it worked for me!