Download Holoview graph

Hi, I am trying to build an application with react and flask. One of the functionality is to download this holoview graph as a png, the issue is that I am building a customized save button instead of using the save button embedded with graph.

To download the image, I need something like a base-64/blob version of this image, I am wondering if there are any methods that can help me convert a holoview chart to a base64 string in python.

On the Python side you can hv.save the file. For example using the matplotlib backend and to .svg. Or Bokeh and .png. Then you can transfer the result over your REST API to the frontend.

You can use something along the lines of

import hvplot.pandas
import pandas as pd
import holoviews as hv
import base64
import pathlib

data = pd.DataFrame({"x": [1,2,3,4]})
plot = data.hvplot()

file=pathlib.Path("out.svg")
hv.save(plot, filename=file, backend="matplotlib")
svg = file.read_text()
print(svg)
svg_64_encode = base64.b64encode(svg.encode())
print(svg_64_encode)

You should make it faster and more robust using BytesIO or StringIO instead of saving to a file.

For more info check out the documentation of hv.save Exporting and Archiving.

Hi Marc, thank you for your wonderful response, yes, I do look for a solution that doesn’t need to download and read the image again, like what you mention, the BytesIO solution, Is it possible that you can just show me how I might convert a hvplot a BytesIO or StringIO with concrete code example