I have an AgGrid component with monetary (floating) values in one column and the currency type (USD, EUR) in another column. I’ve been trying to use d3.formatLocale to use the data in the currency type column to correctly format the numerical value in the adjacent column. I made a Python dict similar to the one below that would take the currency type and return the correct formatter but how do I use this in a valueFormatter function?
In other words, this works:
“valueFormatter”: {“function”: f"{LOCALE[‘USD’]}.format(‘$,.2f’)(params.value)"},
but what I want to do is replace ‘USD’ with params.data.currency_type (currency_type is the column containing strings like ‘USD’ for each country). Is it possible?
Also: this is a databricks app and it will apparently not allow me to use an assets folder with JS code (I can create it of course but it’s ignored by the app code).
LOCALE = {
"USD":
“”"
d3.formatLocale({
"decimal": ".",
"thousands": ",",
"grouping": \[3\],
"currency": \["$", ""\]})
""",
“GBR”:
“”"
d3.formatLocale({
"decimal": ".",
"thousands": ",",
"grouping": \[3\],
"currency": \["£", ""\]})
"""
}
If chatgpt understands your post correctly, then
Databricks’ Ag-Grid accepts static JSON config, so your formatter code is treated as a string and never executed.
One workaround is pre-compute in python or whatever language could do the pre-compute then feed it in. Databricks just displays it as-is.
Hi @David_Bernstein and welcome to the Dash community 
If you can add a column to your data, you could add your locale definition there:
import dash_ag_grid as dag
from dash import Dash
import pandas as pd
import numpy as np
app = Dash()
currency_types = ['USD', 'EUR', 'GBP']
df = pd.DataFrame({
'value': np.random.uniform(-10000, 10000, size=15),
'currency': np.random.choice(currency_types, size=15)
})
locale_map = {
'USD': {
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["$", ""],
"nan": ""
},
'EUR': {
"decimal": ",",
"thousands": "\u00a0",
"grouping": [3],
"currency": ["", "\u00a0€"],
"percent": "\u202f%",
"nan": ""
},
'GBP': {
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["£", ""],
"nan": ""
}
}
df['locale_def'] = df['currency'].map(locale_map)
columnDefs = [
{
"field": "value",
"headerName": "Value",
"valueFormatter": {
"function": """
d3.formatLocale(params.data.locale_def).format('$,.2f')(params.value)
"""
},
},
{
"field": "currency",
"headerName": "Currency Type",
},
]
app.layout = dag.AgGrid(
columnDefs=columnDefs,
rowData=df.to_dict("records"),
)
if __name__ == "__main__":
app.run(debug=True)
@AnnMarieW Thank you! Works nicely.
1 Like