Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number Input fails on float conversion #1479

Closed
bergmansj opened this issue Aug 23, 2023 · 2 comments
Closed

Number Input fails on float conversion #1479

bergmansj opened this issue Aug 23, 2023 · 2 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@bergmansj
Copy link

Description

Using the basic number input example from the docs:

from nicegui import ui

ui.number(label='Number', value=3.1415927, format='%.2f',
          on_change=lambda e: result.set_text(f'you entered: {e.value}'))
result = ui.label()

ui.run()

When you empy out the input field (or even use backspace to make '3.' from '3.14') the following error appears on the console:

could not convert string to float: ''
Traceback (most recent call last):
  File " <snip> /venv/lib/python3.10/site-packages/nicegui/events.py", line 373, in handle_event
    result = handler(arguments) if expects_arguments else handler()
  File " <snip> /venv/lib/python3.10/site-packages/nicegui/elements/mixins/value_element.py", line 30, in handle_change
    self.set_value(self._event_args_to_value(e))
  File "  <snip>  /venv/lib/python3.10/site-packages/nicegui/elements/number.py", line 97, in _event_args_to_value
    return float(e.args)
ValueError: could not convert string to float: ''

(local paths snipped)

This was introduced somewhere after v1.3.6 (that version has a different _event_args_to_value implementation that checks for an empty string)

@falkoschindler
Copy link
Contributor

falkoschindler commented Aug 23, 2023

Wow, thanks for reporting this bug, @bergmansj!

Minimum reproduction:

ui.number()

→ Type "1" followed by backspace.

@falkoschindler falkoschindler added the bug Something isn't working label Aug 23, 2023
@falkoschindler falkoschindler added this to the 1.3.12 milestone Aug 23, 2023
@falkoschindler falkoschindler self-assigned this Aug 23, 2023
@falkoschindler
Copy link
Contributor

Here I introduced the bug while "fixing some mypy issues":

a55a452#diff-876b4c6ca305085724524c58b88f3685057fb0a803275d6957375205a96dfb17

When removing all characters from the number input, the value becomes '' instead of None.
I fixed the condition in a001cab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants