尝试使用Plotly DASH返回2个输出

huangapple go评论56阅读模式
英文:

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(&quot;BMI Calculator&quot;),
html.Div([
html.H2(&#39;Enter your weight&#39;),
dcc.Input(id = &#39;weight&#39;, value = 160, type = &#39;number&#39;),
dcc.RadioItems(options = [{&#39;label&#39;: &#39;pounds&#39;, &#39;value&#39;: &#39;lbs&#39;}, {&#39;label&#39;: &#39;kilograms&#39;, &#39;value&#39;: &#39;kgs&#39;}], 
value = &#39;lbs&#39;,
id = &#39;weight_unit&#39;),
html.H2(&#39;Enter your height&#39;),
dcc.Input(id = &#39;height&#39;, value = 5.83333333, type = &#39;number&#39;),
dcc.RadioItems(options = [{&#39;label&#39;: &#39;feet&#39;, &#39;value&#39;: &#39;ft&#39;},{&#39;label&#39;: &#39;meters&#39;, &#39;value&#39;: &#39;m&#39;}], 
value = &#39;ft&#39;, 
id = &#39;height_unit&#39;),      
]),
html.Br(),
html.H1(&quot;Your estimated body mass index is:&quot;),
html.H1(id = &#39;bmi&#39;),
html.H1(&quot;BMI Classification:&quot;),
html.H1(id = &#39;bmi_class&#39;),
])
@app.callback(
Output(component_id = &#39;bmi&#39;, component_property = &#39;children&#39;),
Output(component_id = &#39;bmi_class&#39;, component_property = &#39;children&#39;),
Input(component_id  = &#39;weight&#39;, component_property = &#39;value&#39;),
Input(component_id  = &#39;weight_unit&#39;, component_property = &#39;value&#39;),
Input(component_id  = &#39;height&#39;, component_property = &#39;value&#39;),
Input(component_id  = &#39;height_unit&#39;, component_property = &#39;value&#39;),
)
def update_output_div(weight, weight_unit, height, height_unit):
bmi = None 
if weight_unit == &#39;lbs&#39; and height_unit == &#39;ft&#39;:
bmi = 703 * (weight/(height * 12)**2)
if weight_unit == &#39;kgs&#39; and height_unit == &#39;m&#39;:
bmi = (weight)/((height)**2)
return bmi 
def bmi_class(bmi):
if bmi &lt; 16:
bmi_class = &quot;Severe Thinness&quot;
elif bmi &gt;= 16 and bmi &lt; 17:
bmi_class = &quot;Moderate Thinness&quot;
elif rval &gt;= 17 and rval &lt; 18.5:
bmi_class = &quot;Mild Thinness&quot;
elif rval &gt;= 18.5 and rval &lt; 25:
bmi_class = &quot;Normal&quot;
elif rval &gt;= 25 and rval &lt; 30:
bmi_class = &quot;Overweight&quot;
elif rval &gt;= 30 and rval &lt; 35:
bmi_class = &quot;Obese Class I&quot;
elif rval &gt;= 35 and rval &lt; 40:
bmi_class = &quot;Obese Class II&quot;
elif rval &gt;= 40:
bmi_class = &quot;Obese Class III&quot;
return bmi_class

Error Message:

dash._grouping.SchemaTypeValidationError: Schema: [&lt;Output `bmi.children`&gt;, &lt;Output `bmi_class.children`&gt;]
Path: ()
Expected type: (&lt;class &#39;tuple&#39;&gt;, &lt;class &#39;list&#39;&gt;)
Received value of type &lt;class &#39;float&#39;&gt;:
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 = &#39;bmi&#39;, component_property = &#39;children&#39;),
Output(component_id = &#39;bmi_class&#39;, component_property = &#39;children&#39;),
Input(component_id  = &#39;weight&#39;, component_property = &#39;value&#39;),
Input(component_id  = &#39;weight_unit&#39;, component_property = &#39;value&#39;),
Input(component_id  = &#39;height&#39;, component_property = &#39;value&#39;),
Input(component_id  = &#39;height_unit&#39;, component_property = &#39;value&#39;),
)
def update_output_div(weight, weight_unit, height, height_unit):
bmi = 0
bmi_class = &quot; &quot;
if weight_unit == &#39;lbs&#39; and height_unit == &#39;ft&#39;:
bmi = 703 * (weight/(height * 12)**2)
if weight_unit == &#39;kgs&#39; and height_unit == &#39;m&#39;:
bmi = (weight)/((height)**2)
if bmi &lt; 16:
bmi_class = &quot;Severe Thinness&quot;
elif bmi &gt;= 16 and bmi &lt; 17:
bmi_class = &quot;Moderate Thinness&quot;
elif rval &gt;= 17 and rval &lt; 18.5:
bmi_class = &quot;Mild Thinness&quot;
elif rval &gt;= 18.5 and rval &lt; 25:
bmi_class = &quot;Normal&quot;
elif rval &gt;= 25 and rval &lt; 30:
bmi_class = &quot;Overweight&quot;
elif rval &gt;= 30 and rval &lt; 35:
bmi_class = &quot;Obese Class I&quot;
elif rval &gt;= 35 and rval &lt; 40:
bmi_class = &quot;Obese Class II&quot;
elif rval &gt;= 40:
bmi_class = &quot;Obese Class III&quot;
return bmi , bmi_class

I initialized the bmi to 0 since it is an integer variable and bmi_class to &quot; &quot;

Output:

尝试使用Plotly DASH返回2个输出

huangapple
  • 本文由 发表于 2023年3月12日 15:04:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定