python - 如何修复“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.
如何修复错误?我尝试卸载并重新安装熊猫。
解决方案
您的错误是在为仪表板应用程序设置布局时未使用 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)
推荐阅读
- java - 无法在 hadoop 中运行 Java 代码。获取 javac:无效标志错误
- java - Android:如何访问最近使用的表情符号?
- sql - 在 SQL 中创建视图(为每种情况选择表)
- javascript - 如何在创建资源请求时附加处理程序?(例如img源,CSS文件)
- django - Django 用户默认权限
- c# - 我的 Blazor 服务器应用程序在一台本地计算机上的 Azure B2c 登录时遇到空引用错误,但在另一台本地计算机上没有,有什么想法吗?
- java - 当我使用 Java 时,程序会跳过数组中的一个值
- flutter - 如何将一个类的价值用于另一类?
- c++ - 使用 C++ 中非 const 引用的构造函数捕获异常类型
- javascript - 如何将具有动态键名和嵌套数组的对象数组简化为更简单的格式?