Jupyter笔记本中的打印变量标记单元格Python
我可以在Markdown Cell Jupyter笔记本中打印变量的值吗 试用代码:Jupyter笔记本中的打印变量标记单元格Python,python,jupyter-notebook,Python,Jupyter Notebook,我可以在Markdown Cell Jupyter笔记本中打印变量的值吗 试用代码: value = 5.3 Markdown cell --> Value is {{ value }} 我希望标记单元格显示变量的值 屏幕截图 因此,在浏览了所有链接后,我通过参考nbextension jupyter笔记本文档解决了问题: 采取的步骤: pip安装jupyter_contrib_nbextensions jupyter contrib nbextension安装--用户 jupyte
value = 5.3
Markdown cell --> Value is {{ value }}
我希望标记单元格显示变量的值
屏幕截图
因此,在浏览了所有链接后,我通过参考nbextension jupyter笔记本文档解决了问题: 采取的步骤:
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))
或者,最后一行可以按照for Python>3.6的建议进行简化:
md(f"The data consists of {n} observations. Bla, Bla, ....")
这将导致所需的输出。然而,它有一个巨大的缺点,即导出NB时代码单元仍然可见。这可以通过以下方式解决:
nbconvert
配置为忽略标记的单元格,例如,通过将此c.TagRemovePreprocessor.remove\u input\u tags={“hide”}
添加到~/.jupyter/jupyter\u notebook\u config.py
配置文件中jupyterlab celltags
插件,以简化单元格标记
{{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?
因此,我强烈建议使用Ipython.display.Markdown有人知道上述问题的解决方案吗
echo = f"""
## {NAME} ({CODE})
- target = '{target}'
- sidecode = '{sidecode}'
![__CHART_IMAGE__]({URL}{target}/{CODE}.png?sidcode={sidecode})
"""
Markdown(echo)
您可以覆盖
%%markdown
IPython magic以替换全局环境中的变量:
from IPython.display import Markdown
from IPython.core.magic import register_cell_magic
@register_cell_magic
def markdown(line, cell):
return Markdown(cell.format(**globals()))
当与JupyterLab LSP一起使用时,这样做的优点是可以降低价格
如果使用nbsphinx开发文档,可以通过在单元格元数据中设置{“hide_input”:true}
来隐藏输入(单元格源):
(注意带下划线的Jupyter[Lab]
和LSP
,因为它们不在英语词典中-linting有效!)
这将产生如下平滑文档:
为了在JupyterLab中获得更好的体验,您可以通过单击输入单元格左侧的蓝色条(鼠标悬停在单元格上方时可见)来折叠输入单元格。如果Jupyter/Python按照RStudio/R的方式运行,那么世界将是一个更好的地方。sign.f-string表示法也能起作用:
md(f“数据由{n}个观测值组成。Bla,Bla,…”)
这是最优雅的解决方案。你知道,我们如何在JupyterLab实现同样的效果吗?我还必须让我的笔记本可信,并通过答案启用扩展。