Transform text string to properly display new line and spaces in dcc.Markdown

Hi,

I have a text string with well defined new lines and spaces but I don’t know how to properly display such text in markdown.

Example:

text = "This markdown\n    should look \n        like this"
print(text)
This markdown
    should look 
        like this

How’d I transform such text to proper markdown to be input in:

dcc.Markdown(text).

Right now it looks like this in markdown:

This markdown should look like this

So it’s basically eliminating spaces and new lines. Generally I want a function that transforms text string to markdown and keeps spaces and new lines formatting. I know I can fix this by just writing text differently, i.e.,

text = “”"
This Markdown

    should look

        like this
“”"

But this is manual and cannot scale for a large text that’s already formatted with /n and spaces.

I’d ideally want a function that takes any input text string and transform it into markdown format.

“proper markdown” is notoriously hard to pin down :slight_smile: try:

dcc.Markdown(
    "This markdown\n    should look \n        like this",
    style={"white-space": "pre"}
)
3 Likes

Thanks! this works with one not-so-minor caveat, if the text is too long it get’s cut instead of putting a side scroller.

Then you can add:
"overflow-x": "scroll"

1 Like