Can I use variables on an ipython notebook markup cell?

我与影子孤独终老i 提交于 2019-11-28 06:13:28

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.

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

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.

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!