Jupyter 笔记本的 Markdown 和围栏代码块

5

我正在尝试这段Markdown文本:

Hello, this should just be a paragraph, and there should be a test of Markdown fenced code block with backticks below:

```
Hello there
This should be in
a code block
```

在Jupyter Notebook的markdown单元格中,实现以下效果: jupyter notebook markdown cell code 并且渲染正确: jupyter notebook markdown cell rendered
现在我想做同样的事情,但是我希望将源代码存储在Python字符串变量中,然后使用IPython.display.Markdown()进行转换。因此,我有以下代码:
from IPython.display import display
from IPython.display import Markdown as md, Javascript, HTML

mdstr="""
Hello, this should just be a paragraph, and there should be a test of Markdown fenced code block with backticks:

```
Hello there
This should be in
a code block
```
"""

display(md(mdstr))

然而,这将呈现如下:

jupyter notebook python cell markdown

从屏幕截图中可以看出,在开头段落和代码块之间现在缺少一个垂直空间。

那么,我该如何使用 IPython.display.Markdown 获得与在 Jupyter 笔记本的 Markdown 单元格中只有 Markdown 文本时相同的输出(即段落和代码块之间有垂直空格)?

或者 - 如果不能使用 IPython.display.Markdown,我还有哪些其他选项,可以定义 Python 字符串中的 Markdown 文本,并生成与具有相同输入的 Jupyter Markdown 单元格完全相同的输出字符串?

1个回答

1

如果在代码块的第一行留出一个空行,那么显示效果将符合预期。

mdstr="""
Hello, this should just be a paragraph, and there should be a test of Markdown fenced code block with backticks:

```

Hello there
This should be in
a code block
```
"""

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接