Generate SVG images from Bokeh Plots and include them in a PDF Report with using "svglib" and "reportlab"

@Mark_Murphy To return string buffers instead of writing into file, I think with the following code would be enough (I have added a new boolean parameter). I tested it with a couple of examples and it worked:

def get_svgs(obj, driver=None, timeout=5, buffer=False, **kwargs):
    with _tmp_html() as tmp:
        html = get_layout_html(obj, **kwargs)
        with io.open(tmp.path, mode="w", encoding="utf-8") as file:
            file.write(decode_utf8(html))
        web_driver = driver if driver is not None else webdriver_control.get()
        web_driver.get("file:///" + tmp.path)
        wait_until_render_complete(web_driver, timeout)
        svgs = web_driver.execute_script(_SVG_SCRIPT)

    if buffer:
        buffer_list = [StringIO(x) for x in svgs]
        return buffer_list
    else:
        return svgs

And I had to change the code in my example a little bit to make it work with buffers:

svgs = get_svgs(p, buffer=True)
return svg2rlg(svgs[0])

I can do the PR if it is not already done (also adding the docstring)

1 Like