Output formatted text in widget

I’m starting out using widgets in my Jupyter notebooks. I have the need to output some text results in a widget. I will be bolding and highlighting particular parts of the output. HTML would be ideal.

I’ve seen some websites mention that the Output widget can display HTML, but I can’t find an example or explanation of how to do this.

Is there a widget I can fill with formatted text?

1 Like

https://ipywidgets.readthedocs.io/en/8.1.5/examples/Widget%20List.html#html

Then you can use an HBox or VBox widget to combine them with other widgets as an output.

import ipywidgets as widgets
output = widgets.Output()

HTML_text_widget = widgets.HTML(
    value='<b>Hell</b>o<br/>Bye<p>You need to pay attention to <mark>this text</mark> today.</p>'
)

with output:
    display(HTML_text_widget)

display(output)

RESULT:

Or if you aren’t needing the Output() widget that you mentioned, you’ll get the same result with simpler variation:

import ipywidgets as widgets
output = widgets.Output()

HTML_text_widget = widgets.HTML(
    value='<b>Hell</b>o<br/>Bye<p>You need to pay attention to <mark>this text</mark> today.</p>'
)

display(HTML_text_widget)

Some other related examples:

  • here
  • here
  • here, if rendered markdown is sufficient for others finding this thread
1 Like

Thanks for the replies! Definitely helps me out!

1 Like