Closed sachin-smart closed 3 years ago
Hello again @sachin-smart and thank you for the feature suggestions!
ipydatagrid
was designed to be extended using the widget ecosystem, and therefore it is probably not within scope to incorporate rendering logic based on data type - that logic is something users can implement using the existing Python language constructs.
To your point on using a single, default renderer for the grid - this exists and is actually well documented in the examples folder. All of ipydatagrid
's documentation is example based. We can also use basic Python to implement logic for selecting a different header renderer for each column based on its data type. See below for a short example which achieves that:
import pandas as pd
import numpy as np
import ipydatagrid as datagrid
data = pd.DataFrame({'Integer':[1,2,3,4,5],
'Float':[1.1, 2.2, 3.3, 4.4, 5.5],
'Text':'A B C D E'.split(),
'Dates':pd.date_range(start='2021-06-01', end='2021-06-05'),
'Mixed':[1, 2.2, 'C', np.datetime64('2021-05-05'), 5]})
default_renderer = datagrid.TextRenderer(background_color="salmon")
string_renderer = datagrid.TextRenderer(background_color="limegreen")
int_renderer = datagrid.TextRenderer(background_color="yellow")
dtype_renderers = {
np.dtype('int64'): int_renderer,
np.dtype('O'): string_renderer
}
column_renderers = {name:dtype_renderers[dtype] for (name, dtype) in data.dtypes.items() if dtype in dtype_renderers}
grid = datagrid.DataGrid(data,
default_renderer=default_renderer,
renderers=column_renderers,
layout={"height":"150px"})
grid
That's great. Much appreciated! And apologies, I did not see the full extent of examples or the usage of default_renderer. Cheers!
Is your feature request related to a problem? Please describe. Submitting a dict of TextRenderers for every column can be frustrating when many columns require the same renderer settings (as I imagine many people face). For example, in my case, I have several string type columns and many float type columns, but it is very manual to write out a dict for every column when only two different dict values are needed. The alternative I have come up with at the moment is to select the columns of one type by name from df.columns and use dict(zip(selected_columns, len(selected_columns)*[TextRenderer(...)])). While messy, it seems to work. But it is not robust to creation / deletion of columns and generally is very manual.
Describe the solution you'd like It would be wonderful to be able to input to the DataGrid call to use one renderer for each column dtype. E.g. use one for all columns of dtype string and another for all columns of float dtype.
Describe alternatives you've considered I suppose you could provide a single renderer for the entire grid (which I know is possible, but can't find any documentation on how). In this renderer, you would provide details on how to handle strings, floats, ints, etc as you desire and then as the renderer encounters each column, it can render appropriately. I'm not sure if this would be better. I think the best solutions would also continue to provide flexibility when needed to handle columns of the same dtype differently. E.g. it would be best if you could apply a certain rendering to all float type columns and then apply a different rendering to a specified float type column afterwards.