Hi!
I’m trying to figure out the [best] way to download my plotly figures saved in session state with the download button.
I do a similar thing with saved data frames for which I have the following code:
@st.cache
def save_df(df: pandas.DataFrame):
return df.to_csv(sep='\t', index=False).encode('utf-8')
# some app code here
if not st.session_state.processed_data and not st.session_state.final_feature_table:
st.warning('Process data!')
else:
files = {
"Feature dataframe": st.session_state.final_feature_table[0],
"Processed dataframe": st.session_state.processed_data[0]
}
available_files = [key for (key, value) in files.items()]
file_name = st.text_input('', value='your_filename')
selected_file = st.selectbox('Select a file for download:', available_files, index=0)
file = save_df(files[selected_file])
st.download_button(
label="Download table",
data=file,
file_name=f'{file_name}.tab',
mime='text/csv'
)
This works like a charm.
Now, I generate figures using a form, resetting the state after each re-submission. I check and it works outside of the form using st.plotly_chart.
The problem occurs when I try to save a static figure to a PDF file using the following code:
@st.cache
def save_figure(figure: go.Figure, file):
return figure.write_image(file, format='pdf')
# this is outside of the form
if not st.session_state.figures:
st.warning('Generate figures!')
else:
st.markdown('## Download Figures')
available_figures = [key for (key, value) in st.session_state.figures.items()]
file_name = st.text_input('', value='your_filename')
selected_figure = st.selectbox('Select a figure for download:', available_figures, index=0)
figure_object = st.session_state.figures[selected_figure] # I can see this figure with
save_figure = save_figure(figure_object, file_name)
btn = st.download_button(
label="Download image",
data=save_figure,
file_name=f"{file_name}.pdf",
mime="figure/pdf")
I get the this error:
RuntimeError: Invalid binary data format: <class 'NoneType'>
I’m guessing it’s something trivial that I’m missing but I just cannot figure out what it is!
Thanks,
Simon