gpt4 book ai didi

python - 将输入从一个 Dash DataTable 输入到第二个 Dash DataTable

转载 作者:行者123 更新时间:2023-12-04 03:58:28 26 4
gpt4 key购买 nike

作为 Dash 的初学者,我有一个简单的问题。我正在使用一个表来接受用户输入,读入它,最终对其执行一个操作,然后在第一个下面返回一个结果表。下面是我的代码:

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

names = ['Player','Pos','Games','Rush Mkt Shr','Yds/Rush','Rush TD Rate','Tgt Mkt Shr','Yds/Rec','Rec TD Rate','Catch Rate','Int Rate','QB Snaps']
team_entries = pd.DataFrame(np.zeros((11,len(names))),columns = names)
team_entries['Pos']=['QB','RB','RB','RB','WR','WR','WR','WR','TE','TE','TEAM']
team_entries['Games'] = 16

app.layout = html.Div([
html.Div(id='div1', children=[dash_table.DataTable(
id='table-editing-simple',
columns=([{'id': p, 'name': p} for p in team_entries.columns]),
data=team_entries.to_dict('records'),
editable=True
)]),
html.Div(id = 'div2', children=[dash_table.DataTable(id='table-editing-simple-output')])
])


@app.callback(
Output('table-editing-simple-output', 'data'),
[Input('table-editing-simple', 'data'),
Input('table-editing-simple', 'columns')])
def display_output(rows, columns):
df = pd.DataFrame(rows, columns=[c['name'] for c in columns])
df['Games'] = df['Games']*2
return df.to_dict('records')


if __name__ == '__main__':
app.run_server(debug=True)
不幸的是,id 为“table-editing-simple-output”的第二个表没有打印,我不太确定为什么会这样。其次,如果我想为输出表使用不同的列名,我该怎么做?

最佳答案

试一试:
在您的回调函数中,不要返回数据,而是尝试用 Output('div2', 'children') 替换您的输出并返回一个全新的破折号数据表,如下所示:

@app.callback(
Output('div2', 'children'),
[Input('table-editing-simple', 'data'),
Input('table-editing-simple', 'columns')])
def display_output(rows, columns):
df = pd.DataFrame(rows, columns=[c['name'] for c in columns])
df['Games'] = df['Games']*2
return dash_table.DataTable(
id='table-output',
columns=columns,
data=rows,
)
这样,您也可以自由更改列名称!
工作代码如下:
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import dash_table
import os
from dash.dependencies import Input, Output
import numpy as np

names = ['Player','Pos','Games','Rush Mkt Shr','Yds/Rush','Rush TD Rate','Tgt Mkt Shr','Yds/Rec','Rec TD Rate','Catch Rate','Int Rate','QB Snaps']
team_entries = pd.DataFrame(np.zeros((11,len(names))),columns = names)
team_entries['Pos']=['QB','RB','RB','RB','WR','WR','WR','WR','TE','TE','TEAM']
team_entries['Games'] = 16
app = dash.Dash(__name__)
app.layout = html.Div([
html.Div(id='div1', children=[dash_table.DataTable(
id='table-editing-simple',
columns=([{'id': p, 'name': p} for p in team_entries.columns]),
data=team_entries.to_dict('records'),
editable=True
)]),
html.Div(id = 'div2', children=[dash_table.DataTable(id='table-editing-simple-output')])
])


@app.callback(
Output('div2', 'children'),
[Input('table-editing-simple', 'data'),
Input('table-editing-simple', 'columns')])
def display_output(rows, columns):
df = pd.DataFrame(rows, columns=[c['name'] for c in columns])
df['Games'] = df['Games']*2
return dash_table.DataTable(
id='table-output',
columns=columns,
data=rows,
)


if __name__ == '__main__':
app.run_server(debug=True)

关于python - 将输入从一个 Dash DataTable 输入到第二个 Dash DataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63479842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com