英文:
Trying to return 2 outputs with Plotly DASH
问题
我试图计算BMI并返回BMI和其所属的类别,但它只返回了BMI。
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
if weight_unit == 'lbs' and height_unit == 'ft':
bmi = 703 * (weight/(height * 12)**2)
if weight_unit == 'kgs' and height_unit == 'm':
bmi = (weight)/((height)**2)
return bmi
def bmi_class(bmi):
if bmi < 16:
bmi_class = "Severe Thinness"
elif bmi >= 16 and bmi < 17:
bmi_class = "Moderate Thinness"
elif bmi >= 17 and bmi < 18.5:
bmi_class = "Mild Thinness"
elif bmi >= 18.5 and bmi < 25:
bmi_class = "Normal"
elif bmi >= 25 and bmi < 30:
bmi_class = "Overweight"
elif bmi >= 30 and bmi < 35:
bmi_class = "Obese Class I"
elif bmi >= 35 and bmi < 40:
bmi_class = "Obese Class II"
elif bmi >= 40:
bmi_class = "Obese Class III"
return bmi_class
错误信息:
dash._grouping.SchemaTypeValidationError: Schema: [<Output `bmi.children`>, <Output `bmi_class.children`>]
Path: ()
期望类型: (<class 'tuple'>, <class 'list'>)
收到类型为 <class 'float'> 的值: 22.955102067050735
英文:
I'm trying to calculate the BMI and return both the BMI and the category it falls in, but it is only returning the BMI.
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
if weight_unit == 'lbs' and height_unit == 'ft':
bmi = 703 * (weight/(height * 12)**2)
if weight_unit == 'kgs' and height_unit == 'm':
bmi = (weight)/((height)**2)
return bmi
def bmi_class(bmi):
if bmi < 16:
bmi_class = "Severe Thinness"
elif bmi >= 16 and bmi < 17:
bmi_class = "Moderate Thinness"
elif rval >= 17 and rval < 18.5:
bmi_class = "Mild Thinness"
elif rval >= 18.5 and rval < 25:
bmi_class = "Normal"
elif rval >= 25 and rval < 30:
bmi_class = "Overweight"
elif rval >= 30 and rval < 35:
bmi_class = "Obese Class I"
elif rval >= 35 and rval < 40:
bmi_class = "Obese Class II"
elif rval >= 40:
bmi_class = "Obese Class III"
return bmi_class
Error Message:
dash._grouping.SchemaTypeValidationError: Schema: [<Output `bmi.children`>, <Output `bmi_class.children`>]
Path: ()
Expected type: (<class 'tuple'>, <class 'list'>)
Received value of type <class 'float'>:
22.955102067050735
答案1
得分: 0
你只需要将这两个函数合并成一个函数,如下所示:
@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 = 0
bmi_class = " "
if weight_unit == 'lbs' and height_unit == 'ft':
bmi = 703 * (weight / (height * 12) ** 2)
if weight_unit == 'kgs' and height_unit == 'm':
bmi = (weight) / ((height) ** 2)
if bmi < 16:
bmi_class = "Severe Thinness"
elif 16 <= bmi < 17:
bmi_class = "Moderate Thinness"
elif 17 <= bmi < 18.5:
bmi_class = "Mild Thinness"
elif 18.5 <= bmi < 25:
bmi_class = "Normal"
elif 25 <= bmi < 30:
bmi_class = "Overweight"
elif 30 <= bmi < 35:
bmi_class = "Obese Class I"
elif 35 <= bmi < 40:
bmi_class = "Obese Class II"
elif bmi >= 40:
bmi_class = "Obese Class III"
return bmi, bmi_class
我将bmi
初始化为0
,因为它是一个整数变量,将bmi_class
初始化为一个空格。
英文:
All you need is to merge both functions in one function as follows:
@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 = 0
bmi_class = " "
if weight_unit == 'lbs' and height_unit == 'ft':
bmi = 703 * (weight/(height * 12)**2)
if weight_unit == 'kgs' and height_unit == 'm':
bmi = (weight)/((height)**2)
if bmi < 16:
bmi_class = "Severe Thinness"
elif bmi >= 16 and bmi < 17:
bmi_class = "Moderate Thinness"
elif rval >= 17 and rval < 18.5:
bmi_class = "Mild Thinness"
elif rval >= 18.5 and rval < 25:
bmi_class = "Normal"
elif rval >= 25 and rval < 30:
bmi_class = "Overweight"
elif rval >= 30 and rval < 35:
bmi_class = "Obese Class I"
elif rval >= 35 and rval < 40:
bmi_class = "Obese Class II"
elif rval >= 40:
bmi_class = "Obese Class III"
return bmi , bmi_class
I initialized the bmi
to 0
since it is an integer variable and bmi_class
to " "
Output:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论