Skip to content

Commit

Permalink
Improve the update mechanism for ui.plotly (#4317)
Browse files Browse the repository at this point in the history
This PR tries to solve #4186 by calling the `update()` method in
JavaScript explicitly rather than within the `updated` hook. It seems to
resolve the problem described with the following code snippet:
```py
ui.label('1. Manually zoom the plot')
ui.button('2. Update the label', on_click=lambda: label.set_text(label.text + '.'))
ui.button('3. Update the plot', on_click=lambda: plot.update())
ui.label('4. --> The plot resets unexpectedly')

label = ui.label('...')
plot = ui.plotly({'layout': {'uirevision': 'constant'}})
```

Tests are passing and demos seem to work like before.
  • Loading branch information
falkoschindler authored Feb 7, 2025
1 parent 5b42401 commit 509faa9
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 3 deletions.
1 change: 1 addition & 0 deletions nicegui/elements/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def update_figure(self, figure: Union[Dict, go.Figure]):
def update(self) -> None:
self._props['options'] = self._get_figure_json()
super().update()
self.run_method('update')

def _get_figure_json(self) -> Dict:
if isinstance(self.figure, go.Figure):
Expand Down
3 changes: 0 additions & 3 deletions nicegui/elements/plotly.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ export default {
await import("plotly");
this.update();
},
updated() {
this.update();
},
methods: {
update() {
// wait for plotly to be loaded
Expand Down

0 comments on commit 509faa9

Please sign in to comment.