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!
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)
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:
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:
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
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}”)
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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.