Python 如何在 Jupyter 笔记本中以编程方式生成降价输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36288670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:41:38  来源:igfitidea点击:

How to programmatically generate markdown output in Jupyter notebooks?

pythonipythonjupyterjupyter-notebook

提问by fulaphex

I want to write a report for classes in Jupyter notebook. I'd like to count some stuff, generate some results and include them in markdown. Can I set the output of the cell to be interpreted as markdown?
I'd like such command: print '$\phi$'to generate phi symbol, just like in markdown.
In other words, I'd like to have a template made in markdown and insert the values generated by the program written in the notebook. Recalculating the notebook should generate new results and new markdown with those new values inserted. Is that possible with this software, or do I need to replace the values by myself?

我想为 Jupyter notebook 中的课程写一份报告。我想计算一些东西,生成一些结果并将它们包含在降价中。我可以将单元格的输出设置为降价吗?
我想要这样的命令:print '$\phi$'生成 phi 符号,就像在降价中一样。
换句话说,我想要一个用 Markdown 制作的模板,并插入笔记本中编写的程序生成的值。重新计算笔记本应该会生成新的结果和插入这些新值的新降价。这个软件可以做到这一点,还是我需要自己替换这些值?

回答by Thomas K

The functions you want are in the IPython.display module.

您想要的功能在IPython.display 模块中

from IPython.display import display, Markdown, Latex
display(Markdown('*some markdown* $\phi$'))
# If you particularly want to display maths, this is more direct:
display(Latex('\phi'))

回答by Honeybear

You are basically asking for two different things:

您基本上要求两件不同的事情:

  1. Markdown cells outputting code results.

    I'd like to count some stuff, generate some results and include them in markdown. [...] I'd like to have a template in markdown and insert valuesgenerated by the program in the notebook

  2. Code cells outputting markdown

    I'd like such command: print '$\phi$'to generate phi symbol, just like in markdown.

  1. Markdown 单元格输出代码结果。

    我想计算一些东西,生成一些结果并将它们包含在降价中。[...] 我想在降价中一个模板,并在笔记本中插入程序生成的

  2. 输出降价的代码单元格

    我想要这样的命令:print '$\phi$'生成 phi 符号,就像在降价中一样。

Since 2. is already covered by another answer(basically: use Latex()or Markdown()imported from IPython.display), I will focus on the first one:

由于 2. 已经包含在另一个答案中(基本上:使用Latex()Markdown()从 导入IPython.display),我将专注于第一个:



1. Markdown Template with inserted variables

1. 插入变量的 Markdown 模板

With the Jupyter extension Python Markdownit actually is possible to do exactly what you describe.

使用 Jupyter 扩展Python Markdown,实际上可以完全按照您的描述进行操作。

Installation instructionscan be found on the github page of nbextensions. Make sure you'll enable the python markdown extension using a jupyter commandor the extension configurator.

安装说明可以在 nbextensions 的 github 页面上找到。确保您将使用jupyter 命令扩展 configurator启用 python markdown 扩展。

With the extension, variables are accessed via {{var-name}}. An example for such a markdown template could look like this:

使用扩展名,变量可以通过{{var-name}}. 这种降价模板的示例可能如下所示:

Python Code in Markdown Cells

The variable a is {{a}}

You can also embed LateX: {{b}} in here!

Even images can be embedded: {{i}}

Markdown 单元格中的 Python 代码

变量 a 是 {{a}}

您还可以在此处嵌入 LateX:{{b}}!

甚至可以嵌入图像:{{i}}

Naturally all variables or images a, b, ishould be set in previous code. And of course you may also make use of Markdown-Latex-style expressions (like $\phi$) without the print command. This image is from the wiki of the extension, demonstrating the capability.

当然,所有的变量或图像abi应该在前面的代码中设置。当然,您也可以在$\phi$没有打印命令的情况下使用 Markdown-Latex 风格的表达式(如)。此图片来自扩展的 wiki,展示了该功能。

example from wiki

来自维基的例子



Further info on this functionality being integrated into ipython/jupyter is discussed in the issue trackers for ipythonand jupyter.

有关集成到 ipython/jupyter 中的此功能的更多信息,请参阅 ipythonjupyter的问题跟踪器。