Merging images and dataframes togheter to download a report

Hi,

I’m trying to use the download button in order to generate a report in PDF or Excel, PDF would be preferred. However, I am using different datatypes; a dataframe, a matplotlib image and a plotly graphic object. Do you guys have any idea if this is possible? With rankings being the dataframe, spiderplot the matplotlib and stdv the plotly.

Thanks in advance!

This is my current code:
with st.sidebar:
with st.expander(‘Selecteer velden om een rapport te maken’):
st.checkbox(label=‘Rankings’, key=‘download_rankings’, value=True)
st.checkbox(label=‘Spiderplot’, key=‘download_spiderplot’, value=True)
st.checkbox(label=‘Standard deviations’, key=‘download_stdv’, value=True)

            session_state_elements = [elm for elm in st.session_state if 'download' in elm]
            
            with open('output.html', 'w', encoding='utf-8') as html_file:
                html_file.write(f'<h1>Report for {st.session_state.option}</h1>')
                for elm in session_state_elements:
                    if st.session_state[elm]:
                        if 'rankings' in elm:
                            html_file.write(filtered_ranking.to_html())
                        if 'spiderplot' in elm:
                            fig.to_html(html_file)
                            continue
                        if 'stdv' in elm:
                            hist.write_html(html_file)

                html_file.write('</body></html>')
                html_file.close()

                
            def dl_report():
                with open('output.html', 'r', encoding='utf-8') as html_file:
                    return html_file.read()

        st.download_button(label='Download rapport', data=dl_report(), file_name='report.pdf')

Hi @LennartVerbruggen

That’s a great question! At a conceptual level, I would approach this by first converting all files to the same file format (seems to be PDF in this specific use case) then merge them together into a single file, finally allowing the user to download the merged file.

Hope this helps!