jupyter-widgets / ipydatagrid

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

Add support for corner header renderer #252

Closed ibdafna closed 3 years ago

ibdafna commented 3 years ago

Signed-off-by: Itay Dafna i.b.dafna@gmail.com

This PR adds support for corner header renderers via a corner_renderer traitlet. The current API behaviour is not changed - users who do not pass to the constructor or set the corner_renderer property on the DataGrid object, but do pass header_renderer will see the header renderer applied to the corner header too. If they do set a corner_renderer property then it will will override the header renderer - only applies to the corner header!

image

import ipydatagrid as grid
import numpy as np
import pandas as pd

np.random.seed(565)
columns = [f'Col {x}' for x in "A B C D E F".split()]
index = [f'Row {x}' for x in range(1, 11)]
data = np.round(np.random.random((10, 6)), 2)
df = pd.DataFrame(index=index, columns=columns, data=data)

corner_renderer = grid.TextRenderer(
    background_color="yellow"
)
header_renderer = grid.TextRenderer(
    background_color="green"
)

index_rend = grid.TextRenderer(
    background_color='lawngreen'
)

g = grid.DataGrid(df, layout={'height':'250px'},
                  corner_renderer=corner_renderer,
                  header_renderer=header_renderer,
                  renderers={
                      'index':index_rend
                  }
                 )
g
ibdafna commented 3 years ago

Not ready for review yet: there are some quirks with stale colours applied to header renderers when changing styles or setting the header renderer to None. Working on that.

ibdafna commented 3 years ago

Tested everything is working as expected. Also squeezed in a bugfix unrelated to this PR where setting a header_renderer initially, but unsetting by by setting header_renderer=False does not actually remove the renderer.

I also updated one of the tests which failed due to changes in the logic - also tested everything is working as expected data-wise including sorting, filtering and changing the data set. @kaiayoung would be good if you could take a look too, just in case I missed something.

EDIT: the test I edited is from a change unrelated to this PR (pre 1.0.7), which was not detected by the CI for some reason.