简体   繁体   English

如何在 Jupyter 笔记本中以编程方式生成 markdown output?

[英]How to programmatically generate markdown output in Jupyter notebooks?

I want to write a report for classes in Jupyter notebook.我想为 Jupyter notebook 中的课程写一份报告。 I'd like to count some stuff, generate some results and include them in markdown.我想计算一些东西,生成一些结果并将它们包含在 markdown 中。 Can I set the output of the cell to be interpreted as markdown?我可以将单元格的 output 设置为 markdown 吗?
I'd like such command: print '$\phi$' to generate phi symbol, just like in markdown.我想要这样的命令: print '$\phi$'来生成 phi 符号,就像在 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.换句话说,我想在 markdown 中制作一个模板,并插入笔记本中编写的程序生成的值。 Recalculating the notebook should generate new results and new markdown with those new values inserted.重新计算笔记本应该会生成新的结果和新的 markdown 并插入这些新值。 Is that possible with this software, or do I need to replace the values by myself?这个软件可以做到吗,还是我需要自己替换这些值?

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

You are basically asking for two different things: 你基本上要求两件事:

  1. Markdown cells outputting code results. Markdown单元输出代码结果。

    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 values generated by the program in the notebook [...]我想在markdown中一个模板,并在笔记本中插入程序生成的

  2. Code cells outputting markdown 代码单元输出降价

    I'd like such command: print '$\\phi$' to generate phi symbol, just like in markdown. 我想要这样的命令: print '$\\phi$'来生成phi符号,就像在markdown中一样。

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.已经被另一个答案覆盖(基本上:使用从IPython.display导入的Latex()Markdown() ),我将重点关注第一个:


1. Markdown Template with inserted variables 1.带有插入变量的Markdown模板

With the Jupyter extension Python Markdown it actually is possible to do exactly what you describe. 使用Jupyter扩展Python Markdown,它实际上可以完全按照您的描述进行操作。

Installation instructions can be found on the github page of nbextensions. 安装说明可以在nbextensions的github页面上找到。 Make sure you'll enable the python markdown extension using a jupyter command or the extension configurator . 确保使用jupyter命令扩展配置程序启用python markdown扩展。

With the extension, variables are accessed via {{var-name}} . 通过扩展,可以通过{{var-name}}访问变量。 An example for such a markdown template could look like this: 这种降价模板的示例可能如下所示:

Python Code in Markdown Cells Markdown单元格中的Python代码

The variable a is {{a}} 变量a是{{a}}

You can also embed LateX: {{b}} in here! 你也可以在这里嵌入LateX:{{b}}!

Even images can be embedded: {{i}} 甚至可以嵌入图像:{{i}}

Naturally all variables or images a , b , i should be set in previous code. 当然,所有变量或图像abi都应该在之前的代码中设置。 And of course you may also make use of Markdown-Latex-style expressions (like $\\phi$ ) without the print command. 当然,您也可以在没有print命令的情况下使用Markdown-Latex风格的表达式(如$\\phi$ )。 This image is from the wiki of the extension, demonstrating the capability. 此图像来自扩展的维基,展示了该功能。

来自wiki的例子


Further info on this functionality being integrated into ipython/jupyter is discussed in the issue trackers for ipython and jupyter . ipythonjupyter的问题跟踪器中讨论了有关集成到ipython / jupyter中的此功能的更多信息。

As an addition to Thomas's answer.作为托马斯答案的补充。 Another easier way to render markdown markup is to use display_markdown function from IPython.display module:呈现 markdown 标记的另一种更简单的方法是使用来自IPython.display模块的display_markdown function:

from IPython.display import display_markdown

display_markdown('''## heading
- ordered
- list

The table below:
| id |value|
|:---|----:|
| a  |  1  |
| b  |  2  |
''', raw=True)

Output below: Output 如下:

在此处输入图像描述

Usage example could be found on Google Colab Notebook使用示例可以在Google Colab Notebook上找到

Another option is to use Rich for Markdown rendering and UnicodeIt for symbols.另一种选择是使用Rich进行 Markdown 渲染,使用UnicodeIt进行符号。 It has some limitations, as Rich uses CommonMark, which does not support tables, for example.它有一些限制,例如 Rich 使用不支持表格的 CommonMark。 Rich has other ways to render tables though; Rich 还有其他渲染表格的方法; this is detailed in the documentation.这在文档中有详细说明。

Here is an example:这是一个例子:

from rich.markdown import Markdown
import unicodeit

alpha = unicodeit.replace('\\alpha')
epsilon = unicodeit.replace('\\epsilon')
phi = unicodeit.replace('\\phi')

MARKDOWN = f"""
# This is an h1

Rich can do a pretty *decent* job of rendering markdown.

1. This is a list item
2. This is another list item

## This is an h2

List of **symbols**:

- alpha: {alpha}
- epsilon: {epsilon}
- phi: {phi}

This is a `code` snippet:

```py
# Hello world
print('Hello world')
```

This is a blockquote:

> Rich uses [CommonMark](https://commonmark.org/) to parse Markdown.

---

### This is an h3

See [Rich](https://github.com/Textualize/rich) and [UnicodeIt](https://github.com/svenkreiss/unicodeit) for more information.
"""

Markdown(MARKDOWN)

... which produces the following output: ...产生以下 output:

Rich + UnicodeIt 输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM