You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Dash库开发仪表盘第150行SyntaxError语法错误求助

问题现象

使用Python Dash库开发美国国内航空公司航班性能可视化仪表盘,调整代码后运行抛出无效语法错误,报错指向第150行的get_graph函数定义位置。

核心报错信息
line 150
    def get_graph(chart, year, children1, children2, c3, c4, c5):
    ^
SyntaxError: invalid syntax
错误根源排查

报错指向get_graph定义行的本质原因是上文中的布局代码存在未闭合的括号、符号误用问题,Python解析器认为get_graph的定义还处于之前的html.Div参数范围内,所以判定语法不合法。具体错误点如下:

  • 布局定义第一行错误使用双等号赋值:app.layout = html.Div(children==[ 应改为单等号 app.layout = html.Div(children=[
  • 整个布局结构缺少对应层级的闭合圆括号和方括号,嵌套层级没有正确闭合
  • 重复定义了id='plot1'的Div组件,同一页面DOM id不能重复
  • 报告类型下拉框的id错误设为input-year,和年份下拉框id冲突,且回调中引用了不存在的input-type输入源
  • 回调输出重复绑定了plot2,第三个输出应为plot3
  • 代码中的"是HTML转义字符,需要替换为正常的双引号"
  • 折线图line_fig的color参数写错列名:Reporting Airline 应改为 Reporting_Airline
修正后的核心代码示例

布局部分修正

# Application layout
app.layout = html.Div(children=[html.H1('US Domestic Airline Flights Performance', 
                                style={'textAlign': 'center', 'color': '#503D36',
                                'font-size': 40}),
                                html.Div([
                                    html.Div([
                                        html.Div(
                                            [
                                            html.H2('Report Type:', style={'margin-right': '2em'}),
                                            ]
                                        ),
                                        dcc.Dropdown(id='input-type', 
                   options=[
                           {'label': 'Yearly Airline Performance Report', 'value': 'OPT1'},
                           {'label': 'Yearly Airline Delay Report', 'value': 'OPT2'}
                           ],
                  placeholder='Select a report type',
                  style={'width':'80%', 'padding':'3px', 'font-size': '20px', 'text-align-last' : 'center'})
                                    ], style={'display':'flex'}),
                                    
                                   html.Div([
                                       html.Div(
                                            [
                                            html.H2('Choose Year:', style={'margin-right': '2em'})
                                            ]
                                        ),
                                        dcc.Dropdown(id='input-year', 
                   options=[{'label': i, 'value': i} for i in year_list],
                  placeholder='Select a year',
                  style={'width':'80%', 'padding':'3px', 'font-size': '20px', 'text-align-last' : 'center'})
                                   ], style={'display':'flex'}),
                                
                                html.Div([ ], id='plot1'),
    
                                html.Div([
                                        html.Div([ ], id='plot2'),
                                        html.Div([ ], id='plot3')
                                ], style={'display': 'flex'}),
                                
                                html.Div([
                                        html.Div([ ], id='plot4'),
                                        html.Div([ ], id='plot5')
                                ], style={'display': 'flex'})
                                ])

回调部分修正

@app.callback( [Output(component_id='plot1', component_property='children'),
 Output('plot2','children'),
 Output('plot3','children'),
 Output('plot4','children'),
 Output('plot5','children')],
               [Input(component_id='input-type', component_property='value'),
                Input(component_id='input-year', component_property='value')],
               [State("plot1", 'children'), State("plot2", "children"),
                State("plot3", "children"), State("plot4", "children"),
                State("plot5", "children")
               ])
def get_graph(chart, year, children1, children2, c3, c4, c5):
        # 原有逻辑不变,仅修正line_fig的列名
        line_fig = px.line(line_data, x='Month', y='AirTime', color='Reporting_Airline', title='Average monthly flight time (minutes) by airline')
        # 其余逻辑保持不变

修正后即可正常运行,语法错误会消失。

内容的提问来源于stack exchange,提问作者OCM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 22:24:05