Generating Static HTML from server app

I have a bokeh server app where users upload a csv file and plots are generated using python code based on the data and some user inputs that are obtained through slider widgets.

If you click on the save icon a bunch of jpgs get downloaded. Is there a way to save a static html that you can manipulate using the bokeh tools? I want to be able to save an instance after the user has done all their manipulations to the data (through the sliders, etc.).

@mylesmoose Have you tried calling save on the plot? Offhand I can’t think of any reason that would not work.

@Bryan Does using output_file save the current state of the widgets and plot in the html? Like if I save a html after manipulating the plots, would the saved html file reflect the changed state or would it default to the state when the server app starts with?

You don’t need to call output_file with save, you can just pass save a filename, which is what I recommend in this scenario. And although I have not tried, I would expect the current state to be what is saved (I don’t know how anything else would be possible).

When I call save I get this message:
call to save() ignored when running scripts with the ‘bokeh’ command

Also is there a way to have the user set the filename and save destination? I was thinking you could do something through the fileinput widget, but am not really sure.

Ah, I forget about. Perhaps that restriction can be lifted or eased. It was put in place a long time ago under different circumstances. For now I would then suggest using file_html instead, to generate the HTML as a string which you can save using standard Python file I/O functions.

Also is there a way to have the user set the filename and save destination? I was thinking you could do something through the fileinput widget, but am not really sure.

Just by offering TextInput fields, I think. Built-in browser file choosers do not expose the actual file path for security reasons, only the chosen file contents are available. There is nothing we can do on our end about that.

Thanks Bryan,

Got it working.

I just did this to test, I’ll try and add the functionality to a widget in the future.
FYI doc = curdoc()

    if not os.path.exists('C:\CPT_Analysis'):
        os.makedirs('C:\CPT_Analysis')
    fname = str('\CPT_Analysis\plot'+sounding_text+'.html')
    file2html = file_html(doc,CDN,sounding_text)
    html_file = open(fname,"w+")
    html_file.write(file2html)
    html_file.close()

I just checked on my code and yes it does save the current state of the widgets and plot in the html.

2 Likes

I just checked on my code and yes it does save the current state of the widgets and plot in the html.

That’s great to know. Have to try it as well :smiley: