Skip to content

Figure resizing with AnyWidget doesn't work as expected #5208

Open
@ndrezn

Description

@ndrezn

See: https://community.plotly.com/t/plotly-v6-0-figure-resizing-with-ipywidgets/89424

import ipywidgets as widgets
import plotly.graph_objs as go
from ipywidgets import HTML, VBox

fig = go.FigureWidget(data=[go.Scatter(x=[1, 2, 3], y=[4, 5, 6])])

custom_css = """
<style>
.plot-container {
    width: 100%;
    height: 100%;
}
</style>
"""
fig.add_class("plot-container")

container = VBox(
    [fig],
    layout=widgets.Layout(
        width="500px",
        height="600px",
        border="solid red",
    ),
)

display(HTML(custom_css), container)

5.24
Image

6.0
Image


A follow-up:

fig.layout.autosize = True

works, but it is poorly implemented. For example if it is set before displaying fig, it many times does not respect this decision. So I usually set it after display. Still if container size changes, it is not going well on its own and you may need to reset it. Honestly, to me it looks like sending a message through this trait which upon setting it again to same value works, although a Bool trait does not work like that. Ideally we should set this once and javascript behind the scene should observe the container size change automatically.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2considered for next cyclebugsomething broken

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions