Skip to content

Commit

Permalink
Merge pull request #198 from maxfordham/196-issue-with-hiding-nan-if-…
Browse files Browse the repository at this point in the history
…global_decimal_places-not-defined

🐛 Fix text_value VegaExpr for hide_nan parameter
  • Loading branch information
ollyhensby authored Oct 9, 2023
2 parents 2980761 + 627de0a commit fd6ffdb
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions src/ipyautoui/custom/autogrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,36 +488,34 @@ def _default_count_changes(self):

@tr.observe("global_decimal_places", "hide_nan")
def _set_text_value(self, change):
newfmt = f".{self.global_decimal_places}f"
global_decimal_place_vega_expr = f"""
if (
!isNumber(cell.value),
cell.value,
if self.global_decimal_places is None:
vega_expr = "cell.value"
else:
newfmt = f".{self.global_decimal_places}f"
vega_expr = f"""
if (
round(cell.value) == cell.value,
!isNumber(cell.value),
cell.value,
format(cell.value, '{newfmt}')
if (
round(cell.value) == cell.value,
cell.value,
format(cell.value, '{newfmt}')
)
)
)
"""
# ^ If not a number then return the value, else if the value is a whole number
# then return the value, else format the value to the number of decimal places specified
"""
# ^ If not a number then return the value, else if the value is a whole number
# then return the value, else format the value to the number of decimal places specified
if self.hide_nan:
vega_expr = f"""
if(
!isValid(cell.value),
' ',
{global_decimal_place_vega_expr}
{vega_expr}
)
"""
# ^ If the value is not valid (i.e. NaN) then return a blank space, else return the value
# evaluated by global_decimal_place vega expression
else:
vega_expr = global_decimal_place_vega_expr
if self.default_renderer is None:
self.default_renderer = TextRenderer(VegaExpr(vega_expr))
else:
self.default_renderer.text_value = VegaExpr(vega_expr)
self.default_renderer.text_value = VegaExpr(vega_expr)

@property
def datagrid_schema_fields(self):
Expand Down Expand Up @@ -1260,7 +1258,6 @@ class DataFrameCols(BaseModel):
class TestDataFrame(BaseModel):
__root__: ty.List[DataFrameCols] = Field(
format="dataframe",
global_decimal_places=2,
hide_nan=True,
)

Expand Down

0 comments on commit fd6ffdb

Please sign in to comment.