bokehpdfkitpython-pdfkit

Bokeh autoload_static still interactive


I'm assuming I'm doing something wrong as when I attempt to use Bokeh's 'autoload_static' function and place the script tag in my html file the graph is still interactive? In addition to this, the output of my script tag (by autoload static) doesn't look exactly the same as tutorial despite it being the same code...

Would really appreciate the help. I'm trying to output it as static so I can correctly convert it to pdf with pdfkit - which unfortunately doesn't work with interactive graphs.

Thanks!

Html :

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Bokeh Scatter Plots</title>

        <!-- <link rel="stylesheet" href="http://cdn.bokeh.org/bokeh/release/bokeh-0.12.7.min.css" type="text/css" /> -->
        <!-- <script type="text/javascript" src="http://cdn.bokeh.org/bokeh/release/bokeh-0.12.7.min.js"></script> -->

    </head>
    <body>


<script
    src="js-outputted-by-autoload_static"
    id="f9632bd4-873b-4c08-a4ad-c8a997873430"
    data-bokeh-model-id="bec3e18b-71d0-4d3d-9d6a-0079d8fc6082"
    data-bokeh-doc-id="b39e1b50-1e37-4062-92a8-888cc4424328"
></script>

</html>

Bokeh:

from bokeh.resources import CDN
from bokeh.plotting import figure
from bokeh.embed import autoload_static

plot = figure()
plot.circle([1,2], [3,4])

js, tag = autoload_static(plot, CDN, "js-outputted-by-autoload_static")

Solution

  • autoload_static is not for generating images, it is for generating JavaScript files that can embed standard interactive Bokeh plots in web pages. The "static" part refers to the fact that these plots are not backed by a Bokeh server.

    Since autoload_static still generates JavaScript to render onto an HTML canvas, I doubt it will be useful at all with pdfkit (which I assume cannot do anything with JS code).

    If you want to create images (e.g. PNGs) of Bokeh plots, you should look at the Exporting Plots section of the User's Guide. With recent versions of Bokeh, you can do e.g.

    from bokeh.io import export_png
    
    export_png(plot, filename="plot.png")
    

    to generate a PNG (which presumably is what pdfkit can handle). Some optional dependencies are required to be installed to use this functionality, the linked User's Guide has all the information.