4

For some reason, I want to my HTML widget to have fixed height, no matter how many lines there are in the widget. If the lines are too many to fit into the height, ideally one can scroll to see all the lines. I tried something like the following, but it does not work:

import ipywidgets as widgets
widgets.HTML(
    value="Hello <p>World</p><p>World</p><p>World</p><p>World</p><p>World</p><p>World</p><p>World</p><p>World</p>",
    placeholder='Some HTML',
    description='Some HTML',
    disabled=True,
    height='50px',
    overflow_y='scroll'
)
RNA
  • 146,987
  • 15
  • 52
  • 70

1 Answers1

7

The follwoing code solves the problem:

import ipywidgets as widgets
from ipywidgets import Button, Layout
b=widgets.HTML(
    value="Hello <p>World</p><p>World</p><p>World</p><p>World</p><p>World</p><p>World</p><p>World</p><p>World</p>",
    placeholder='Some HTML',
    description='Some HTML',
    disabled=True
)
a = HBox([b], layout=Layout(height='50px', overflow_y='auto'))
display(a)

Interestingly, this does not work:

a = HBox([b], height='50px', overflow_y='auto')

It seems HBox does not pass overflow_y to the html/css, for some reason I don't understand.

RNA
  • 146,987
  • 15
  • 52
  • 70