Hi,
I am currently working on a POC where I am trying to create a button that, when clicked, will trigger some data processing and then automatically download a CSV file to the user’s Downloads. However, I am facing a challenge in making this functionality work as intended.
Here is a code snippet that illustrates my current approach:
download_report = st.sidebar.button("Download Diagnosis Report")
if download_report:
conversation = format_to_conversation(conv_data)
diagnosis_report = get_summary(conversation)
data = {'Conversation': [conversation],
'Diagnosis Report': [diagnosis_report]}
df = pd.DataFrame(data)
csv = df.to_csv().encode('utf-8')
datetime_stamp = f"{datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}"
st.download_button(
label="Download data as CSV",
data=csv,
file_name=f'diagnosis_report_{datetime_stamp}.csv',
mime='text/csv',
)
As you can see, st.download_button renders a new button for downloading the CSV file. However, my goal is to perform the download when the download_report button is clicked, without the need for an additional button click.
I would greatly appreciate any guidance or suggestions on how to achieve this functionality
Yes certainly, you can proceed to use only the second instance of the st.download_button instead of nesting this inside the st.sidebar.download_button (thus delete this first instance).
Sorry, there was a mistake in the code snippet i provided, i have edited the code. I want to perform some processing as well as download the csv file on click of download_report button. Is there a way to achieve thta?
The st.download_button would perform 1 thing which is to download the provided file. However, if you’d like it to do some pre-processing (like generating the report) followed by downloading the report, then you’ll need a custom button.
Please see this previous post with example code snippet that you can customize for your use case:
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.