Confused by NoneType error

I adapted (tried to simplify) an example on the “dcc.Input” page:
https://dash.plotly.com/dash-core-components/input
The output is good, but I am getting an error message on the web page:
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'
When I converted the Fahrenheit to Celsius equation to float, I got the following error message:
TypeError: float() argument must be a string or a real number, not 'NoneType'
This probably has more to do with my weak knowledge of Python than Bash or Plotly, but I don’t konw what to do. Thank you.

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

app = Dash(__name__, title='A_CD-02')

app.layout = html.Div(
    [
        html.Div('American temperature ?'),
        dcc.Input(
            id="input_F", type="number", placeholder="Fahrenheit",
            # min=10, max=100, step=3,
        ),
        html.Hr(),
        html.Div(id="output_C"),
    ]
)


@callback(
    Output("output_C", "children"),
    Input("input_F", "value")
)
def number_render(fahrenheit):
    celsius = (fahrenheit-32) * 5 / 9
    return celsius


if __name__ == "__main__":
    app.run_server(debug=True)
1 Like

@geodancer that is because all callbacks get called at startup, but the default value of the Input() is None. You have two options:

  • set an initial value for the Input()
  • prevent the callback from executing at startup

Here an example where I do both:

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

app = Dash(__name__, title='A_CD-02')

app.layout = html.Div(
    [
        html.Div('American temperature ?'),
        dcc.Input(
            id="input_F", type="number", placeholder="Fahrenheit",value=0
            # min=10, max=100, step=3,
        ),
        html.Hr(),
        html.Div(id="output_C"),
    ]
)


@callback(
    Output("output_C", "children"),
    Input("input_F", "value"),
    prevent_initial_call=True
)
def number_render(fahrenheit):
    celsius = (fahrenheit-32) * 5 / 9
    return celsius


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

Thanks for the reproducible example!

1 Like

@AIMPED - Both of your solutions check out. Thank you for the explanation and your two solutions. :grinning:

1 Like