[Python] How do I display DataFrame attribute in NiceGUI with click on Plotly Map?

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

  1. Stack

    Stack Membro Participativo

    I've been able to understand how to display long/lat from a map, as well as x/y on a plot, on a label in NiceGUI with a click on a Plotly map with the help of this forum, but now I would like to display attributes from a dataframe object that contains multiple attributes. Sample code is below, dataframe is made up for simplicity.

    import pandas as pd
    import plotly.graph_objects as go
    from nicegui import ui, events

    df = pd.DataFrame([
    [12345, -95, 45, 'Cross River'],
    [12346, -94, 43, 'Snake River'],
    [12347, -92, 45, 'Temple River'],
    [12348, -96, 46, 'Gold River'],
    [12349, -95.5, 44.5, '#FT Ty7']],
    columns=['site', 'long', 'lat', 'name']
    )

    fig = go.Figure(go.Scattermap(
    mode='markers',
    lon=df['long'],
    lat=df['lat'],
    )
    )
    fig.update_layout(margin=dict(l=0,r=0,t=0,b=0), width=400, showlegend=False,
    map = {
    'style': 'satellite-streets',
    'center': {'lon': sum(df['long']/len(df)), 'lat': sum(df['lat']/len(df)) },
    'zoom':5})
    plot = ui.plotly(fig)

    label = ui.label("Click on a point for more information.")

    def handle_click(e: events.GenericEventArguments):
    index = e.args
    label.text = f'Name: {name}, Site number: {site}.'

    plot.on('plotly_click', handle_click)

    ui.run()


    This code doesn't work to display the attribute "name" from the df, and I would like to include all the attributes in a label as well.

    Continue reading...

Compartilhe esta Página