ui.plotly auto resize #4256
-
QuestionHello, Perhaps my question is more on the styling side than the nicegui side, but I hope someone could help me. I'm using a ui.plotly in a splitter and I would like the figure to shrink when the splitter is moved. Here is a small code to show what i mean : from nicegui import ui
import plotly.graph_objects as go
with ui.splitter(value=15).classes('w-full') as splitter:
with splitter.before:
with ui.column().classes('w-full items-start'):
with ui.row():
ui.label('label')
ui.radio(['x', 'tps'], value='x')
with splitter.after:
with ui.row().classes('w-full h-full'):
fig = go.Figure(go.Scatter(x=[1, 2, 3, 4], y=[1, 2, 3, 2.5]))
fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
ui.plotly(fig).classes('w-full h-full')
ui.run() Is there any way to do what I want ? Thank by advance ! |
Beta Was this translation helpful? Give feedback.
Answered by
falkoschindler
Jan 24, 2025
Replies: 1 comment 7 replies
-
Hi @chupins, With a custom JavaScript call we can tell Plotly to resize: with ui.splitter(value=15).classes('w-full') as splitter:
with splitter.before:
ui.label('before')
with splitter.after:
plot = ui.plotly(go.Figure(go.Scatter(x=[1, 2, 3, 4], y=[1, 2, 3, 2.5]))).classes('w-full h-full')
splitter.on_value_change(lambda: ui.run_javascript(f'Plotly.Plots.resize(getHtmlElement({plot.id}))')) Optionally you can let the splitter emit change events while being dragged: splitter.props('emit-immediately') |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Because HTML doesn't like numbers as element IDs, NiceGUI adds a "c" (for "component") in front of it. The utility functions
getElement
andgetHtmlElement
can be used to access such elements by their integer ID. But the latter,getHtmlElement
, has just been added in NiceGUI 2.9.0, so you're probably using an older version.Alternatively you can use
getElement(...).$el
: