首页 > 解决方案 > 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)

标签: pythondashboardplotly-dash

解决方案


问题是您的回调函数没有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 {}

推荐阅读