python - Null 不是 Dash Plotly 中的对象(评估“n.layout”)
问题描述
我正在使用 Dash Plotly 开发仪表板,单击选项卡时出现错误。
错误说 null is not an object (evaluating ‘n.layout’)
(此错误源于运行 Dash 应用程序的内置 JavaScript 代码。单击以查看完整的堆栈跟踪或打开浏览器的控制台。)"
谁能帮我解决这个问题?
我的代码在下面找到。
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_daq as daq
from dash.dependencies import Input, Output
import plotly.graph_objs as go
import pandas as pd
import numpy as np
from copy import copy
import dash_table
import json
import base64
import plotly.express as px
#Data
errors = pd.read_csv(r’/Users/kapital/Documents/ABCD/PM/errors.csv’)
external_stylesheets = [‘https://codepen.io/chriddyp/pen/bWLwgP.css’]
app = dash.Dash(name, external_stylesheets=external_stylesheets)
first_graph = dcc.Graph(id=‘graph1’,style={‘borderBottom’: ‘thin lightgrey solid’,‘padding’: ‘10px 5px’})
#, animate = True
content_tab_1 = html.Div(children = [
html.Div(first_graph, style = {‘vertical-align’:‘center’, ‘horizontal-align’:‘center’})
],
style={‘width’: ‘87%’})
app.layout = html.Div([
dcc.Tabs(id='tabs-example', value='tab-1', children=[
dcc.Tab(label='Tab one', value='tab-1',
children =[content_tab_1]),
dcc.Tab(label='Tab two', value='tab-2'),
]),
html.Div(id='tabs-example-content')
])
@app.callback(Output(‘graph1’, ‘figure’),
Input(‘tabs-example’, ‘value’))
def render_content(tab):
if tab == ‘tab-1’:
err_count = pd.DataFrame(errors['errorID'].value_counts().reset_index().values, columns=["Error", "Count"])
err_count = err_count.sort_index(axis = 0, ascending=True)
fig = px.bar(err_count, x = 'Error', y = 'Count')
return fig
# return html.Div([
# html.H3('Tab content 1')
# ])
if name == ‘main’:
app.run_server(debug=True)
解决方案
问题是您的回调函数没有else
条件语句。当用户选择第二个选项卡时,该回调没有备用路径,并返回None
,这会给您看到的错误。我这样做是为了修复它,但您可能需要更多:
@app.callback(Output('graph1', 'figure'),
Input('tabs-example', 'value'))
def render_content(tab):
if tab == 'tab-1':
err_count = pd.DataFrame.from_dict(dict(
Error=[1, 2, 3, ],
Count=[5, 6, 7])
)
err_count = err_count.sort_index(axis=0, ascending=True)
fig = px.bar(err_count, x='Error', y='Count')
return fig
else:
return {}
推荐阅读
- flutter - 页面浏览构建器颤动的分页
- java - jar 不是多版本 jar 文件,但设置了 --multi-release 选项
- java - 从 python 运行 jar 文件会导致初始化错误
- flutter - 添加后如何保存颤动表日历事件
- c# - VB.NET 中带有 Asp.Net Web 窗体的 PayPalCheckoutSDK 无法正常工作
- hdfs - 连接到 hdfs 时是否可以在 trino 中设置最大重试次数?
- python - 加密模块和加密方法不起作用
- python - Apache Beam - 在 PTransform 之外获取值提供者参数
- node.js - 在 nodejs 中的 res.render 之前设置变量
- google-chrome-extension - 服务工作者注册失败:mv2 到 mv3