sdbbs
sdbbs

Reputation: 5384

Jupyter notebook markdown and fenced code blocks

I'm trying this Markdown text:

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

... in a Jupyter notebook markdown cell:

jupyter notebook markdown cell code

... and this in fact renders fine:

jupyter notebook markdown cell rendered


Now I want to do the same, but I want to store the source text in a Python string variable, then use IPython.display.Markdown() to convert it; so I have this code:

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

... however, this renders like so:

jupyter notebook python cell markdown

... and as visible from the screenshot, there is now a missing vertical space between the opening paragraph, and the code block.

So how do I get the exact same output with IPython.display.Markdown, as I get from just having Markdown text in a Jupyter notebook markdown cell - with a vertical space between paragraph and code block?

Or - if not possible with IPython.display.Markdown, what other options do I have, to define Markdown text in a Python string, and then generate output exactly the same, as what a Jupyter Markdown cell would produce, with the same Markdown input string?

Upvotes: 5

Views: 2753

Answers (1)

rehaqds
rehaqds

Reputation: 2000

With an empty line as the first line of the code block, the display will look like what is expected.

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
```
"""

Upvotes: 1

Related Questions