maxfordham / ipyautoui

create ipywidgets user input form pydantic model or jsonschema
https://maxfordham.github.io/ipyautoui/
39 stars 4 forks source link

🐛 Can't hide NaNs and set global_decimal_places #193

Closed ollyhensby closed 11 months ago

ollyhensby commented 11 months ago

Issue When setting the global_decimal_places the NaNs appear in that columns even if passed to the default_renderer that they should be hidden.

I think this is because the individual renderers defined for a column overrides the default.

Solution Similar to global_decimal_places in schema, we could add a hide_nans to datagrid_traits and if they are both set to true then we set the following Vega Expression:

VegaExpr(
f"""
  if(
      isValid(cell.value),
      format(cell.value, '.{str(self.global_decimal_places)}f'),
      ' '
  )
"""

e.g.

We set hide_nans in TestDataFrame. We also see that global_decimal_places is set to 2 so the above VegaExpr is constructed and set for that column.

class DataFrameCols(BaseModel):
    floater: float = Field(
        None,
    )

class TestDataFrame(BaseModel):
    __root__: ty.List[DataFrameCols] = Field(
        format="dataframe",
        global_decimal_places=2,
        hide_nans=True,
    )
ollyhensby commented 11 months ago

Use updated VegaExpr and just set to default_renderer

if(
    !isValid(cell.value),
    ' ',
    if (
        isNumber(cell.value),
        format(cell.value, '.2f'),
        cell.value
    )
)

https://github.com/maxfordham/ipyautoui/blob/782955ed379cd2b4f53b0ec0de74557813c2e80e/src/ipyautoui/custom/autogrid.py#L489-L501

Instead of having to loop through the cols to see which are of type number, the VegaExpr will handle it.