Can I use variables on an IPython notebook markup cell?

jupyter notebook markdown
jupyterlab variables in markdown
ipython display(markdown)
jupyter notebook extensions
markdown notebook
jupyter markdown brackets
jupyter notebook extensions runtime
jupyter notebook disable extensions

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?

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.

Print Variable In Jupyter Notebook Markdown Cell Python, Surprisingly, Jupyter Notebooks do not support the inclusion of variables in Markdown Cells out of the box. If you still use Jupyter Notebooks  Surprisingly, Jupyter Notebooks do not support the inclusion of variables in Markdown Cells out of the box. If you still use Jupyter Notebooks there is a readily solution: the Python Markdown extension. It is part of the nbextensions package which is easy to install and configure.

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

Include variables in Markdown cells of JupyterLab Notebooks, What syntax do we use? Are we happy with the {{}}; How do we make sure we are processing Markdown in a  In order to enter a text which should not be treated as code by Notebook server, it must be first converted as markdown cell either from cell menu or by using keyboard shortcut M while in command mode. The In[] prompt before cell disappears. Header cell. A markdown cell can display header text of 6 sizes, similar to HTML headers.

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.

Allow references to Python variables in Markdown cells · Issue , : Shift + J or Shift + Down selects the next sell in a downwards direction. By definition, if a user wants variables from the kernel in markdown cell, that markdown cell will depend on the kernel. Saying "that breaks the semantics of the notebok" is like saying we shouldn't allow code cells because they run on the kernel and return output.

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!

28 Jupyter Notebook Tips, Tricks, and Shortcuts for Data Science, How do I add text to a cell in Jupyter notebook? iPython notebooks are often used for tinkering with new problems. There's nothing wrong with using global variables in this case. This isn't about writing polished code, but rather about why the specific case given by @user1940979 doesn't work. – Thucydides411 Aug 10 '15 at 18:07

Using Jupyter notebook for writing, If the markdown cell is run in the jupyter notebook, it will say "my_var just put the variable in a string in python and use IPython.core.display to  Usually, one can access from the current notebook cell variables from previous cells (for instance, we define a in cell 2 and access it in 3). However, I can not get that to work when using heat (example in cell 4), which is sometimes annoying (especially when creating such variable can take up some time). Full traceback (click to expand)

Jupyter notebook asterisk???, The Python Markdown extension allows displaying output produced by the current to the kernel language, however most testing has been done using Python. You can check if the notebook is trusted by looking at the check mark at the top of in the metadata of the markdown cell, i.e. cell.metadata.variables[​varname] . Every cell starts off being a code cell, but its type can be changed by using a dropdown on the toolbar (which will be “Code”, initially), or via keyboard shortcuts. For more information on the different things you can do in a notebook, see the collection of examples .

Variable names in double curly braces don't evaluate in HTML , 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? You can use ? at the end of nearly any command in IPython if you want more information on the command (or method, variable, etc.). Pasting Blocks of Code; Last, but certainly not least, IPython is excellent for pasting large amounts of Python code.

Comments
  • Do you know if this also works if I use a non-Python kernel (e.g. R or MATLAB)?
  • @VerenaHaunschmid I never tried. But you might ask this question in the github issues of this package.
  • The extension needed for python variables being rendered inside markdown cells is called Python Markdown. For enabling it, once Jupyter notebook extensions have been installed, it's recommendable to also install jupyter_nbextensions_configurator, and then check it in the configuration frontend (i.e. localhost:8888/nbextensions).
  • This answer could be improved. State what the nbextension is called. The second link is also dead.