問題描述
回調警告:回調錯誤創建破折號數據表 (Callback warning: Callback error creating dash' DataTable)
我正在嘗試從回調生成一個表,雖然該表實際上正在生成,但我收到一個警告:
更新 priceTable.data、priceTable 的回調錯誤。 columns
dash.exceptions.InvalidCallbackReturnValue:回調 ..priceTable.data...priceTable.columns.. 是一個多輸出。期望輸出類型是列表或元組,但得到:無。
我知道我的問題在於我如何佈置回調,但我不確定什麼是正確的格式應該是。我發現 這個在線回答對我有很大幫助,因為我有'
參考解法
方法 1:
The reason it is not working is because you're not handling the case in your callback where the button has not been clicked. Even if the button hasn't been clicked your callback still needs to return a tuple (of your data
and columns
).
That is what the error is saying here:
Expected the output type to be a list or tuple but got: None.
You don't return anything (None
) when n_clicks
is 0
(false
).
So what you need to do is return the data
and columns
when n_clicks
is 0
. The only difference in approach from what you have is that you don't apply any filters to the data if n_clicks
is 0
.
Full example:
import dash
import dash_html_components as html
import dash_core_components as dcc
import dash_table as dt
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/solar.csv")
app = dash.Dash(__name__)
states = df.State.unique().tolist()
numSP = df["Number of Solar Plants"].unique().tolist()
app.layout = html.Div(
[
dcc.Dropdown(
id="filter_dropdown",
options=[{"label": st, "value": st} for st in states],
value=states[0],
),
html.Br(),
dcc.Dropdown(
id="filter2",
options=[{"label": np, "value": np} for np in numSP]
# value = numSP[0]
),
html.Br(),
html.Button("Submit", id="submit‑button‑state", n_clicks=0),
html.Br(),
html.Div(id="output"),
html.Div(
# id ='priceTable'
dt.DataTable(id="priceTable")
),
]
)
def get_table_data(data):
columns = [
{
"name": i,
"id": i,
}
for i in (data.columns)
]
data = data.to_dict("records")
return (data, columns)
@app.callback(
[Output("priceTable", "data"), Output("priceTable", "columns")],
[Input("submit‑button‑state", "n_clicks")],
[State("filter_dropdown", "value")],
[State("filter2", "value")],
[State("submit‑button‑state", "n_clicks")],
)
def update_table(n_clicks, filter_dropdown, filter2, table):
if n_clicks:
if filter_dropdown is None and filter2 is None:
raise PreventUpdate
if filter_dropdown is not None and filter2 is not None:
table = df[df.State == filter_dropdown]
table = table[table["Number of Solar Plants"] == filter2]
if filter_dropdown is None and filter2 is not None:
table = df[df["Number of Solar Plants"] == filter2]
if filter_dropdown is not None and filter2 is None:
table = df[df.State == filter_dropdown]
(data, columns) = get_table_data(table)
print("columns", columns)
print("data", columns)
return data, columns
return get_table_data(df)
if __name__ == "__main__":
app.run_server(debug=True)
In the example above I've also put your code for retrieving data and columns from a dataframe in its own function get_table_data
. For making sure a tuple of your data is always returned I've used a guard clause.
(by zealousSloth、Bas van der Linden)