Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: handle numeric formatting in table widgets (#3618)

devin-ai-integration[bot]0d4d779b459b
diff --git a/marimo/_plugins/ui/_impl/tables/format.py b/marimo/_plugins/ui/_impl/tables/format.py
index 1d32a8c50a..7ba700df2a 100644
--- a/marimo/_plugins/ui/_impl/tables/format.py
+++ b/marimo/_plugins/ui/_impl/tables/format.py
@@ -11,14 +11,27 @@ FormatMapping = Dict[str, Union[str, Callable[..., JSONType]]]
def format_value(
col: str, value: JSONType, format_mapping: FormatMapping
) -> JSONType:
- # Return None if the format mapping is None
if format_mapping is None:
return value
+ if value is None:
+ if col in format_mapping:
+ formatter = format_mapping[col]
+ if callable(formatter):
+ return formatter(value)
+ return value
+
if col in format_mapping:
formatter = format_mapping[col]
try:
if isinstance(formatter, str):
+ # Handle numeric formatting specially to preserve signs and separators
+ if isinstance(value, (int, float)):
+ # Keep integers as integers for 'd' format specifier
+ if isinstance(value, int) and "d" in formatter:
+ return formatter.format(value)
+ # Convert to float for float formatting
+ return formatter.format(float(value))
return formatter.format(value)
if callable(formatter):
return formatter(value)
diff --git a/tests/_plugins/ui/_impl/tables/test_format.py b/tests/_plugins/ui/_impl/tables/test_format.py
new file mode 100644
index 0000000000..b929f6978c
--- /dev/null
+++ b/tests/_plugins/ui/_impl/tables/test_format.py
@@ -0,0 +1,25 @@
+from __future__ import annotations
+
+from marimo._plugins.ui._impl.tables.format import format_value
+
+
+def test_numeric_formatting():
+ # Test positive number with + sign
+ assert format_value("col", 42.123, {"col": "{:+.2f}"}) == "+42.12"
+ assert format_value("col", -42.123, {"col": "{:+.2f}"}) == "-42.12"
+
+ # Test thousand separators
+ assert format_value("col", 1234.567, {"col": "{:,.2f}"}) == "1,234.57"
+ assert format_value("col", -1234.567, {"col": "{:,.2f}"}) == "-1,234.57"
+
+ # Test combining + sign and thousand separators
+ assert format_value("col", 1234.567, {"col": "{:+,.2f}"}) == "+1,234.57"
+ assert format_value("col", -1234.567, {"col": "{:+,.2f}"}) == "-1,234.57"
+
+ # Test integer values
+ assert format_value("col", 1234, {"col": "{:,d}"}) == "1,234"
+ assert format_value("col", -1234, {"col": "{:+,d}"}) == "-1,234"
+
+ # Test non-numeric values (should not be affected)
+ assert format_value("col", "text", {"col": "{}"}) == "text"
+ assert format_value("col", None, {"col": "{}"}) is None