Dear all,
I would like to have a double click event on a cell of a Dash AgGrid (grid-data in the example) to trigger two events:
- a change in a mask matrix (grid-flag in the example) => this mask matrix will then be used to perform some calculations
- the updated mask matrix will then be used to color the background of the first dataframe (grid-data depending whether grid-flag is True or False).
I managed to do the first part but I don’t find a way to apply a cell style depending on True/False from another dataframe (the shape/index/columns will be exactly the same). (I tried both callback and clientside_callback withtout success…)
I made a simplified example of the source code I have.
import dash
from dash import Dash, html, dcc, Input, Output, State, callback, clientside_callback, page_container
import dash_ag_grid as dag
import pandas as pd
import json
import os
try:
parent_dir = os.path.dirname(os.path.abspath(__file__))
data_absolute_path = os.path.join(parent_dir, "data", "solar.csv")
except:
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/solar.csv')
app = Dash(__name__, title="Test Dash AgGrid")
df_data = pd.read_csv(data_absolute_path, decimal=".", sep=",", index_col=0)
df_flag = df_data.copy()
df_flag[df_flag > 0] = True
grid_data = dag.AgGrid(
id="grid-data",
rowData=df_data.to_dict("records"),
columnDefs=[{"field": i} for i in df_data.columns],
)
grid_flag = dag.AgGrid(
id="grid-flag",
rowData=df_flag.to_dict("records"),
columnDefs=[{"field": i} for i in df_flag.columns],
)
app.layout = html.Div(
children=[
grid_data,
grid_flag,
html.A(id="grid-text"),
],
)
@callback(
Output("grid-text", "children"),
Output("grid-flag", "rowData"),
Input("grid-data", "cellDoubleClicked"),
)
def display_cell_double_clicked_on(cell):
if cell:
row = cell['rowIndex']
col = cell['colId']
df_flag.at[df_flag.index[row], col] = not df_flag.at[df_flag.index[row], col]
message = f"Double-clicked on cell:\n{json.dumps(cell, indent=2)}"
else:
message = "Double-click on a cell"
return message, df_flag.to_dict("records")
if __name__ == '__main__':
app.run(debug=True)
Thank you!

