ValueFormatter -- pass result of python function to scripts.js

Is there any way to pass a python variables other than the standard “params” to the ValueFormatter function? Better would be just passing it to the scripts.js somewhere, then accessing it from there, as the value never changes.

Update: I’ve verified that the variable that I am trying to pass in is not undefined.
However as soon as I send it through the ValueFormatter I get ziltch

dagfuncs.AmountFormatter = function(params, budgetList, filna="") {

    if (budgetList === undefined) {
        console.log('yes it is undefined lol')
    } else {
        console.log('yo sick')
    }

As an example, when I do this:

test = 'lol'
"valueFormatter": {"function": "AmountFormatter(params, test)"}

Console outputs undefined.

But when I do this:

"valueFormatter": {"function": "AmountFormatter(params, 'lol')"}

Console outputs undefined 50% of the time, and not undefined the other 50% of the time.
WTF is going on…

Thanks

Hello @daflamingpotato,

When using things like a valueFormatter or anything. You need to account for the params.value to not have a value, just do to the nature of how the renderers work. Sometimes the data isn’t available when first creating the div that holds the cell, etc.

If the data is being presented in the right manner, then it is correct.

This is being called after the function would normally return for NA values.
I’ll have to see if I can get a smaller example to show… just frustrating.


test = 'lol'
"valueFormatter": {"function": "AmountFormatter(params, test)"}

In the above example, the test variable is undefined on the JavaScript side.

If you want to pass a variable row by row, you should pass it in the data as an extra column and reference it via params.data.extraColumn

I found a workaround here:

In app.layout:
html.Div(id='myVar', **{'data-label': json.dumps(myVar)}),

Where ‘myVar’ is the python variable you want to access.
Then in scripts.py:

myVar_element = document.getElementById('myVar')
const data = myVar_element.getAttribute("data-label");
const myVar = JSON.parse(data);       
console.log(myVar) 

Thanks

Or, you could do this:


test = 'lol'
"valueFormatter": {"function": "AmountFormatter(params, “ + test + “)"}

:grin: