question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Input with type number returning unicode after first value change

See original GitHub issue

Hi,

i stumbled across a problem when i wanted to use numeric input to change something in my dash app. It seems that for an dcc.Input of type number the value that is handed to the callback function is not a float but a unicode string.

The following code should clarify the problem as soon as you change a value in the browser:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

app = dash.Dash()

app.layout = html.Div(children=[
    html.H1(children='Hello Dash'),
    dcc.Input(id='input_1', value=0.7, type="number"),
    dcc.Input(id='input_2', value=0.7, type="number"),
    html.Div(id='typeof_1'),
    html.Div(id='typeof_2'),
    html.Div(id='display_sum'),
    ]
)
@app.callback(
    Output('typeof_1', 'children'),
    [Input('input_1', 'value')]
)
def update_output_div(input_value1):
    return 'type of input 1"{}"'.format(type(input_value1))
  
@app.callback(
    Output('typeof_2', 'children'),
    [Input('input_2', 'value')]
)
def update_output_div2(input_value2):
    return 'type of input 2"{}"'.format(type(input_value2))


@app.callback(
    Output('display_sum', 'children'),
    [Input('input_1', 'value'),
     Input('input_2', 'value'),]
)
def update_output_div3(input_value1, input_value2):
    return 'sum "{}"'.format(input_value1+input_value2)

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

I guess the problem is that the Input is just always treated as a string no matter what type you choose. Is this something i have to live with or would you consider this a bug that should be fixed?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
chriddypcommented, Sep 4, 2017

I do think that the implicit type casting is confusing. The user will most likely set the value to a number, only to find it being returned as a string (Input(value=0.7)).

has the result of giving you automatic client-side validation that the input is a number and possibly other enhancements such as increment buttons, but is only intended as a convenience for client-side development

That’s right - We’re just using the default behaviour right now: https://github.com/plotly/dash-core-components/blob/master/src/components/Input.react.js#L28-L43

Fixing this wouldn’t be too tough, I’m in favor of it 👍

0reactions
Madhu94commented, Oct 7, 2017

Cool! Thanks @chriddyp

Read more comments on GitHub >

github_iconTop Results From Across the Web

<input type="number"> - HTML: HyperText Markup Language
A Boolean attribute which, if present, means this field cannot be edited by the user. Its value can, however, still be changed by...
Read more >
HTML input type="number" still returning a string when ...
When you set input type="number" then these input field don't accept non numeric input but at the same time it doesn't make the...
Read more >
Insert ASCII or Unicode character codes in Word
Press and hold the ALT key and type the character code on the numeric keypad. Change the font back to your previous font...
Read more >
GREL functions - OpenRefine
Takes any value type (string, number, date, boolean, error, ... Returns the first character index of sub as it first occurs in s;...
Read more >
What every JavaScript developer should know about Unicode
Escape sequences in strings are used to express code units based on code point numbers. JavaScript has 3 escape types, one which was...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found