Repository: zauberzeug/nicegui
Pull request #3389 (2024-07-23 06:09:20)

Update `ui.number` after sanitization
This PR solves #3324 by updating the number element after sanitization on blur. This is necessary, because calling `set_value` not always updates the client. E.g. an input value "10" equals the formatted value "10.000", so an explicit `update()` is necessary.


Patch:
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index b3a16734..75646d8c 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -75,29 +75,6 @@ To view the log output, use the command
 
 ### Formatting
 
-We use [pre-commit](https://github.com/pre-commit/pre-commit) to make sure the coding style is enforced.
-You first need to install pre-commit and the corresponding git commit hooks by running the following commands:
-
-```bash
-python3 -m pip install pre-commit
-pre-commit install
-```
-
-After that you can make sure your code satisfies the coding style by running the following command:
-
-```bash
-pre-commit run --all-files
-```
-
-These checks will also run automatically before every commit.
-The command may fail with
-
-> RuntimeError: failed to find interpreter for Builtin discover of python_spec='python3.8'
-
-You will need to install Python 3.8 and make sure it is available in your `PATH`.
-
-### Formatting
-
 We use [autopep8](https://github.com/hhatto/autopep8) with a 120 character line length to format our code.
 Before submitting a pull request, please run
 
@@ -114,24 +91,46 @@ There are cases where one or the other arrangement of, e.g., function arguments
 Then we like the flexibility to either put all arguments on separate lines or only put the lengthy event handler
 on a second line and leave the other arguments as they are.
 
-### Imports
+### Linting
 
-We use [ruff](https://docs.astral.sh/ruff/) to automatically sort imports:
+We use [pre-commit](https://github.com/pre-commit/pre-commit) to make sure the coding style is enforced.
+You first need to install pre-commit and the corresponding git commit hooks by running the following commands:
 
 ```bash
-ruff check . --fix
+python3 -m pip install pre-commit
+pre-commit install
 ```
 
-### Single vs Double Quotes
-
-Regarding single or double quotes: [PEP 8](https://peps.python.org/pep-0008/) doesn't give any recommendation, so we simply chose single quotes and sticked with it.
-On qwerty keyboards it's a bit easier to type, is visually less cluttered, and it works well for strings containing double quotes from the English language.
+After that you can make sure your code satisfies the coding style by running the following command:
 
-### F-Strings
+```bash
+pre-commit run --all-files
+```
 
-We use f-strings where ever possible because they are generally more readable - once you get used to them.
-There are only a few places in the code base where performance really matters and f-strings might not be the best choice.
-These places should be marked with a `# NOTE: ...` comment when diverging from f-string usage.
+> [!TIP]
+> The command may fail with
+>
+> > RuntimeError: failed to find interpreter for Builtin discover of python_spec='python3.8'
+>
+> You will need to install Python 3.8 and make sure it is available in your `PATH`.
+
+These checks will also run automatically before every commit:
+
+- Run `ruff check . --fix` to check the code and sort imports.
+- Remove trailing whitespace.
+- Fix end of files.
+- Enforce single quotes.
+
+> [!NOTE]
+>
+> **Regarding single or double quotes:** > [PEP 8](https://peps.python.org/pep-0008/) doesn't give any recommendation, so we simply chose single quotes and sticked with it.
+> On qwerty keyboards it's a bit easier to type, is visually less cluttered, and it works well for strings containing double quotes from the English language.
+
+> [!NOTE]
+>
+> **We use f-strings** where ever possible because they are generally more readable - once you get used to them.
+> There are only a few places in the code base where performance really matters and f-strings might not be the best choice.
+> These places should be marked with a `# NOTE: ...` comment when diverging from f-string usage.
 
 ## Running tests
 
diff --git a/nicegui/elements/number.py b/nicegui/elements/number.py
index e4691d95..ae45487a 100644
--- a/nicegui/elements/number.py
+++ b/nicegui/elements/number.py
@@ -116,6 +116,7 @@ class Number(ValidationElement, DisableableElement):
         if self.precision is not None:
             value = float(round(value, self.precision))
         self.set_value(float(self.format % value) if self.format else value)
+        self.update()
 
     def _event_args_to_value(self, e: GenericEventArguments) -> Any:
         if not e.args:


Test patch:
diff --git a/tests/test_number.py b/tests/test_number.py
index 0711591b..09114a15 100644
--- a/tests/test_number.py
+++ b/tests/test_number.py
@@ -30,6 +30,11 @@ def test_apply_format_on_blur(screen: Screen):
     screen.click('Button')
     screen.should_contain_input('3.1417')
 
+    element.click()
+    element.send_keys(Keys.BACKSPACE * 10 + '2')
+    screen.click('Button')
+    screen.should_contain_input('2.0000')
+
 
 def test_max_value(screen: Screen):
     ui.number('Number', min=0, max=10, value=5)