回調警告:回調錯誤創建破折號數據表 (Callback warning: Callback error creating dash' DataTable)


問題描述

回調警告:回調錯誤創建破折號數據表 (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 zealousSlothBas van der Linden)

參考文件

  1. Callback warning: Callback error creating dash' DataTable (CC BY‑SA 2.5/3.0/4.0)

#Python #Callback #plotly-dash #pandas






相關問題

如何從控制台中導入的文件中訪問變量的內容? (How do I access the contents of a variable from a file imported in a console?)

在 python 3.5 的輸入列表中添加美元符號、逗號和大括號 (Adding dollar signs, commas and curly brackets to input list in python 3.5)

為 KeyError 打印出奇怪的錯誤消息 (Strange error message printed out for KeyError)

django 1.9 中的 from django.views.generic.simple import direct_to_template 相當於什麼 (What is the equivalent of from django.views.generic.simple import direct_to_template in django 1.9)

查詢嵌入列表中的數組 (Querying for array in embedded list)

如何在 Python 中搜索子字符串是否在二進製文件中? (How to search if a substring is into a binary file in Python?)

為什麼要避免 while 循環? (Why avoid while loops?)

使用python的json模塊解析json請求 (Parse a json request using json module of python)

為什麼使用 py2app 模塊創建 mac 文件時出現錯誤? (Why i am getting Error when creating mac file using py2app module?)

當 python 線程在網絡調用(HTTPS)中並且發生上下文切換時會發生什麼? (What happens when the python thread is in network call(HTTPS) and the context switch happens?)

如何繪製一條帶斜率和一個點的線?Python (How to plot a line with slope and one point given? Python)

Pickle 找不到我不使用的模塊? (Pickle can't find module that I am not using?)







留言討論