首页 > 解决方案 > 如何修复“dash.exceptions.NoLayoutException”错误

问题描述

我想用相同值的总和创建一个聚合 df,并将日期列放在 df 中。这是我原来的df:

在此处输入图像描述

这是我的代码:

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

raw = [['tom', 10, pd.to_datetime('2021-01-01')], ['nick', 15, pd.to_datetime('2021-01-01')], ['juli', 14, pd.to_datetime('2021-01-01')],['tom', 8, pd.to_datetime('2021-01-02')],
        ['nick', 4, pd.to_datetime('2021-01-02')], ['juli', 12, pd.to_datetime('2021-01-02')]]
df = pd.DataFrame(raw, columns=['Name', 'Apples Gathered', 'date'])

#App layout
app = dash.Dash(__name__, prevent_initial_callbacks=True, suppress_callback_exceptions=True)

layout = html.Div([
    dcc.DatePickerRange(
        id='datepicker',
        display_format='DD-MM-YYYY',
        first_day_of_week=1,
        max_date_allowed=dt.today(),
    ),
])
dash_table.DataTable(
       id='datatable',
       columns=[
            {'id': 'Name', 'name': 'Name'},
            {'id': 'Apples Gathered', 'name': 'Apples Gathered'},
            {'id': 'date', 'name': 'Date'}],

        data = df.to_dict('records'),
)

@app.callback(
       Output('datatable', 'data'),
       [Input('datepicker', 'start_date'),
       Input('date-picker', 'end_date')]
)
def update_table(start_date, end_date):
   dff = (df.groupby(['Name', 'Apples Gathered']).sum())
   return dff.to_dict('records')

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

当我运行它时,我收到一条错误消息:

dash.exceptions.NoLayoutException
dash.exceptions.NoLayoutException: The layout was `None` at the time that `run_server` was called.
Make sure to set the `layout` attribute of your application
before running the server.

如何修复错误?我尝试卸载并重新安装熊猫。

标签: pythonpandasplotlyplotly-dashplotly-python

解决方案


您的错误是在为仪表板应用程序设置布局时未使用 app.layout。此代码有效:

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

raw = [['tom', 10, pd.to_datetime('2021-01-01')], ['nick', 15, pd.to_datetime('2021-01-01')], ['juli', 14, pd.to_datetime('2021-01-01')],['tom', 8, pd.to_datetime('2021-01-02')],
        ['nick', 4, pd.to_datetime('2021-01-02')], ['juli', 12, pd.to_datetime('2021-01-02')]]
df = pd.DataFrame(raw, columns=['Name', 'Apples Gathered', 'date'])

#App layout
app = dash.Dash(__name__, prevent_initial_callbacks=True, suppress_callback_exceptions=True)

app.layout = html.Div([
    dcc.DatePickerRange(
        id='datepicker',
        display_format='DD-MM-YYYY',
        first_day_of_week=1,
        max_date_allowed=dt.today(),
    ),
])
dash_table.DataTable(
       id='datatable',
       columns=[
            {'id': 'Name', 'name': 'Name'},
            {'id': 'Apples Gathered', 'name': 'Apples Gathered'},
            {'id': 'date', 'name': 'Date'}],

        data = df.to_dict('records'),
)

@app.callback(
       Output('datatable', 'data'),
       [Input('datepicker', 'start_date'),
       Input('date-picker', 'end_date')]
)
def update_table(start_date, end_date):
   dff = (df.groupby(['Name', 'Apples Gathered']).sum())
   return dff.to_dict('records')

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

推荐阅读