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

Plotly DASH BMI计算器报错:无法同时返回BMI值与分类

解决Plotly Dash BMI计算器的返回值错误及分类功能实现

错误原因分析

  • 回调函数定义了两个Output(bmi.children和bmi_class.children),但当前仅返回单个bmi数值,不符合Dash要求的返回类型(必须是元组或列表)
  • bmi_class函数内部存在变量名错误:所有rval都应替换为bmi
  • bmi_class函数从未被调用,无法生成体重分类结果

修改后的完整代码

import dash
from dash import html, dcc, Input, Output

app = Dash(__name__)

app.layout = html.Div([
    html.H1("BMI Calculator"),
    html.Div([
        html.H2('Enter your weight'),
        dcc.Input(id='weight', value=160, type='number'),
        dcc.RadioItems(options=[{'label': 'pounds', 'value': 'lbs'}, {'label': 'kilograms', 'value': 'kgs'}], 
                       value='lbs',
                       id='weight_unit'),
        html.H2('Enter your height'),
        dcc.Input(id='height', value=5.83333333, type='number'),
        dcc.RadioItems(options=[{'label': 'feet', 'value': 'ft'},{'label': 'meters', 'value': 'm'}], 
                       value='ft', 
                       id='height_unit'),      
    ]),
    html.Br(),
    html.H1("Your estimated body mass index is:"),
    html.H1(id='bmi'),
    html.H1("BMI Classification:"),
    html.H1(id='bmi_class'),

])

@app.callback(
    Output(component_id='bmi', component_property='children'),
    Output(component_id='bmi_class', component_property='children'),
    Input(component_id='weight', component_property='value'),
    Input(component_id='weight_unit', component_property='value'),
    Input(component_id='height', component_property='value'),
    Input(component_id='height_unit', component_property='value'),
)
    
def update_output_div(weight, weight_unit, height, height_unit):
    bmi = None 
    # 处理匹配的单位组合计算BMI
    if weight_unit == 'lbs' and height_unit == 'ft':
        bmi = 703 * (weight/(height * 12)**2)
    elif weight_unit == 'kgs' and height_unit == 'm':
        bmi = weight / (height ** 2)
    
    # 获取BMI分类结果
    classification = bmi_class(bmi)
    
    # 格式化BMI显示,处理单位不匹配的情况
    bmi_display = f"{bmi:.2f}" if bmi is not None else "无效的单位组合"
    return bmi_display, classification

def bmi_class(bmi):
    # 修复变量名错误,添加空值判断
    if bmi is None:
        return "请选择匹配的单位组合(磅+英尺 / 千克+米)"
    elif bmi < 16:
        return "Severe Thinness"
    elif 16 <= bmi < 17:
        return "Moderate Thinness"
    elif 17 <= bmi < 18.5:
        return "Mild Thinness"
    elif 18.5 <= bmi < 25:
        return "Normal"
    elif 25 <= bmi < 30:
        return "Overweight"
    elif 30 <= bmi < 35:
        return "Obese Class I"
    elif 35 <= bmi < 40:
        return "Obese Class II"
    elif bmi >= 40:
        return "Obese Class III"

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

关键修改点

  • 回调返回值调整:将返回值改为(bmi_display, classification)元组,匹配两个Output的要求
  • 修复变量错误:把bmi_class函数中所有rval替换为bmi
  • 调用分类函数:在回调函数内调用bmi_class(bmi)获取分类结果
  • 增加异常处理:处理单位不匹配的情况,返回友好提示
  • 格式化BMI显示:将BMI数值保留两位小数,提升可读性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 23:27:16