[Python] NiceGUI won't update Plotly trace on Scattermap fig

Discussão em 'Python' iniciado por Stack, Julho 5, 2025.

  1. Stack

    Stack Membro Participativo

    NiceGUI won't update Plotly trace Scattermap when I utilize a function, almost exactly like the documentation update sample here: https://nicegui.io/documentation/plotly#plot_updates Code runs fine without a function:

    import plotly.graph_objects as go
    import dataretrieval.nwis as nwis
    from nicegui import ui

    fig = go.Figure(go.Scattermap(
    fill = "toself",
    lon = [-90,-89,-89,-90],
    lat = [45,45,44,44],
    marker = { 'size': 10, 'color': "orange" },
    name='BBox'))

    fig.update_layout(margin=dict(l=0,r=0,t=0,b=0), width=400, showlegend=False,
    map = {
    'style': 'carto-darkmatter',
    'center': {'lon': -90, 'lat': 44 },
    'zoom':5})

    plot = ui.plotly(fig)

    # def update_plt():

    siteBBOX, md = nwis.get_info(bBox=[-90,44,-89,45])

    select_y = siteBBOX['dec_lat_va']

    select_x = siteBBOX['dec_long_va']
    names = siteBBOX['station_nm']

    fig.add_trace(go.Scattermap(lon=select_x,
    lat=select_y,
    fill=None,
    mode= 'markers',
    marker=dict(
    size=15,
    color='blue'
    ), text=names, name='sites'))

    plot.update()

    # ui.button('Update', on_click=update_plt)

    ui.run(title='Test')


    But when I uncomment and indent to make a function, it does not update to add the new points.

    import plotly.graph_objects as go
    import dataretrieval.nwis as nwis
    from nicegui import ui

    fig = go.Figure(go.Scattermap(
    fill = "toself",
    lon = [-90,-89,-89,-90],
    lat = [45,45,44,44],
    marker = { 'size': 10, 'color': "orange" },
    name='BBox'))

    fig.update_layout(margin=dict(l=0,r=0,t=0,b=0), width=400, showlegend=False,
    map = {
    'style': 'carto-darkmatter',
    'center': {'lon': -90, 'lat': 44 },
    'zoom':5})

    plot = ui.plotly(fig)

    def update_plt():

    siteBBOX, md = nwis.get_info(bBox=[-90,44,-89,45])

    select_y = siteBBOX['dec_lat_va']

    select_x = siteBBOX['dec_long_va']
    names = siteBBOX['station_nm']

    fig.add_trace(go.Scattermap(lon=select_x,
    lat=select_y,
    fill=None,
    mode= 'markers',
    marker=dict(
    size=15,
    color='blue'
    ), text=names, name='sites'))

    plot.update()

    ui.button('Update', on_click=update_plt)

    ui.run(title='Test')


    I'm not sure if I'm understanding what is different about my function and the NiceGUI example function. Thanks.

    Continue reading...

Compartilhe esta Página