我按照本指南和文档将 excel 文件上传到仪表板:https://dash.plot.ly/dash-core-components/upload
我想知道如何在熊猫数据框中显示上传结果。我的代码概述如下。从本质上讲,我的表是某些百分比的状态细分,我正在尝试将其上传到我的仪表板中。
import base64
import datetime
import io
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import pandas as pd
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
dcc.Upload(
id='upload-data',
children=html.Div([
'Drag and Drop or ',
html.A('Select Files')
]),
style={
'width': '100%',
'height': '120px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
# Allow multiple files to be uploaded
multiple=True
),
html.Div(id='output-data-upload'),
])
def parse_contents(contents, filename, date):
content_type, content_string = contents.split(',')
decoded = base64.b64decode(content_string)
try:
if 'csv' in filename:
# Assume that the user uploaded a CSV file
df = pd.read_csv(
io.StringIO(decoded.decode('utf-8')))
elif 'xls' in filename:
# Assume that the user uploaded an excel file
df = pd.read_excel(io.BytesIO(decoded))
except Exception as e:
print(e)
return html.Div([
'There was an error processing this file.'
])
def generate_table(df, max_rows=10):
return html.Table(
# Header
[html.Tr([html.Th(col) for col in df.columns])] +
# Body
[html.Tr([
html.Td(df.iloc[i][col]) for col in df.columns
]) for i in range(min(len(df), max_rows))]
)
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(children =[
html.H4(children = 'test'),
dcc.Dropdown( id = 'dropdown', options = [
{'label' : i , 'value' : i} for i in df.state.unique()
], multi = True, placeholder = 'Filter by State'),
html.Div(id='table-container'),
])
def display_table(dropdown_value):
if dropdown_value is None:
return generate_table(df)
#x = df[df['state'] == str(dropdown_value)]
return html.Div([
html.H5(filename),
html.H6(datetime.datetime.fromtimestamp(date)),
generate_table(df[df['state'].isin(dropdown_value)])
#app.css.append_css({"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"})
@app.callback(
dash.dependencies.Output('table-container', 'children'),
[dash.dependencies.Input('dropdown','value')])
if __name__ == '__main__':
app.run_server(debug=True)
嗨@ntwong我建议使用 dash_tables 来显示数据帧,这是您最接近的数据帧。 以下是一些使其正常工作的基本代码:
import dash_table
import pandas as pd
import dash_html_components as html
# create your dataframe "df" from your data idk what method you are using
html.Div([
dash_table.DataTable(data=df.to_dict("rows"), columns = [{"id": x, "name": x} for x in df.columns])
])
只需将代码放在脚本正文中即可。
不鼓励渲染原始 HTML,这里的其他人已经描述了将解析的 html 转换为dash-html-components
以与dash_table
一起使用的函数。您可以通过提供正确的样式信息使表格"看起来像"本机 jupyter 中的表格 - 或者您可以下载破折号危险设置内部 html 包,它可以让你显示它。
一个问题是,当你做df.to_html()
时,pandas 只返回一个基本的 html,而不是像这个问题中那样带有任何样式属性的 html——你可以通过渲染 df 然后获取 html 来解决(见下文(。 然后你解析 html,将样式提取到样式字典中。然后,也许您可以使用style_table/style_cell属性将这些样式传递给dash_table。
html = (df.style.render())
查看本指南,了解有关构建表的更多信息,