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.
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"):
your_scraping_function
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: https://xlsxwriter.readthedocs.io/working_with_pandas.html
writer.save()
return output.getvalue()
st.download_button(
label="download as Excel-file",
data=convert_to_excel(df),
file_name=awesome_data.xlsx,
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
key="excel_download",
)
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.