Get data only at a click of a button?

Hello! I’m a beginner programmer and i’m doing a project with Streamlit. I have two questions about what is possible to accomplish with the tool.

My project has some functions that scrape data from a web page and display it as a table. It is working the way I expect it to, but I would like to add some things.

The first question is: can I keep the data I display cached and only refresh it after the user presses a specific button? Today the application scraps it every time the project is reloaded.

The second is: can I, from the scraping functions I’m using, store this data in an external platform from that button click? For example: user presses a button, then I scrape the data, show to the user and then I store that data in a spreadsheet.


Hi there,

yes, you can do stuff only after pressing a button. Just create a button like this

st.button(label="scrape stuff", key="btn_scrape")

and then put everything it’s supposed to do behind a line like this:

if st.session_state.get("btn_scrape"):

Now it only does stuff after you click the button.

For downloading things, there’s a special download button widget. Check out the docu here: download button
For example if you have a pandas data frame and want to download it as an Excel-file, it could look something like this:

from io import BytesIO
import pandas as pd
import streamlit as st

def convert_to_excel(df):
    output = BytesIO()
    writer = pd.ExcelWriter(output, engine="xlsxwriter")
    df.to_excel(writer, sheet_name="scraped data")

    # you can do all sorts of formating here 
    # see:
    return output.getvalue()

    label="download as Excel-file",
1 Like