我用Plotly Dash创建了条形图。一切正常,但是当我单击“清除值”时:
我收到下一条错误消息:
KeyError: ('Quantity', 'declined')
Traceback (most recent call last)
File "C:\Users\iakubal\Anaconda3\envs\general_3_6\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
During handling of the above exception, another exception occurred:
File "pandas\_libs\index.pyx", line 701, in pandas._libs.index.BaseMultiIndexCodesEngine.get_loc
File "C:\Users\iakubal\Anaconda3\envs\general_3_6\lib\site-packages\pandas\core\indexes\base.py", line 2648, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
During handling of the above exception, another exception occurred:
File "pandas\_libs\index.pyx", line 704, in pandas._libs.index.BaseMultiIndexCodesEngine.get_log
KeyError: ('Quantity', 'declined')
Traceback (most recent call last):
File "C:\Users\iakubal\Anaconda3\envs\general_3_6\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'declined'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pandas\_libs\index.pyx", line 701, in pandas._libs.index.BaseMultiIndexCodesEngine.get_loc
File "C:\Users\iakubal\Anaconda3\envs\general_3_6\lib\site-packages\pandas\core\indexes\base.py", line 2648, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'declined'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pandas\_libs\index.pyx", line 704, in pandas._libs.index.BaseMultiIndexCodesEngine.get_loc
KeyError: ('Quantity', 'declined')
此外,当我单击“清除值”时,它应显示原始图,但不会执行此操作。
我试图将一个数据帧更改为另一个,但是我仍然遇到相同的错误。在我看来,熊猫存在问题。这是可复制的代码段(摘自here):
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
# Read in the data from Excel
df = pd.read_excel(
"https://github.com/chris1610/pbpython/blob/master/data/salesfunnel.xlsx?raw=True"
)
# Get a list of all the avilable managers
mgr_options = df["Manager"].unique()
# Create the app
app = dash.Dash()
# Populate the layout with HTML and graph components
app.layout = html.Div([
html.H2("Sales Funnel Report"),
html.Div(
[
dcc.Dropdown(
id="Manager",
options=[{
'label': i,
'value': i
} for i in mgr_options],
value='All Managers'),
],
style={'width': '25%',
'display': 'inline-block'}),
dcc.Graph(id='funnel-graph'),
])
# Add the callbacks to support the interactive componets
@app.callback(
dash.dependencies.Output('funnel-graph', 'figure'),
[dash.dependencies.Input('Manager', 'value')])
def update_graph(Manager):
if Manager == "All Managers":
df_plot = df.copy()
else:
df_plot = df[df['Manager'] == Manager]
pv = pd.pivot_table(
df_plot,
index=['Name'],
columns=["Status"],
values=['Quantity'],
aggfunc=sum,
fill_value=0)
trace1 = go.Bar(x=pv.index, y=pv[('Quantity', 'declined')], name='Declined')
trace2 = go.Bar(x=pv.index, y=pv[('Quantity', 'pending')], name='Pending')
trace3 = go.Bar(x=pv.index, y=pv[('Quantity', 'presented')], name='Presented')
trace4 = go.Bar(x=pv.index, y=pv[('Quantity', 'won')], name='Won')
return {
'data': [trace1, trace2, trace3, trace4],
'layout':
go.Layout(
title='Customer Order Status for {}'.format(Manager),
barmode='stack')
}
if __name__ == '__main__':
app.run_server(debug=True)
希望此代码段将有助于找到答案。
答案 0 :(得分:2)
您可以在回调函数的顶部添加这样的内容:
var img_path;
var upload_path;
if (img.length != 0){
var imageFile = img[0];
var imageName = imageFile.name;
img_path = ‘/some-path/’ + imageName;
//upload image
let storageRef = firebase.storage().ref(img_path);
storageRef.put(imageFile).then((savedPicture) => {
storageRef.getDownloadURL().then(function(downloadURL) {
console.log('File available at', downloadURL);
upload_path = downloadURL;
});
});
//returns undefined
console.log(upload_path);
编辑: 从更改到原始问题,我相信这将为您提供所需的行为:
if Manager is None:
raise dash.exceptions.PreventUpdate
对于用户来说,这是一种棘手的行为,因为它实际上是在隐藏选项。最好包含此选项( if Manager == "All Managers" or Manager is None:
df_plot = df.copy()
else:
df_plot = df[df['Manager'] == Manager]
作为下拉选项之一,然后将下拉菜单设置为Manger == "All Managers")
。