[Python] How do I get Plotly to work well with NiceGUI?

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

  1. Stack

    Stack Membro Participativo

    I know this is simple, and yet I can't figure it out. All I want to do is take an input list of number, bind it to a dictionary, and create a plot that refreshes when I click a button. I've been able to do this to some degree with the below code, but problems arise as the dictionary returns dictionary data NOT in a list for some reason.

    from nicegui import ui
    import plotly.graph_objects as go
    import numpy as np

    coords = {'x': [], 'y': []}
    ui.input(label='X',value=[1, 2, 3, 4]).bind_value_to(coords,'x')
    ui.input(label='Y',value=[1, 2, 3, 4]).bind_value_to(coords,'y')

    fig = go.Figure(go.Scatter(x=coords['x'], y=coords['y']))
    fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
    plot = ui.plotly(fig).classes('w-full h-40')

    def update_plt():
    fig.data = []
    x2 = coords['x']
    y2 = coords['y']
    fig.add_trace(go.Scatter(x=x2, y=y2))
    plot.update()
    print(fig.data)

    ui.button('Refresh', on_click=update_plt)
    ui.run()

    Continue reading...

Compartilhe esta Página