Skip to content

Commit a19eba7

Browse files
authored
Avoid setting a new default height in order to get consistent filling layout behavior (#124)
1 parent 4eb86ea commit a19eba7

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

shinywidgets/_shinywidgets.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -312,24 +312,27 @@ def set_layout_defaults(widget: Widget) -> Tuple[Widget, bool]:
312312

313313
layout = widget.layout # type: ignore
314314

315-
# Give the ipywidget Layout() width/height defaults that are more sensible for
316-
# filling layout https://ipywidgets.readthedocs.io/en/stable/examples/Widget%20Layout.html
315+
# If the ipywidget Layout() height is set to something other than "auto", then
316+
# don't do filling layout https://ipywidgets.readthedocs.io/en/stable/examples/Widget%20Layout.html
317317
if isinstance(layout, Layout):
318-
if layout.width is None: # type: ignore
319-
layout.width = "100%"
320-
if layout.height is None: # type: ignore
321-
layout.height = "400px"
322-
else:
323-
if layout.height != "auto": # type: ignore
318+
if layout.height is not None and layout.height != "auto": # type: ignore
319+
fill = False
320+
321+
pkg = widget_pkg(widget)
322+
323+
# Plotly provides it's own layout API (which isn't a subclass of ipywidgets.Layout)
324+
if pkg == "plotly":
325+
from plotly.graph_objs import Layout as PlotlyLayout
326+
if isinstance(layout, PlotlyLayout):
327+
if layout.height is not None:
324328
fill = False
325329

326330
widget.layout = layout
327331

328-
# Some packages (e.g., altair) aren't setup to fill their parent container by
329-
# default. I can't imagine a situation where you'd actually want it to _not_ fill
330-
# the parent container since it'll be contained within the Layout() container, which
331-
# has a full-fledged sizing API.
332-
pkg = widget_pkg(widget)
332+
# altair, confusingly, isn't setup to fill it's Layout() container by default. I
333+
# can't imagine a situation where you'd actually want it to _not_ fill the parent
334+
# container since it'll be contained within the Layout() container, which has a
335+
# full-fledged sizing API.
333336
if pkg == "altair":
334337
from altair import JupyterChart
335338

shinywidgets/static/shinywidgets.css

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
}
88

99
/*
10-
For most/all ipywidgets, we can set these defaults via the Layout() API,
11-
but go.FigureWidget().layout points to plotly.js' layout, and I'm not
12-
sure how to get at the ipywidget's layout object from there.
10+
.html-fill-item defaults to `flex-basis: auto`, which works well when item's
11+
all have consistent height, but we can't assume that (because different
12+
widgets want different height defaults). By setting `flex-basis: 400px`,
13+
we let widgets keep its natural height (when not filling), but when filling,
14+
widgets will all grow/shrink to a consistent height.
1315
*/
14-
.plotly.plot-container {
15-
height: 400px;
16+
.shiny-ipywidget-output.html-fill-container > .lm-Widget.html-fill-item {
17+
flex-basis: 400px;
1618
width: 100%;
1719
}
1820

0 commit comments

Comments
 (0)