Repository: zauberzeug/nicegui

Issue #4182: ui.leaflet location is reset when ui.plotly is updated
Created: 2025-01-07T09:19:00Z
Description
minimal reproducible code:
import plotly.graph_objects as go
from nicegui import ui
from random import random


@ui.page("/")
async def page():
    m = ui.leaflet(zoom=5)
    central_park = m.generic_layer(
        name="polygon",
        args=[
            [
                (40.767809, -73.981249),
                (40.800273, -73.958291),
                (40.797011, -73.949683),
                (40.764704, -73.973741),
            ]
        ],
    )
    await m.initialized()
    bounds = await central_park.run_method("getBounds")
    m.run_map_method("fitBounds", [[bounds["_southWest"], bounds["_northEast"]]])

    fig = go.Figure()
    fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
    plot = ui.plotly(fig).classes("w-full h-40")

    def add_trace():
        fig.add_trace(go.Scatter(x=[1, 2, 3], y=[random(), random(), random()]))
        plot.update()

    ui.button("Add trace", on_click=add_trace)


ui.run()
Once I click the button to update the graph, the leaflet map view was reset to the original state before running the map method to fit bounds.
I tested the code on windows platform.

Duplicate issue #4186: Introducing an updating text label causes inadvertent plotly figure reset
Created: 2025-01-08T02:00:09Z
Description
I am trying to create a page with dynamic text labels and a plotly timeline figure. I had all of the plotly functionality working as intended, prior to the introduction of a dynamically updating text label.
See the following attempt at a minimally reproducible example. If one changes the x scale zoom of the figure to not show all of the tasks, the plotly figure will automatically reset to once again show the full range. If the second ui.timer call (which only updates the text label) is commented out, this no longer happens and the user selected zoom persists
from nicegui import ui
from datetime import datetime, timedelta
import plotly.express as px
import pandas as pd

df = pd.DataFrame({
    "Task": ["A", "B", "C"],
    "Start": [datetime.now(), datetime.now() + timedelta(hours=1), datetime.now() + timedelta(hours=2)],
    "Finish": [datetime.now() + timedelta(hours=1), datetime.now() + timedelta(hours=2), datetime.now() + timedelta(hours=3)],
})

def initialize_figure():
    now = datetime.now()
    fig = px.timeline(
        df,
        x_start="Start",
        x_end="Finish",
        y="Task",
    )
    fig.add_shape(
        dict(
            type="line",
            x0=now,
            x1=now,
            yref="paper",
            y0=0,
            y1=1,
            line=dict(color="red", dash="dash"),
        )
    )
    fig.update_layout(uirevision="constant",       
                        xaxis=dict(
                        rangeslider=dict(visible=True),
                        type="date",
                    )
    )
    return fig

def update_now_line(fig, plot):
    now = datetime.now()
    for shape in fig.layout.shapes:
        if getattr(shape, 'type', None) == 'line':
            shape.x0 = now
            shape.x1 = now
    plot.update_figure(fig)

def update_time_label(label):
    label.text = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

time_label = ui.label(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
fig = initialize_figure()
plot = ui.plotly(fig)

ui.timer(5.0, lambda: update_now_line(fig, plot))
ui.timer(1.0, lambda: update_time_label(time_label))

ui.run()
I'm using nicegui 2.8.1. I scanned the release notes and didn't see any relevant updates. Is this behavior understood and/or expected? if so, is there a work around?
Thanks

Closing comment (2025-01-08T13:51:39Z): Thanks for reporting this issue, @duane-space!
It might indeed be related to #4182, but I'm not sure.
I just boiled it down to this:
plot = ui.plotly({'layout': {'uirevision': 'constant'}})
ui.button('Update plot', on_click=plot.update)

label = ui.label('...')
ui.button('Update label', on_click=lambda: label.set_text(label.text + '.'))
If you update the label before updating the plot, the layout option {'uirevision': 'constant'} seems to be ignored.
Maybe both problems, #4182 and this #4186, are caused by some rendering procedure in the browser, because an update to one element causes the window and, thus, the other elements to re-adjust.