【发布时间】:2019-02-11 00:28:57
【问题描述】:
我最近一直在尝试使用Dash 构建一个应用程序,但是尽管浏览了许多指南,但我根本无法弄清楚如何将 pandas 数据帧导入 Dash 的数据表(这本质上是一个 pandas 数据帧,除了 web -托管和反应)。
大多数示例说明了如何从示例中已经硬编码的数据框中手动选择某些列/行,例如here。然而,在我的情况下,数据框是在我的代码中构建的(熊猫是最简单的方法),所以我最终不得不想办法将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)
【问题讨论】:
标签: python pandas plotly-dash