Saving the Plotly Chart as .png or .jpeg file format locally

Dear Team,

Wish you all a very Happy, Safe & Prosperous New Year!!

Trust you all are doing well!!

Recently, I was trying to add a PDF generator to one of my Streamlit App. I got stuck on the below part:

I have created an app that read the datframe and plot charts (plotly express charts) as the result. Below is the snippet.

Now, I want to store/save these charts as tempfile in the work folder to access for the next action that is PDF generator. So, I want to add these charts as Images inside HTML image-holders to create PDF report.

The PDF report generator example follows here (developed by Streamlit):
https://share.streamlit.io/streamlit/example-app-pdf-report/main

@AvratanuBiswas

Thanks in Advance!!

Hi @amrit ,

Just found this from the community,

Although, I haven’t tried it myself, but does this help in your case study ( for sure , you need to tweak the code further in your case ) ?

Best
Avra

1 Like

Dear Avra,
Thank you for your quick reply!!

The shared thread is about downloading the plotly chart by user. Where in my case, I want to automate the process of saving the plotly chart as .png in the same folder temporarily for next step use.

I will also look into the shared tread if anything can be tweaked for automation.

Thanks!!

Hello @amrit !

Could you share the snippet for using pdfkit to produce an html that reads images from a folder you’re trying to use? There might be 2 things to worry about:

  1. For static image export of plotly I usually go with Kaleido to write in tempfile locally so hopefully that should work for you too
import plotly.express as px
fig = px.scatter(px.data.iris(), x="sepal_length", y="sepal_width", color="species")
fig.write_image(<some_tempfile>, engine="kaleido")
  1. Depending on your code, the way you link to an image in your html template before reading with pdfkit could not work because the work folder is not accessible by the HTML file. This may be a thing to solve by for example saving the temp image in the Streamlit static folder (not sure we need to go as far as this, should depend on your code :wink: )

Have a nice day,
Fanilo :balloon:

2 Likes

Dear @andfanilo ,

Thanks for you reply and solution!!
However, I shifted from plotly to seaborn and I’m able to save the charts locally in my work folder.

BUT, as you already mentioned, FAILED to load the images into the template html file. I have spent the entire day looking for solution, but no help!!

Also, Im not able to implement the static folder access inside the code. Can you please help on this regard?

I’m using the streamlit app code to test. May I request you if its possible for you to show an example adding image into this same source code please:

https://share.streamlit.io/streamlit/example-app-pdf-report/main

Thanks in Advance!!