Print Variable In Jupyter Notebook Markdown Cell P

2020-02-01 07:22发布

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

3条回答
Melony?
2楼-- · 2020-02-01 07:44

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!

查看更多
叛逆
3楼-- · 2020-02-01 07:44

@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. For example, you could install the jupyterlab-celltags plugin for JupyterLab to simplify the cell tagging.

查看更多
家丑人穷心不美
4楼-- · 2020-02-01 07:44
{{URL+target+'/'+CODE+'.png?sidcode='+str(sidecode)}} # Shows URL Properly, BUT!...

![__img_01__]({{URL+target+'/'+CODE+'.png?sidcode='+str(sidecode)}}) # why? not working?
![__img_02__]({{URL}}{{target}}/{{CODE}}.png?sidcode={{sidecode}}) # Why not working either?

So, I strongly recommend using Ipython.display.Markdown
Does anyone know a solution for above?

echo = f"""
## {NAME} ({CODE})
- target = '{target}'
- sidecode = '{sidecode}'
![__CHART_IMAGE__]({URL}{target}/{CODE}.png?sidcode={sidecode})
"""

Markdown(echo)
查看更多
登录 后发表回答