Calling external javascript functions in clientside callbacks

Hi,

I’ve been playing around with clientside callbacks today (to hopefully attach to wasm runtime). However, importing external javascript in any of my files in /assets will cause the following error:

Cannot read properties of undefined (reading ‘*’)

where * is the name of the js-defined callback. Is there a possibility to statically (once) import js-functions from other files and call them from the clientside callback function?

My /assets/custom-script.js looks like

import * as external from "./external"

window.dash_clientside = Object.assign({}, window.dash_clientside, {
    clientside: {
        call_greeting: function(name) {
            return external.greet(name);
        }
    }
});

Dynamic imports almost works though (allowing promised values as of Dash 2.4?)

window.dash_clientside.clientside = {
    call_greeting: function(name) {
        return import("./external").then((module) => {
            return module.greet(name)
        })
    }
}

However, using such dynamic import, the wasm runtime would somehow be started at each function call, which probably would be excessive?

Thanks!

1 Like

I’m also looking for a solution to this, have you found something? I have a few things I’d like to use in my clientside callbacks, but i currently don’t know how to. Also, do you know if i have to import Plotly? I want to use Plotly.relayout for my application.

Hello @Isisgv,

It is possible to import the javascript files as modules, what you need to do is create a static directly, then load the file into that directly.

Then in the app, declare the external scripts like so:

external_scripts=[{'src':'../static/mycustom.js','type':'module'}]

This will import it as a module and not a regular script. :slight_smile:

When using Dash and a dcc.Graph, you do not need to import Plotly, as it is already included. :slight_smile:

1 Like

Is this also possible with other external resources like scripts and functions i havent made myself,but which can be imported in your usual js scripts?

@Isisgv,

Yes, any scripts can be imported. Just be sure that you trust the source.

1 Like

great! thank you :))