[英]Unable to access dataframe while uploading form plotly-dash app
I am new to python and plotly-dash.我是 python 和 plotly-dash 的新手。 I am trying to use "Hidden Div" to store a data frame as suggested in dash tutorial 5. But I am not able to process the uploaded file.
我正在尝试使用“Hidden Div”来存储数据框,如破折号教程 5 中所建议的那样。但我无法处理上传的文件。
import base64
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
#global_df = pd.read_csv('...')
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Graph(id='graph'),
html.Table(id='table'),
dcc.Upload(
id='datatable-upload',
children=html.Div(['Drag and Drop or ',html.A('Select Files')]),
),
# Hidden div inside the app that stores the intermediate value
html.Div(id='intermediate-value', style={'display': 'none'})
])
def parse_contents(contents, filename):
content_type, content_string = contents.split(',') #line 28
decoded = base64.b64decode(content_string)
if 'csv' in filename:
# Assume that the user uploaded a CSV file
return pd.read_csv(
io.StringIO(decoded.decode('utf-8')))
elif 'xls' in filename:
# Assume that the user uploaded an excel file
return pd.read_excel(io.BytesIO(decoded))
elif 'xlsx' in filename:
# Assume that the user uploaded an excel file
return pd.read_excel(io.BytesIO(decoded))
@app.callback(Output('intermediate-value', 'children'),
[Input('datatable-upload', 'contents')],
[State('datatable-upload', 'filename')])
def update_output(contents, filename):
# some expensive clean data step
cleaned_df = parse_contents(contents, filename)
# more generally, this line would be
# json.dumps(cleaned_df)
return cleaned_df.to_json(date_format='iso', orient='split')
@app.callback(Output('graph', 'figure'), [Input('intermediate-value', 'children')])
def update_graph(jsonified_cleaned_data):
# more generally, this line would be
# json.loads(jsonified_cleaned_data)
dff = pd.read_json(jsonified_cleaned_data, orient='split')
figure = create_figure(dff)
return figure
@app.callback(Output('table', 'children'), [Input('intermediate-value', 'children')])
def update_table(jsonified_cleaned_data):
dff = pd.read_json(jsonified_cleaned_data, orient='split')
table = create_table(dff)
return table
if __name__ == '__main__':
app.run_server(port=8050, host='0.0.0.0')
I am getting the following error while running the code:运行代码时出现以下错误:
File "ipython-input-12-4bd6fe1b7399", line 28, in parse_contents content_type, content_string = contents.split(',') AttributeError: 'NoneType' object has no attribute 'split'
文件“ipython-input-12-4bd6fe1b7399”,第 28 行,在 parse_contents content_type 中,content_string = contents.split(',') AttributeError: 'NoneType' object 没有属性 'split'
The callback is likely running on initialization with empty values.回调很可能在使用空值初始化时运行。 You can prevent this by adding something like this at the top of your callback:
您可以通过在回调顶部添加类似这样的内容来防止这种情况发生:
if contents is None:
raise dash.exceptions.PreventUpdate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.