I have a column within my dash ag-grid where the cell values are either an image or just text. When clicking on an image, I want to open that image within a modal as it is shown here. I am following this example to account for the different cell data types.
However, the callback is not fired when using cellRendererData as input property of the ag-grid.
Here is what I have tried so far:
import dash_ag_grid as dag
from dash import Dash, html, dcc, Input, Output, callback
import pandas as pd
import dash_bootstrap_components as dbc
webb_stephans_quintet = "https://user-images.githubusercontent.com/72614349/179115663-71578706-1ab5-45a5-b809-812c7c3028a7.jpg"
webb_deep_field = "https://user-images.githubusercontent.com/72614349/179115668-2630e3e4-3a9f-4c88-9494-3412e606450a.jpg"
webb_southern_nebula = "This is text"
webb_carina = "https://user-images.githubusercontent.com/72614349/179115673-15eaccb9-d17d-4667-84fb-e0a46fd444e8.jpg"
data_dict = {
"name": ["Deep Field", "Southern Nebula", "Stephans Quintet", "Carina Nebula"],
"img": [webb_deep_field, webb_southern_nebula, webb_stephans_quintet, webb_carina],
}
df = pd.DataFrame(data_dict)
columnDefs = [
{
"headerName": "Thumbnail or NOT?",
"field": "img",
"cellRendererSelector": {'function': 'imageORText(params)'},
"width": 100,
},
{
"headerName": "Image Name",
"field": "name",
},
]
grid = dag.AgGrid(
id="custom-component-img-grid",
columnDefs=columnDefs,
rowData=df.to_dict("records"),
dashGridOptions={"rowHeight": 100},
style={"height": 475},
columnSize="sizeToFit",
)
app = Dash(__name__, external_stylesheets=[dbc.themes.SPACELAB])
app.layout = html.Div(
[
dcc.Markdown(
"Example of cellRenderer with custom Image component. Click on Thumbnail to see full size Image"
),
grid,
dbc.Modal(id="custom-component-img-modal", size="xl"),
]
)
@callback(
Output("custom-component-img-modal", "is_open"),
Output("custom-component-img-modal", "children"),
Input("custom-component-img-grid", "cellRendererData"),
)
def show_change(data):
if data:
return True, html.Img(src=data["value"])
return False, None
if __name__ == "__main__":
app.run(debug=True)
"""
----------------------
// Place the following in the dashAgGridComponents.js file in the assets folder
var dagcomponentfuncs = window.dashAgGridComponentFunctions = window.dashAgGridComponentFunctions || {};
dagcomponentfuncs.ImgThumbnail = function (props) {
const {setData, data} = props;
function onClick() {
setData(props.value);
}
return React.createElement(
'div',
{
style: {
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
},
},
React.createElement(
'img',
{
onClick,
style: {width: '100%', height: 'auto'},
src: props.value,
},
)
);
}
-----------------------------------------
// Place the following in the dashAgGridFunctions.js file in the assets folder
var dagfuncs = window.dashAgGridFunctions = window.dashAgGridFunctions || {};
dagfuncs.imageORText = function(params) {
var dagcomponentfuncs = window.dashAgGridComponentFunctions;
const imageDetails = {
component: dagcomponentfuncs.ImgThumbnail,
};
if (params.data && params.data.img && params.data.img.includes('.jpg')) {
return imageDetails;
}
return undefined; // No renderer, let ag-Grid handle default rendering
};
"""
I am using Python 3.12.1, dash 2.18.1 and dash_ag_grid 31.2.0.