Hello you lovley people,
I build a small app for people to play around with their own data on a conditional generation task. Itβs used only by internal trusted users so security is of no concern to me. The user will upload a file, get their generations and in the end they should be able to download the results as a csv file. I want to prevent the conversion to happen each time the app reloads. I am aware of @chach_data, but since the df changes each time a new result is appended the conversion happens. So in a very minimal example, how can I make it that convert_df is only run when the download button is clicked.
import pandas as pd
import streamlit as st
if 'df' not in st.session_state:
st.session_state.df = pd.DataFrame()
@st.cache_data
def convert_df(df):
print('happend')
return df.to_csv().encode('utf-8')
def do():
print('done')
st.session_state.df = pd.concat([st.session_state.df, pd.Series([1,2,3]).T])
print('done')
data = convert_df(st.session_state.df)
st.button('Do', on_click=do)
st.download_button("Download", data=data, file_name='res.csv', mime='csv')
At the moment Iβm using a workaround. I have two variables that I store in session state, one stores the data while the users are running the app and one which contains an empty df. I then added a second button that copies the df to the variable that will be exported. But Iβm sure there is a better way, right?
if 'df' not in st.session_state:
st.session_state.df = pd.DataFrame()
if 'df_export' not in st.session_state:
st.session_state.df_export = pd.DataFrame()
@st.cache_data
def convert_df(df):
return df.to_csv().encode('utf-8')
def do():
st.session_state.df = pd.concat([st.session_state.df, pd.Series([1,2,3]).T])
print('done')
def save():
st.session_state.df_export = st.session_state.df
data = convert_df(st.session_state.df_export)
st.button('Do', on_click=do)
st.button('Save', on_click=save)
st.download_button("Download", data=data, file_name='res.csv', mime='csv')
Prior to the actual generation of the CSV file, you can add additional task to perform and package it into a single function, which when users click on would trigger all the task in the function.
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.