How to use two functions for clientside callbacks within js-files

Hi,
I am currently trying to put my clientside callbacks out of my app.py-file into .js-files and am receiving an error.

As long as I have only one function and one .js-file everything works fine. However I want to have more than one function to call by clientside callbacks.

I tried to have two .js-files and two clientside callbacks and received an error:

TypeError: dc[namespace][function_name] is undefined
    _callee2$ callbacks.ts:151
    tryCatch callbacks.ts:2
    _invoke callbacks.ts:2
    defineIteratorMethods callbacks.ts:2
    asyncGeneratorStep callbacks.ts:2
    _next callbacks.ts:2
    _asyncToGenerator callbacks.ts:2

File 1 ā€œfunction1.jsā€:

window.dash_clientside = Object.assign({}, window.dash_clientside, {
    clientside: {
		
		function1: function() {
			alert("first function");
		}	
    }
		
});

Callback 1:

app.clientside_callback(
    ClientsideFunction(
    namespace='clientside',
    function_name='function1'
    ),
    Output('dump-1', 'children'),
    Input('interval-1', 'n_intervals')
)

File 2 ā€œfunction2.jsā€:

window.dash_clientside = Object.assign({}, window.dash_clientside, {
    clientside: {
		
		function2: function() {
			alert("second function");
		}	
    }
		
});

Callback 2:

app.clientside_callback(
    ClientsideFunction(
    namespace='clientside',
    function_name='function2'
    ),
    Output('dump-2', 'children'),
    Input('interval-2', 'n_intervals')
)

Can someone tell me how to define multiple functions for clientside callbacks in .js-files?

Found a solution by myself. I put both my functions in one .js-file like this:

window.dash_clientside = Object.assign({}, window.dash_clientside, {
    clientside: {
		
		function1: function() {
			alert("first function");
		},
		
		function2: function() {
			alert("second function");
		}
	
    }
		
});
2 Likes

Are there any requirements for the .js file name, so the ClientsideFunction can find it?

@Isisgv does this help?

Hi @Isisgv , the name of the .js-file does not seem to matter. You have to call your functions by their name in order for them to work. In my case I got the following in my app.py:

==========================================================================
clientside callback 1
"""
app.clientside_callback(
    ClientsideFunction(
    namespace='clientside',
    function_name='function1'
    ),
    Output('dump1', 'children'),
    Input('interval-1', 'n_intervals')
)

"""
==========================================================================
clientside callback 2
"""
app.clientside_callback(
    ClientsideFunction(
    namespace='clientside',
    function_name='function2'
    ),
    Output('dump2', 'children'),
    Input('interval-2', 'n_intervals')
)

The clientside callbacks are triggered by dcc.Interval.

1 Like

I donā€™t know why, but my application cannot find the js functions i want to use in the clientside callbackā€¦

this is app.py

app.clientside_callback(
    ClientsideFunction(
    namespace='clientside',
    function_name='update_main_with_coarse'
    ),
    Output("plotly-resampler-graph", "id", allow_duplicate=True),
    Input("coarse-graph", "selectedData"),
    State("plotly-resampler-graph", "id"),
    prevent_initial_call=True,
)

and this is assets/coarse_fine.js:

window.dash_clientside = Object.assign({}, window.dash_clientside, {
    clientside: {
		update_main_with_coarse: function(selectedData, mainFigID) {
			alert("update after selectbox\n" + selectedData);
		}	
    }	
});

I saw in the documentation that JS files should be automatically included in dash apps, but my clientside_callback doesnā€™t seem to find it. it gives the following error:

Cannot read properties of undefined (reading ā€˜update_main_with_coarseā€™)

any idea what could be causing this?

Try removing the trailing comma.

It sadly didnt work, I have also tried changing the current working directory, moving the .js file into different folders, etcā€¦ nothing workedā€¦

There has just been a new release of dash fixing a bug concerning clientside functions https://github.com/plotly/dash/releases/tag/v2.9.3. Maybe updating dash will fix your issue.

1 Like

Yeah i guess it did! It works perfectly fine now!