仪表盘--用下拉菜单更新数据表中的行[重复]

问题描述 投票:0回答:1

我想在有人改变下拉值时更新数据表。我已经做了一些尝试,但似乎并不成功。以下是官方教程中的例子,但做了些许修改。

(1)尝试1。

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import dash_table as dt
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/solar.csv')

app = dash.Dash(__name__)

app.layout = html.Div([
    dcc.Dropdown(
        id='dropdown',
        options=[{'label': 'California', 'value': 'CA'},
                 {'label': 'Texas', 'value': 'TX'},
                 {'label': 'New York', 'value': 'NY'}],
        value='CA'
    ),
    dt.DataTable('data-table-1')
])

@app.callback(
    Output('data-table-1', 'row'),
    [Input('dropdown', 'value')]
)
def update_rows(selected_value):
    data = df[df['State'] == selected_value]
    columns = [{"name": i, "id": i} for i in data.columns]
    return [dt.DataTable(data=data, columns=columns)]

if __name__ == '__main__':
    app.run_server(debug=True)

错误信息说

                            Attempting to assign a callback to the
                            component with the id "data-table-1" but no
                            components with id "data-table-1" exist in the
                            app's layout.

                            Here is a list of IDs in layout: ['dropdown']

                            If you are assigning callbacks to components
                            that are generated by other callbacks
                            (and therefore not in the initial layout), then
                            you can suppress this exception by setting
                            `suppress_callback_exceptions=True`.

它说没有id为'data-table-1'的组件存在,我不知道为什么会这样。这个id'data-table-1'在app.layout中。

其次,它说要设置'suppress_callback_exceptions=True',所以下面是另一个尝试。

(2)尝试2:只在'尝试1'中增加一行代码。

app.config['suppress_callback_exceptions'] = True

添加这行代码后,我可以运行应用程序。但当我进入仪表板时,它有以下错误信息。

Invalid argument `active_cell` passed into DataTable.
Expected `object`.
Was supplied type `string`.
Value provided: "data-table-1"

谁能帮我解决这个问题,谢谢。

python datatable dropdown plotly-dash hyphen
1个回答
1
投票

从你的第一个例子来看,问题在于你只是没有完全把ID分配正确。这是你需要的。

app.layout = html.Div([
    dcc.Dropdown(
        id='dropdown',
        options=[{'label': 'California', 'value': 'CA'},
                 {'label': 'Texas', 'value': 'TX'},
                 {'label': 'New York', 'value': 'NY'}],
        value='CA'
    ),
    dt.DataTable(id='data-table-1')
])
© www.soinside.com 2019 - 2024. All rights reserved.