Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid setting a new default height in order to get consistent filling layout behavior #124

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions shinywidgets/_shinywidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,24 +312,27 @@ def set_layout_defaults(widget: Widget) -> Tuple[Widget, bool]:

layout = widget.layout # type: ignore

# Give the ipywidget Layout() width/height defaults that are more sensible for
# filling layout https://ipywidgets.readthedocs.io/en/stable/examples/Widget%20Layout.html
# If the ipywidget Layout() height is set to something other than "auto", then
# don't do filling layout https://ipywidgets.readthedocs.io/en/stable/examples/Widget%20Layout.html
if isinstance(layout, Layout):
if layout.width is None: # type: ignore
layout.width = "100%"
if layout.height is None: # type: ignore
layout.height = "400px"
else:
if layout.height != "auto": # type: ignore
if layout.height is not None and layout.height != "auto": # type: ignore
fill = False

pkg = widget_pkg(widget)

# Plotly provides it's own layout API (which isn't a subclass of ipywidgets.Layout)
if pkg == "plotly":
from plotly.graph_objs import Layout as PlotlyLayout
if isinstance(layout, PlotlyLayout):
if layout.height is not None:
fill = False

widget.layout = layout

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

Expand Down
12 changes: 7 additions & 5 deletions shinywidgets/static/shinywidgets.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
}

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

Expand Down