Dash AG Grid + DMC #467
Unanswered
AnnMarieW
asked this question in
Tips and Tricks
Replies: 2 comments
-
Cell Renderer with dmc.Button with DashIconify iconsThis is the example from the the Dash AG Grid Docs updated for dmc>=0.14.0 """
styling with custom cell renderer
Note:
Custom components must be defined in the dashAgGridComponentFunctions.js in assets folder.
"""
import json
import dash_ag_grid as dag
from dash import Dash, html, dcc, Input, Output, _dash_renderer
import pandas as pd
import dash_mantine_components as dmc
import dash_iconify
_dash_renderer._set_react_version("18.2.0")
data = {
"ticker": ["AAPL", "MSFT", "AMZN", "GOOGL"],
"company": ["Apple", "Microsoft", "Amazon", "Alphabet"],
"price": [154.99, 268.65, 100.47, 96.75],
"buy": ["Buy" for _ in range(4)],
"sell": ["Sell" for _ in range(4)],
"watch": ["Watch" for _ in range(4)],
}
df = pd.DataFrame(data)
columnDefs = [
{
"headerName": "Stock Ticker",
"field": "ticker",
},
{"headerName": "Company", "field": "company", "filter": True},
{
"headerName": "Last Close Price",
"type": "rightAligned",
"field": "price",
"valueFormatter": {"function": """d3.format("($,.2f")(params.value)"""},
},
{
"field": "buy",
"cellRenderer": "DMC_Button",
"cellRendererParams": {
"variant": "outline",
"leftIcon": "ic:baseline-shopping-cart",
"color": "green",
"radius": "xl"
},
},
{
"field": "sell",
"cellRenderer": "DMC_Button",
"cellRendererParams": {
"variant": "outline",
"leftIcon": "ic:baseline-shopping-cart",
"color": "red",
"radius": "xl"
},
},
{
"field": "watch",
"cellRenderer": "DMC_Button",
"cellRendererParams": {
"rightIcon": "ph:eye",
},
},
]
defaultColDef = {
"resizable": True,
"sortable": True,
"editable": False,
}
grid = dag.AgGrid(
id="custom-component-dmc-btn-grid",
columnDefs=columnDefs,
rowData=df.to_dict("records"),
columnSize="autoSize",
defaultColDef=defaultColDef,
)
app = Dash(__name__)
app.layout = dmc.MantineProvider(html.Div(
[
dcc.Markdown("Example of cellRenderer with dash-mantine-components Button and DashIconify icons"),
grid,
html.Div(id="custom-component-dmc-btn-value-changed"),
],
style={"margin": 20},
))
@app.callback(
Output("custom-component-dmc-btn-value-changed", "children"),
Input("custom-component-dmc-btn-grid", "cellRendererData"),
)
def showChange(n):
return json.dumps(n)
if __name__ == "__main__":
app.run_server(debug=True)
"""
Put the following in the dashAgGridComponentFunctions.js file in the assets folder
---------------
var dagcomponentfuncs = window.dashAgGridComponentFunctions = window.dashAgGridComponentFunctions || {};
dagcomponentfuncs.DMC_Button = function (props) {
const {setData, data} = props;
function onClick() {
setData();
}
let leftIcon, rightIcon;
if (props.leftIcon) {
leftIcon = React.createElement(window.dash_iconify.DashIconify, {
icon: props.leftIcon,
});
}
if (props.rightIcon) {
rightIcon = React.createElement(window.dash_iconify.DashIconify, {
icon: props.rightIcon,
});
}
return React.createElement(
window.dash_mantine_components.Button,
{
onClick,
variant: props.variant,
color: props.color,
leftSection: leftIcon,
rightSection: rightIcon,
radius: props.radius,
style: {
margin: props.margin,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
},
},
props.value
);
};
""" |
Beta Was this translation helpful? Give feedback.
0 replies
-
Cell Renderer - dmc.Card"""
styling with custom cell renderer - Cards
Note:
Custom components must be defined in the dashAgGridComponentFunctions.js in assets folder.
"""
import dash_ag_grid as dag
from dash import Dash, html, dcc, _dash_renderer
import pandas as pd
import dash_mantine_components as dmc
_dash_renderer._set_react_version("18.2.0")
data = {
"stats": ["Revenue", "EBITDA", "Net Income"],
"results": ['$13,120,000','$1,117,000', '$788,000'],
"change": [10.1, -22.1, 18.6],
'comments': ['', '', '']
}
df = pd.DataFrame(data)
columnDefs = [
{
"headerName": "",
"cellRenderer": "DMC_Card",
},
{
"field": "comments",
"editable": True,
"cellEditorPopup": True,
"cellEditor": "agLargeTextCellEditor",
"minWidth": 300,
},
]
grid = dag.AgGrid(
columnDefs=columnDefs,
rowData=df.to_dict("records"),
columnSize="autoSize",
dashGridOptions={"rowHeight": 100}
)
app = Dash(__name__)
app.layout = dmc.MantineProvider(html.Div(
[
dcc.Markdown("Example of cellRenderer with dash-mantine-components Card"),
grid,
],
style={"margin": 20},
), forceColorScheme="light")
if __name__ == "__main__":
app.run_server(debug=True)
"""
Put the following in the dashAgGridComponentFunctions.js file in the assets folder
---------------
var dagcomponentfuncs = window.dashAgGridComponentFunctions = window.dashAgGridComponentFunctions || {};
dagcomponentfuncs.DMC_Card = function (props) {
return React.createElement(
window.dash_mantine_components.Card,
{
withBorder:true
},
React.createElement(
window.dash_mantine_components.Text,
{c:"dimmed", tt: "uppercase", fw:700},
props.data.stats
),
React.createElement(
window.dash_mantine_components.Text,
{size: "lg"},
props.data.results
),
React.createElement(
window.dash_mantine_components.Text,
{size: "sm", c: (props.data.change > 0) ? "green" : "red"},
props.data.change + "%"
)
);
};
""" |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Cell Editors
How to use DMC V>=0.14 components as cell editors in Dash AG Grid
To use these features, place the JavaScript file containing the
AllFunctionalComponentEditors
function in the assets folder of your Dash project. You can then define the Mantine components in your app's column definitions (columnDefs), referencingAllFunctionalComponentEditors
as the cellEditor.Note - this app is using the Mantine Dark theme. The grid sets a dark theme using
className="ag-theme-alpine-dark"
Note that the dmc components are correctly styled with the dark theme. For a light theme, the grid's default ofag-thime-alpine
works well with the default Mantine light theme.See code in GitHub
Live Demo on PyCafe
Beta Was this translation helpful? Give feedback.
All reactions