jupyter-widgets / ipydatagrid

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

Identify column by name in renderer rather than index. #487

Open jobinnthomas opened 8 months ago

jobinnthomas commented 8 months ago

In below code example, is there a way to do something where based on the cell column name, something is done. In the example below the column index is used.

I need to identify if a certain column name is present and do something. I dont want to do it by column index. If you could also describe how I could look this up myself in the code, that would help me in future.

How can i troubleshoot these vegaexpression in ipydatagrid. Right now, I have no way to setup a breakpoint or print anything. Could you give some guidance on this?

My psudo-code below (wouldnt work but i need something similar)

renderer = TextRenderer(
    background_color=VegaExpr(
        "cell.colum_name == 'column 1' ? 'limegreen' : 'pink'"
    )

Example (that runs fine)


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

df = pd.DataFrame(
    {
        "column 1": [{"key": 11}, ["berry", "apple", "cherry"]],
        "column 2": [["berry", "berry", "cherry"], {"key": 10}],
    }
)

renderer = TextRenderer(
    background_color=VegaExpr(
        "cell.value[1] == 'berry' && cell.metadata.data['column 1']['key'] == 11 ? 'limegreen' : 'pink'"
    )
)

DataGrid(
    df,
    layout={"height": "100px"},
    base_column_size=150,
    default_renderer=renderer,
)