Using external JS libraries via webTemplate folder

Hi everyone,
I am trying to make an external JavaScripit available in the Knime server, following the instruction below from the Knime Server Administration Guide:

In order to change behavior, add additional libraries or define JavaScript functions to be available for example to the JavaScript enabled nodes, place a JS file called knime_template.js into the templates folder.

I have placed my .js file under the folder indicated (webportalTemplate), however I cannot find it afterwards when using it in a Generic JavaScript View (via require, it gives error 404 message). Is there more information available on how to make use of the .js files included in this wbportalTemplate folder?

Thank you,
Matheus

Hi,
The way I understand this the knime_template.js is loaded into the page and no require is needed. You can just define your functions there and they will be available as global function in your Generic JavaScript View. However, I have never tried it myself.
Kind regards,
Alexander

Hi Alexander, thanks for the response. Does it mean if I want to use more than one library I need to add their content to the knime_template.js file? Do you know if there is someone in Knime that has an example workflow?

Hi,
I think there is no example workflow because only the workflow is not enough. You also need the knime_template.js in the correct location. You also need to navigate out of the nested iFrames to access the global variables: https://stackoverflow.com/questions/7647042/sharing-global-javascript-variable-of-a-page-with-an-iframe-within-that-page.
But you can put as many libraries as you like into the knime_template.js file.
Kind regards,
Alexander

Hi Alexander,
It seems that including the content of the library in the knime_template.js did not work. Just so you know, I got an error after the login (when the main page was being loaded), which for me indicates that it has to do with the loading of the file (and not my workflow).

require.js:5 Uncaught Error: Mismatched anonymous define() module: function(){return ...

For reference, I am trying to use the Mermaid JS library

Is this something we can check via the support?

Hi,
I would also not recommend doing it that way. It makes the workflow almost impossible to debug when it is running somewhere else than the KNIME Server. I have attached a workflow that downloads the library code first and then passes it as a flow variable into the Generic JS View. You have to hack around with module.exports a bit, but it works. I would suggest you add some caching for the library so it is not retrieved every time you run the workflow. You can use a table writer to write the output of the Binary Object to String node into a table file and then when the workflow runs check if that file exists and load from there. If it does not exist, you use the logic I put in the workflow. You can use a List Files in conjunction with a Empty Table Switch to have those two branches.
Kind regards,
Alexander

mermaid.knwf (388.9 KB)

1 Like

Hi Alexander,
The workflow that you sent is very helpful and I think a much better solution than having the library in the server. I adapted slightly so now the content of the JS comes from a variable and not from the GET request. It works well both in the platform and in the server .Thank you very much for this!
Regards,
Matheus

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.