相关疑难解决方法(0)

从带有 Plotly Dash for Python 的回调中将 Pandas DataFrame 作为 data_table 返回

我想读取一个 .csv 文件并返回一个 groupby 函数作为回调,以显示为带有“dash_table”库的简单数据表。@Lawliet 的有用答案显示了如何使用“dash_table_experiments”库来做到这一点。这是我被困的地方:

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

df = pd.read_csv(
        'https://gist.githubusercontent.com/chriddyp/'
        'c78bf172206ce24f77d6363a2d754b59/raw/'
        'c353e8ef842413cae56ae3920b8fd78468aa4cb2/'
        'usa-agricultural-exports-2011.csv')

app = dash.Dash()
application = app.server

app.layout = html.Div([
    dash_table.DataTable(
        id = 'datatable',        
    ),

    html.Div([
        html.Button(id='submit-button',                
                children='Submit'
    )
    ]),    

])

@app.callback(Output('datatable','data'),
            [Input('submit-button','n_clicks')],
                [State('submit-button','n_clicks')])

def update_datatable(n_clicks,csv_file):            
    if n_clicks:                            
        dfgb = df.groupby(['state']).sum()
        return dfgb.to_dict('rows')

if __name__ == '__main__':
    application.run(debug=False, port=8080)
Run Code Online (Sandbox Code Playgroud)

python pandas plotly-dash

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

pandas ×1

plotly-dash ×1

python ×1