【问题标题】:Print Variable In Jupyter Notebook Markdown Cell Python在 Jupyter Notebook Markdown Cell Python 中打印变量
【发布时间】:2018-10-15 08:05:00
【问题描述】:

我可以在 Markdown Cell Jupyter Notebook 中打印变量的值吗?

尝试过的代码:

value = 5.3

Markdown cell --> Value is {{ value }} 

我希望 Markdown 单元格显示变量的值

屏幕截图

【问题讨论】:

标签: python jupyter-notebook


【解决方案1】:

@nilansh bansal's 答案非常适合 Jupyter Notebooks。不幸的是,它不适用于 JupyterLab,因为不再支持该插件(所有 nbextension 插件都是如此)。由于 JupyterLab 越来越受欢迎,我想补充到目前为止的答案,因为我花了很长时间才找到解决方案。这是因为到目前为止还没有与 JupyterLab 兼容的插件。我通过结合thisthis SO 答案为自己找到了以下解决方案:

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))

或者,最后一行可以按照@Igor Fobia for Python >3.6 的建议进行简化:

md(f"The data consists of {n} observations. Bla, Bla, ....")

这会导致所需的输出。但是,它有一个巨大的缺点,即在导出 NB 时代码单元仍然可见。不过这可以解决:

  1. 在代码单元格中添加标签,即将其命名为“隐藏”
  2. 配置 nbconvert 以忽略标记的单元格,例如通过将此 c.TagRemovePreprocessor.remove_input_tags = {"hide"} 添加到您的 ~/.jupyter/jupyter_notebook_config.py 配置文件中

我已经写了一份详细的blog-post,讲述了我是如何在我的博客上实现这个用于发布笔记本的解决方案的。如果您使用 JupyterLab < 2.0,您可以为 JupyterLab 安装 jupyterlab-celltags 插件以简化单元格标记。

【讨论】:

  • f 字符串表示法也可以:md(f"The data consists of {n} observations. Bla, Bla, ....")
  • 这是最优雅的解决方案。
  • 非常感谢
【解决方案2】:

所以在浏览完所有链接后,我可以通过参考 nbextension jupyter notebook 文档来解决问题:https://github.com/ipython-contrib/jupyter_contrib_nbextensions

采取的步骤:

  1. pip install jupyter_contrib_nbextensions
  2. jupyter contrib nbextension install --user
  3. jupyter nbextension 启用 python-markdown/main

在上面的命令启动一个 jupyter notebook 并在 markdown 单元格中打印一个变量的值之后就像魅力一样!

您只需在降价单元格中使用 {{ ac_score }}

截图

谢谢!

【讨论】:

  • 任何想法,我们如何在 JupyterLab 中实现相同的目标?
  • 我还必须通过回答 here 让我的笔记本受信任启用扩展
【解决方案3】:

您可以覆盖%%markdown IPython 魔术来替换全局环境中的变量:

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 一起使用时允许 Markdown linting 的优点。

如果使用 nbsphinx 开发文档,您可以通过在单元格元数据中设置 {"hide_input": true} 来隐藏输入(单元格的来源):

(注意 Jupyter[Lab]LSP 下划线,因为它们不在英语词典中 - linting 有效!)

这会产生像这样的平滑文档:

为了在 JupyterLab 中获得更好的体验,您始终可以通过单击左侧的蓝色条折叠输入单元格(将鼠标悬停在单元格上时可见)。

【讨论】:

    【解决方案4】:
    {{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)
    

    【讨论】:

      猜你喜欢
      • 2019-06-14
      • 2023-01-03
      • 2018-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-06
      • 1970-01-01
      • 2019-02-27
      相关资源
      最近更新 更多