How to download profile_report() from Pandas Profiling?

Greetings,
I am working on a mini project wherein I ask the users to upload a CSV file. I performed some basic EDA and visuals, I also made use of the Pandas Profiling in order to generate the profile report. I want to create a download button which on clicking allows the users to download the profile report.
The code I have tried is :

if dataset is not None:
                df = pd.read_csv(dataset , delimiter = ",")
                st.dataframe(df)
                pr = df.profile_report()
                st_profile_report(pr)       
                export=pr.to_file(b"Analysis.html")
                st.download_button(label="Download Full Report", data=export,file_name='Profile Report')

Sadly I don’t find this working ( I am using Streamlit version 1.2.0). Requesting help from the community to find a workaround to achieve the above.
Thanks in advance.

Hello @Sai_Ram_k, welcome to the community!

The st.download_button expects data but I think the pr.to_file() only creates the file and does not return any html data back to Python. There’s a .to_html() that does the trick though:

export=pr.to_html()
st.download_button(label="Download Full Report", data=export, file_name='report.html')

Happy Streamlitin’ :balloon:
Fanilo

3 Likes

Hello @andfanilo , Thanks a lot.
The to_html() worked like a charm. I learnt that I need to explore the documentation more, I will keep it in mind next time.
Thank you.