Download button data parameter issues

Hi,

Im trying to make a generate report button. Using st.download_button, I want so that on click I can run some expensive code that then generates a pdf file to download. As of now this “expensive pdf generation code” to produce data to download has to be done decoupled from the button.

Having it decoupled, it runs everytime the app is changed in terms of interaction (caching does not solve the issue) making user experience really slow.

Is there a way to make the data param wait? Or be able to pass a function that it will only run on click.

2 Likes

Create a button to generate report, once the report is generated create a download button.

import streamlit as st


def generate_report(repfn):
    with open(repfn, 'w') as f:
        f.write('Report')
    st.write('done report generation')


if st.button('generate report'):
    repfn = 'report.pdf'
    generate_report(repfn)

    with open(repfn, "rb") as f:
        st.download_button(
            label="Download report",
            data=f,
            file_name=repfn)

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