Python Dash:将 pandas 数据帧加载到数据 table
Python Dash: loading pandas dataframes into data table
我最近一直在尝试使用 Dash 构建一个应用程序,但是尽管查阅了很多指南,我还是无法弄清楚如何将 pandas 数据帧导入 Dash 的数据 table(本质上是一个 pandas 数据框,除了网络托管和反应式)。
大多数示例都说明了如何从示例中已经硬编码的数据框中手动选择某些 columns/rows,例如 here。但是,在我的情况下,数据框是在我的代码中构建的(pandas 是最简单的方法),所以我最终不得不想办法将 pd.Dataframe()
转换为dash_table.DataTable()
.
我怎样才能完成这项工作?使用参考资料,我尝试了以下代码将我的数据帧的字典发送到 dash_table.DataTable()
,但没有任何显示。
我的代码:
## Imports
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table
from dash.dependencies import Input, Output, State
import datetime as dt
import pandas as pd
import numpy as np
## Custom functions that creates the pandas dataframe
from twitter_functions import old_tweets
app = dash.Dash(dev_tools_hot_reload=True)
app.scripts.config.serve_locally = True
app.config['suppress_callback_exceptions'] = True
app.layout = html.Div(children=[
html.H3('Twitter App'),
dcc.Input('ScreenName_Input', type='text'),
html.Button(id='screenNames_submit_button', children='Submit'),
dash_table.DataTable(id='tweet_table')
])
@app.callback(
Output(component_id='tweet_table', component_property='data'),
[Input(component_id='screenNames_submit_button', component_property='n_clicks_timestamp')],
[State(component_id='ScreenName_Input', component_property='value')]
)
def display_tweets(submit_button, screen_names):
tweets = old_tweets(screen_names)
return tweets.to_dict(orient='records')
if __name__ == '__main__':
app.run_server(debug=True)
这有点远,并且未经测试,但基于 https://community.plot.ly/t/dash-datatable-using-callbacks/6756,如果您要通过回调修改 Dash DataTables,它似乎隐含地需要一个初始值。
尝试更改此行:
dash_table.DataTable(id='tweet_table')
为此:
dash_table.DataTable(id='tweet_table', rows=[{}])
在someone也在plotly论坛上回复了我(谢天谢地),看来最终的答案是用pandas的列预先设置一个人的数据Table将在某个时候进入其中的数据框,像这样,
dash_table.DataTable(
id='table',
columns=[
{'name': 'Column 1', 'id': 'column1'},
{'name': 'Column 2', 'id': 'column2'},
{'name': 'Column 3', 'id': 'column3'},
{'name': 'Column 4', 'id': 'column4'},
{'name': 'Column 5', 'id': 'column5'}]
)
,然后发送您的 pandas 数据框的字典。
这是另一个对我有用的解决方案:
dt_col_param = []
for col in output_df.columns:
dt_col_param.append({"name": str(col), "id": str(col)})
dash_table.DataTable(
columns=dt_col_param,
data=output_df.to_dict('records')
)
我最大的问题是我的应用程序一直抛出异常,无论我试图传递给 dash_table.DataTable(...) 的 'columns' 参数。
希望这将有助于您不必 hard-code 任何事情。
假设你的推文函数 returns 一个数据框,
添加 table 列作为回调的第二个输出应该可以。
@app.callback(
[Output(component_id='tweet_table', component_property='data'),
Output(component_id='tweet_table', component_property='columns')
[Input(component_id='screenNames_submit_button', component_property='n_clicks_timestamp')],
[State(component_id='ScreenName_Input', component_property='value')]
)
def display_tweets(submit_button, screen_names):
tweets = old_tweets(screen_names)
columns = [{'name': col, 'id': col} for col in tweets.columns]
data = tweets.to_dict(orient='records')
return data, columns
Was working on sample codes and this worked for me , this example might help u with your Query
import pandas as pd
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import base64
import io
app = dash.Dash()
# app.scripts.config.serve_locally = True
# app.css.config.serve_locally = True
DF_GAPMINDER = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv'
)
DF_GAPMINDER = DF_GAPMINDER[DF_GAPMINDER['year'] == 2007]
DF_GAPMINDER.loc[0:20]
DF_SIMPLE = pd.DataFrame({
'x': ['A', 'B', 'C', 'D', 'E', 'F'],
'y': [4, 3, 1, 2, 3, 6],
'z': ['a', 'b', 'c', 'a', 'b', 'c']
})
dataframes = {'DF_GAPMINDER': DF_GAPMINDER,
'DF_SIMPLE': DF_SIMPLE}
def get_data_object(user_selection):
"""
For user selections, return the relevant in-memory data frame.
"""
return dataframes[user_selection]
app.layout = html.Div([
html.H4('DataTable'),
html.Label('Report type:', style={'font-weight': 'bold'}),
dcc.Dropdown(
id='field-dropdown',
options=[{'label': df, 'value': df} for df in dataframes],
value='DF_GAPMINDER',
clearable=False
),
dash_table.DataTable(id='table')
])
@app.callback([Output(component_id='table', component_property='data'),
Output(component_id='table', component_property='columns')],
[Input('field-dropdown', 'value')])
def update_table(user_selection):
"""
For user selections, return the relevant table
"""
df = get_data_object(user_selection)
columns = [{'name': col, 'id': col} for col in df.columns]
data = df.to_dict(orient='records')
return data, columns
app.run_server()
我最近一直在尝试使用 Dash 构建一个应用程序,但是尽管查阅了很多指南,我还是无法弄清楚如何将 pandas 数据帧导入 Dash 的数据 table(本质上是一个 pandas 数据框,除了网络托管和反应式)。
大多数示例都说明了如何从示例中已经硬编码的数据框中手动选择某些 columns/rows,例如 here。但是,在我的情况下,数据框是在我的代码中构建的(pandas 是最简单的方法),所以我最终不得不想办法将 pd.Dataframe()
转换为dash_table.DataTable()
.
我怎样才能完成这项工作?使用参考资料,我尝试了以下代码将我的数据帧的字典发送到 dash_table.DataTable()
,但没有任何显示。
我的代码:
## Imports
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table
from dash.dependencies import Input, Output, State
import datetime as dt
import pandas as pd
import numpy as np
## Custom functions that creates the pandas dataframe
from twitter_functions import old_tweets
app = dash.Dash(dev_tools_hot_reload=True)
app.scripts.config.serve_locally = True
app.config['suppress_callback_exceptions'] = True
app.layout = html.Div(children=[
html.H3('Twitter App'),
dcc.Input('ScreenName_Input', type='text'),
html.Button(id='screenNames_submit_button', children='Submit'),
dash_table.DataTable(id='tweet_table')
])
@app.callback(
Output(component_id='tweet_table', component_property='data'),
[Input(component_id='screenNames_submit_button', component_property='n_clicks_timestamp')],
[State(component_id='ScreenName_Input', component_property='value')]
)
def display_tweets(submit_button, screen_names):
tweets = old_tweets(screen_names)
return tweets.to_dict(orient='records')
if __name__ == '__main__':
app.run_server(debug=True)
这有点远,并且未经测试,但基于 https://community.plot.ly/t/dash-datatable-using-callbacks/6756,如果您要通过回调修改 Dash DataTables,它似乎隐含地需要一个初始值。
尝试更改此行:
dash_table.DataTable(id='tweet_table')
为此:
dash_table.DataTable(id='tweet_table', rows=[{}])
在someone也在plotly论坛上回复了我(谢天谢地),看来最终的答案是用pandas的列预先设置一个人的数据Table将在某个时候进入其中的数据框,像这样,
dash_table.DataTable(
id='table',
columns=[
{'name': 'Column 1', 'id': 'column1'},
{'name': 'Column 2', 'id': 'column2'},
{'name': 'Column 3', 'id': 'column3'},
{'name': 'Column 4', 'id': 'column4'},
{'name': 'Column 5', 'id': 'column5'}]
)
,然后发送您的 pandas 数据框的字典。
这是另一个对我有用的解决方案:
dt_col_param = []
for col in output_df.columns:
dt_col_param.append({"name": str(col), "id": str(col)})
dash_table.DataTable(
columns=dt_col_param,
data=output_df.to_dict('records')
)
我最大的问题是我的应用程序一直抛出异常,无论我试图传递给 dash_table.DataTable(...) 的 'columns' 参数。
希望这将有助于您不必 hard-code 任何事情。
假设你的推文函数 returns 一个数据框, 添加 table 列作为回调的第二个输出应该可以。
@app.callback(
[Output(component_id='tweet_table', component_property='data'),
Output(component_id='tweet_table', component_property='columns')
[Input(component_id='screenNames_submit_button', component_property='n_clicks_timestamp')],
[State(component_id='ScreenName_Input', component_property='value')]
)
def display_tweets(submit_button, screen_names):
tweets = old_tweets(screen_names)
columns = [{'name': col, 'id': col} for col in tweets.columns]
data = tweets.to_dict(orient='records')
return data, columns
Was working on sample codes and this worked for me , this example might help u with your Query
import pandas as pd
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import base64
import io
app = dash.Dash()
# app.scripts.config.serve_locally = True
# app.css.config.serve_locally = True
DF_GAPMINDER = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv'
)
DF_GAPMINDER = DF_GAPMINDER[DF_GAPMINDER['year'] == 2007]
DF_GAPMINDER.loc[0:20]
DF_SIMPLE = pd.DataFrame({
'x': ['A', 'B', 'C', 'D', 'E', 'F'],
'y': [4, 3, 1, 2, 3, 6],
'z': ['a', 'b', 'c', 'a', 'b', 'c']
})
dataframes = {'DF_GAPMINDER': DF_GAPMINDER,
'DF_SIMPLE': DF_SIMPLE}
def get_data_object(user_selection):
"""
For user selections, return the relevant in-memory data frame.
"""
return dataframes[user_selection]
app.layout = html.Div([
html.H4('DataTable'),
html.Label('Report type:', style={'font-weight': 'bold'}),
dcc.Dropdown(
id='field-dropdown',
options=[{'label': df, 'value': df} for df in dataframes],
value='DF_GAPMINDER',
clearable=False
),
dash_table.DataTable(id='table')
])
@app.callback([Output(component_id='table', component_property='data'),
Output(component_id='table', component_property='columns')],
[Input('field-dropdown', 'value')])
def update_table(user_selection):
"""
For user selections, return the relevant table
"""
df = get_data_object(user_selection)
columns = [{'name': col, 'id': col} for col in df.columns]
data = df.to_dict(orient='records')
return data, columns
app.run_server()