How to inline raw bokeh js text in a <script> clause?

Corporate policy is not to down load any js files dynamically from outside the firewall. So I would like to inline the bokeh javascript files.
Alternatively, copy over and internally host each new version of the bokeh js files.

Python can fetch the javascript files text using JSResources

But when I try to put the text into a script clause, I can’t figure out a way to quote the text.
I get an python error UnicodeEncodeError( … 6707, 6708, ‘ordinal not in range(128)’

Here is an attempt with no quoting

from bokeh.resources import JSResources
bokeh_js_texts = JSResources(mode="inline").js_raw
bokeh_js_libs = '<script type="text/javascript"> ' + bokeh_js_texts[0] + ' </script>' 

@johnzzzzzzz More context is needed. Is this in the context of embedding Bokeh in e.g. a Flask app? Is this for standalone HTML files that Bokeh is generating directly? Are you saying that you are getting that error with only the code snippet above? it works for me, are you using Python 2?

A cgi-script is printing the string using python 3.8.
So the cgi-script is generating the html text piecemeal and the target text is print() into the cgi text stream.

@johnzzzzzzzYou will have to be more specific about what is actually throwing the error, then. If I just add print(bokeh_js_libs) to the above code, it works fine. Do you have a complete stack trace? As always, the best way to help others help you is to provide a Minimal Reproducible Example so that there does not have to be time spent on speculation and back-and-forth about what is actually occurring, or that code that is actually causing an issue.

Otherwise, all I can really do is point out is that the BokehJS source has non-ascii characters inside it. Your cgi sript will need to handle non-ascii content, but that is not really a Bokeh question, per se. E.g. if you are doing .encode('ascii') somewhere that definitely will never work and would have to change.

Sorry it was my bug. I mixed python 2.7 and python 3.8 code
This code works for python 3.8

texts = JSResources(mode="inline").js_raw
inline =  '<script type="text/javascript">' + texts[0] + '</script>'  # bokeh
inline += '<script type="text/javascript">' + texts[1] + '</script>'  # bokeh_widget
inline += '<script type="text/javascript">' + texts[2] + '</script>'  # bokeh_table
print(inline)
1 Like