Problem creating button to save profile

Hi everyone

I am creating an application using Streamlit and Pandas-Profile, but I have a problem in my final part, I created a button that its functions to export a profile in. HTML but when I click on the button this does nothing. I don’t find the error, because it doesn´t show me an error, this is the part of the code:

if mode == ‘complete’:
profile = ProfileReport(
df,
title=“Dataframe report”, # Title
progress_bar=True, # to indicate progress
dataset={
“description”: ‘Generating Insights’})

# Buttom to generate file
if st.button("Save Profile Report"):
    profile_path = "profile_report.html"
    profile.to_file(profile_path)
    st.success("Profile report saved successfully!")

I will be so grateful to them, for your collaboration. Have a nice day!

1 Like

Hi @Miguelmag

The st.button works without an error in generating the html file, however what you want may be to download the generated html file.

Thus, you may want to use st.download_button instead of st.button.

Please see the Docs page for example code snippet:

Hope this helps!

1 Like

Hi @dataprofessor thanks for your answer, I used this tool and I got the solution, but I have a question about it, it’s mandatory to use or export a file for example, in this case, I exported a file HTML, and took this file to used to download when the person click on the download button. This is my solution using the download button:

    profile.to_file("profile_report.html")   
    def download_profile():
        profile.to_file("profile_report.html")
    with open("profile_report.html", "rb") as file:
        btn = st.download_button(
            label="Download Profile Report",
            data=file,
            file_name="profile_report.html",
            mime="text/html"
        )
    download_profile()

Is there another possibility that doesn’t include generating files before download?

Thanks once again for your response and assistance!

Hey @Miguelmag . Recently I have used the following logic for downloading. May be it helps for your reference.

# Function to create a download link for a DataFrame as CSV
def get_table_download_link(df):
    csv = df.to_csv(index=False)
    b64 = base64.b64encode(csv.encode()).decode()
    href = f'<a href="data:file/csv;base64,{b64}" download="answers.csv">Download Answers as CSV</a>'
    return href

st.markdown(get_table_download_link(percentage_message), unsafe_allow_html=True)
2 Likes

Hi @Miguelmag

Adjusting the example code snippet from the above mentioned Docs page (st.download_button - Streamlit Docs) gives the following which allows the generation of the profile report upon button click:

def download_profile(input_profile):
   return input_profile.to_file()

file = download_profile(profile)

st.download_button(
            label="Download Profile Report",
            data=file,
            file_name="profile_report.html",
            mime="text/html"
)
3 Likes

Hi @dataprofessor I tried your solution, and I got good performance with this, but I had a little problem because always my code generated a file before downloading, and trying to find the solution I messed up my code, after that I wrote a new code using this functions, and I sorted it out, now the user can download the profile in json and my code generates a temporal file:

  1. download profile fiction:

    def download_profile(profile, output_file=“profile_report.html”):
    profile.to_file(output_file)
    with open(output_file, “rb”) as file:
    profile_bytes = file.read()
    return profile_bytes

  2. dele_file function:

    def delete_file(file_path):
    try:
    os.remove(file_path)
    print(f"Archivo ‘{file_path}’ removed successfully.“)
    except FileNotFoundError:
    print(f"El archivo ‘{file_path}’ It does not exist.”)
    except PermissionError:
    print(f"You do not have permission to delete the file ‘{file_path}’.“)
    except Exception as e:
    print(f"An error occurred while deleting the file ‘{file_path}’: {e}”)

and then I applied in my code flow :

file = download_profile(profile, output_file=“profile_report.html”)

    st.download_button(
        label="Download Profile Report",
        data=file,
        file_name="profile_report.html",
        mime="text/html"
        )   

    delete_file('profile_report.html')

The primary objective is to remove the main file from my source folder, which is intended for downloading. This occurs once the user of the application downloads the file by clicking on the download button.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.