zauberzeug / nicegui

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

ui.aggrid does not show values if there is a period in the column name #1417

Closed dashorty closed 1 year ago

dashorty commented 1 year ago

Description

If there is a period in the Name column, the value of this column is not displayed. I noticed when I wanted to normalize a JSON with panda.

Here is an example:

import pandas as pd
from nicegui import ui

data = [
    {"id": 1, "name": {"first": "Coleen", "last": "Volk"}},
    {"name": {"given": "Mark", "family": "Regner"}},
    {"id": 2, "name": "Faye Raker"},
]

df = pd.json_normalize(data)
df2 = pd.json_normalize(data, sep="_")

ui.label("with period in the column name")
ui.aggrid.from_pandas(df)
ui.label("without period in the column name (sep=\"_\")")
ui.aggrid.from_pandas(df2)

ui.run(show=False)

2023-08-14 12 05 09 localhost 59e593e7cf84

falkoschindler commented 1 year ago

Thanks for reporting this issue, @dashorty! I didn't even know that you can - in principle - use nested dictionaries for defining row data. But apparently we can't use column IDs with dots like in the following example:

ui.aggrid({
    'columnDefs': [{'headerName': 'First name', 'field': 'name.first'}],
    'rowData': [{'name.first': 'Alice'}],
})

That's because AG Grid tries to access rowData[0]['name']['first'] instead of rowData[0]['name.first'].

I'm not sure if this is a bug in NiceGUI or rather a limitation of AG Grid. Maybe NiceGUI should warn about column IDs containing dots? What do you think, @dashorty? Do you have an idea in mind how to approach this?

dashorty commented 1 year ago

I think a warning at runtime and/or a note in the documentation would be sufficient.

falkoschindler commented 1 year ago

Ok, commit c6ad97a adds such a warning. Commit 3f4f2cf adds a demo with complex objects in an AG Grid.

firai commented 1 year ago

According to https://stackoverflow.com/questions/58772051, it's possible to show field names with dots by suppressing Aggrid's parsing for nested structures

falkoschindler commented 1 year ago

Thanks for the hint, @firai! So should we simply set suppressFieldDotNotation instead of showing a warning?

firai commented 1 year ago

So should we simply set suppressFieldDotNotation instead of showing a warning?

It seems to me that having dots in the field names would be much more common than trying to display nested json structures, so I would think so. But as soon as I say that, I'm sure we'll get this: https://xkcd.com/1172/

falkoschindler commented 1 year ago

Ok, in 325dfcd I set the suppressFieldDotNotation option instead of showing a warning. That should work in basically all cases. I also added the possibility to pass additional options to the from_pandas method. So the user could overwrite this flag.

firai commented 1 year ago

Does the complex object demo that you added need to explicitly set the flag to false then?

falkoschindler commented 1 year ago

@firai I don't think so, because it doesn't use from_pandas.