使用Dash更新pandas数据框

0 投票
1 回答
30 浏览
提问于 2025-04-14 18:14

你好,我想知道是否可以开发一个仪表盘,让一些输入(比如复选框、单选按钮等)能够更新后台的pandas数据框。简单来说,我想做一个数据标记工具。例如,仪表盘上会显示一张图片,你可以通过复选框选择几个类别,然后点击提交按钮,这样就会把选择的内容填入一个pandas数据框,最后可以保存成一个csv文件。

app.layout = html.Div([

    # Dropdown selection that will be used to select some image
    html.Div([
        dcc.Dropdown(id = "image-folders", options = [{"label" : i, "value" : i} for i in folders, value = folders[0]]
    ]),
    
    # Graph goes here ...
    html.Div([
        dcc.Graph(id = "image-graph", figure :{}) # Returns a plotly graph through a callback
    ])

    # Checkboxes go here - Selected values should be updated in the dataframe
    html.Div([
        dcc.Checklist(id = "labels-checklist", options = [{"label" : "Agriculture" , "value" : "Agriculture"}, {"label" : "Residential", "value" : "Residential"}])
    ])
    
    # Submission button goes here - Click submit to append selected values from checklist to a dataframe and then move on to the next image using the drop down and do the same
    html.Button(id = "submission-button", n_clicks = 0, children = "Submit")
])

# Callback & function to update figure
@callback(Output("image-graph","figure"), Input("image-folders", "value))

def update_figure(img_folder):
    file_path = os.path.join(root, img_folder)
    img = skimage.io.imread(file_path)
    fig = px.imshow(img)
    return fig

现在我基本上需要写另一个回调函数,从“标签检查列表”中获取值,并将其添加到数据框中。这些内容不一定需要在仪表盘上显示,但我在一些地方读到回调函数需要同时有输入和输出。所以如果它出现在某个dash表格里也没问题,但在我这个情况下其实并不是必须的。有没有什么想法可以做到这一点?

# Ideally no Output
@callback([Input("label-checklist","value"), Inputs("submission-button")])

def populate_dataframe(nclicks, checklist_value):
    # need help what goes here and what output I have to give
   

1 个回答

1

我觉得你应该先创建一个空的数据框,然后把你的清单值添加到里面。下面的代码可能会有效:

import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_table
import pandas as pd
import dash_html_components as html
from dash.dependencies import Input, Output, State

app = dash.Dash(__name__)

form = pd.DataFrame({'A':[]})

app.layout = html.Div([
    html.Div([
        dcc.Checklist(id = "labels-checklist",
                      options = [{"label" : "Agriculture" , "value" : "Agriculture"}, 
                                 {"label" : "Residential", "value" : "Residential"}])
    ]),
    
    # Submission button goes here - Click submit to append selected values from checklist to a dataframe and then move on to the next image using the drop down and do the same
    html.Button(id = "submission-button", n_clicks = 0, children = "Submit"),
    dash_table.DataTable(id='table', data=[], columns=[{"name":i, "id":i} for i in form.columns])
])

@app.callback(Output('table', 'data'),
             [Input('submission-button', 'n_clicks')],
             [State('table', 'data'),
              State('table', 'columns')] + [State('labels-checklist', 'value')])

def add_row(n_clicks, rows, columns, labels):
    if n_clicks >0:
        rows.append({c['id']: r for c, r in zip(columns, labels)})
    return rows

def update_data(labels):
    data = pd.DataFrame({'A':[labels]})
    return data.to_dict(orient="records")

if __name__ == "__main__":
    app.run_server(debug=False)

在这里输入图片描述

撰写回答