Skip to content

Introducing an updating text label causes inadvertent plotly figure reset #4186

Open
@duane-space

Description

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

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions