zauberzeug / nicegui

Create web-based user interfaces with Python. The nice way.
https://nicegui.io
MIT License
8.63k stars 521 forks source link

ui.number displayed number format not maintained #3324

Closed eddie3ruff closed 1 month ago

eddie3ruff commented 1 month ago

Description

ui.number(label='Test Field', min=1.0, max=100.0, step=1.0, value=10.0, format='%.2f')

The displayed number starts correctly at 10.00 but after changing the value, the displayed number is only an integer with no decimal point. The '%.2f' format is ignored only for the displayed number unless the page is refreshed.

For example, we start at 10.00 which is correct, then we click increment and we get 11 which is wrong. It should be 11.00 If we refresh the page, we do in fact get 11.00

python-and-fiction commented 1 month ago

It is a little similar to #3217 . I change LOOPBACK = False in .venv\Lib\site-packages\nicegui\elements\number.py to LOOPBACK = True. The format shows corretly as set.

Following code shows that _props['model-value'] has changed, but the value displayed in ui.number is the same as the value when input is activated.

from nicegui import ui

un = ui.number(label='Test Field', min=1.0, max=100.0, step=1.0, value=10.0, format='%.2f')
ui.button('print',on_click=lambda :ui.notify(un._props))

ui.run()
falkoschindler commented 1 month ago

I just created PR #3389 to update the number element on blur. This should apply the formatting as soon as the element looses focus.

This issue actually describes missing formatting when using the stepper buttons. But as far as I can tell, there is no easy way to discriminate stepper buttons from normal typing with the keyboard. And because we don't want to interfere the user typing on the keyboard, we don't want to send updates after using the steppers. But I guess that's ok, as long as the format is applied on blur.