Adding Markdown content to a Rich Text Box at Run-Time

Hello Friends:

I generated this simple Markdown string like so:

      x = random.randint(1,50)
      y = random.randint(1,50)
      z = x + y
      
      markdown = f"""
      Let: **x** = {x}
      Let: **y** = {y}
      Let: **z** = **x** + **y**
      
      What is the value of **z**?
      """

Then, at run-time when I apply it as the Content of a Rich Text box, like so:

   self.rich_text_exercise.content = markdown       # -OR-
   self.rich_text_exercise.content = str(markdown)  # Just to try.

the text and spacing render fine, but the double asterisks (for bold) aren’t respected – they are shown literally.

Note: When the identical Markdown text is manually pasted into the content property of the Rich Text Box component, it renders perfectly.

Can friends provide guidance? Thank you in advance for helping.

Can you please check if the format of RichText component is markdown?

2 Likes

Hello. Yes, I double-checked. It’s Markdown and not Plain Text or Restricted HTML.

It’s because of the indentation. Adding more than (I think) 3 space at the beginning of a markdown row, makes it a code formatting and ignores bold, italic and other formatting.

You should do this:

      x = random.randint(1,50)
      y = random.randint(1,50)
      z = x + y
      
      markdown = f"""
Let: **x** = {x}
Let: **y** = {y}
Let: **z** = **x** + **y**

What is the value of **z**?
"""

or this:

        markdown = (
            f'Let: **x** = {x}\n'
            f'Let: **y** = {y}\n'
            'Let: **z** = **x** + **y**\n'
            '\n'
            'What is the value of **z**?'
        )

5 Likes

Ah yes, that makes sense. I should have thought of that from tons of Markdown I’ve written in Jupyter Notebooks; but as this is a brand new platform for me, it didn’t cross my mind. I was too concerned with if I was using these widgets properly. Yes, three (3) spaces will change Markdown text behavior.

I’ll try it tomorrow morning. It’s quite late here (Eastern Time). Thank you for the reply. :slight_smile:

1 Like