Unable to run Plotly Kaleido on Streamlit Cloud to generate image exports from graphs

Hello.

I’ve been attempting to use Plotly’s Kaleido to generate graph exports as images on the Streamlit Cloud backend but have been unable to get it to work, initially solved an issue due to no access to /tmp with the below:

import plotly.io as pio
pio.kaleido.scope.chromium_args = tuple([arg for arg in pio.kaleido.scope.chromium_args if arg != "--disable-dev-shm-usage"])

However now that I’ve jumped that hurdle it seems the page just hangs with ‘Running…’ whenever I try to generate an image like below:

fig_img = fig.to_image(format='png')
st.image(fig_img)

It seems Streamlit Cloud may not be set up correctly to support the chromium process that Kaleido uses, which is rather disappointing seeing as the built in st.plotly_chart already provides some of the functionality but does not expose enough to allow getting the image in the python backend.

I explored trying various chromium command line arguments to no avail, as there are far too many for me to know what may or may not solve the issue.

Has anyone been able to find a solution to this? Or able to suggest one?

Hi @blipk, welcome to the Streamlit community!

Could you elaborate more on the actual use-case you are trying to solve? Specifically, why export the picture to PNG, then load it as an image, than to just plot it as a Javascript visualization?

Best,
Randy

That’s just an example @randyzwitch

The real use case is to get the graph image into a word document using python-docx.
There are plenty of other uses for having the graphs exportable as well.

I am also having problems with kaleido. I wanted to use it to allow users to download the plotly graph being displayed on the app.The idea was to pass the result of fig.to_image() to st.download_button().

Surprisingly it works correctly when i run the app locally but not once it is hosted on streamlit cloud…

Can you share a link to your github project?

@florianbrnrd Yes, it’s an issue with streamlit cloud, and there’s no info in the log so really not sure whats happening. Would need some more info from @randyzwitch to know what may be able to get the process running.

@Franky1 It’s a private project, all the relevant code is in my original post.

I had success with explicitly setting these chromium options for kaleido on Streamlit Cloud:

# set explicit headless parameters for chromium (not sure if all are needed)
    pio.kaleido.scope.chromium_args = (
        "--headless",
        "--no-sandbox",
        "--single-process",
        "--disable-gpu"
    )  # tuple with chromium args
  • install the latest versions of plotly and kaleido
  • don’t forget a “reboot” of streamlit app, that sometimes works wonders :wink:
1 Like

Hey @Franky1

Looks like the streamlit dev team has changed something on their end, as it’s now working flawlessly with my original code.

Actually, I spoke too soon.

Initially it worked, but seems its a 50/50 chance of working (or getting stuck into a ‘Running…’ state) after rebooting, perhaps they are still working on something back there?

I also tried with your chromium arguments and same issue.

Perhaps the bug is reintroduced when I try to get it into a word document (although it still sometimes happened when I removed this):

from docx import Document
from docx.shared import Inches
document = Document()
document.add_picture(io.BytesIO(fig_img), width=Inches(5.5))

This is working fine with an export from a graphviz graph:

graph = graphviz.Digraph(engine='dot')
graph_image = graph.pipe(format='png')
document.add_picture(io.BytesIO(graph_image), width=Inches(5.5))

For me it looks like kaleido is conflicting with the app when it is starting (getting stuck at the “app is in the oven” stage).

If i comment the function containing fig.to_image(format='png') then the app starts correctly. I then did a push to uncomment the function and it started working as intended…