jupyter-widgets / ipydatagrid

Fast Datagrid widget for the Jupyter Notebook and JupyterLab
BSD 3-Clause "New" or "Revised" License
579 stars 51 forks source link

map a user-facing title to a backend name #450

Open jgunstone opened 1 year ago

jgunstone commented 1 year ago

Is your feature request related to a problem? Please describe. often when developing short-hand names are used, they don't contain spaces and are selected for ease of writing code. when displaying results to a user the backend names are often mapped to user facing titles. it would be great if ipydatagrid facilitated this.

Describe the solution you'd like in ipyautoui we implemented something that does this - ipyautoui requires a schema which gives the user the opportunity to define a title. we override DataGrid with AutoGrid and in the __init__ we check if titles are present and update the dataframe before setting https://github.com/maxfordham/ipyautoui/blob/782955ed379cd2b4f53b0ec0de74557813c2e80e/src/ipyautoui/custom/autogrid.py#L667-L696 something like this: https://github.com/maxfordham/ipyautoui/blob/782955ed379cd2b4f53b0ec0de74557813c2e80e/src/ipyautoui/custom/autogrid.py#L432-L438

in autoui we care mostly about the jsonable value so we have a records property which does the mapping back to name rather than title.

it would be good if we could unpick some of this functionality and add it to ipydatagrid

Describe alternatives you've considered when I looked at this again i thought that it might actually be better to use renderers rather than actually changing the underlying data

i.e.

import pandas as pd
from ipydatagrid import DataGrid, TextRenderer, Expr

def map_name(cell):
    map_ = {'a': 'Title A', 'b': 'Title B'}
    return map_[cell.value]

df = pd.DataFrame({"a":[1,2], "b": [3,4]})
hr = TextRenderer(text_value=Expr(map_name))
gr = DataGrid(df, header_renderer=hr)
gr

image

I could imagine adding a map_name_title trait which automatically applies this renderer on_change... ? this is good as you don't need to change the underlying dataframe. the issues I see with this are:

Additional context I was having a poke around thinking about where this could be best added to ipydatagrid... pandas defines the schema like this: https://specs.frictionlessdata.io/table-schema/ which explicitly supports a title (and description which would be great as a column heading tooltip!) but unfortunately i don't think there is a way of add these metadata fields to a pandas dataframe object.

the main issue that i see is that the setting of the dataframe is the only required field. setting the dataframe happens first (before other traits are set) : https://github.com/bloomberg/ipydatagrid/blob/c7cf3a48756264db92218ac40a0d5417a078dc9a/ipydatagrid/datagrid.py#L372-L380 this then creates the schema: https://github.com/bloomberg/ipydatagrid/blob/c7cf3a48756264db92218ac40a0d5417a078dc9a/ipydatagrid/datagrid.py#L441-L467

I think it would be good if the schema was also a trait - if not given it is defined by the dataframe, otherwise it can be used to add additional metadata about the dataframe... with some logic to ensure the 2 things are consistent