Hi, i have a code that is like that (see below).
It seems that it is not working as I thought.
I woud like my code to update my data in case of I click on “Mise à jour des données” (with password code) but it seems it did not.
why ?
@st.cache
def update_data():
# update des data
return list_sdg, categorie_fonds, list_fonds
@st.cache
def load_data():
# load data
return list_sdg, list_fonds, list_aum, iso_pays, categorie_fonds
with tab1:
mise_a_jour = st.button("Mise à jour des données:")
if mise_a_jour:
col = st.columns([0.1,0.9])
password = col[0].text_input('Mot de passe pour mise à jour des données:',
type='password'
)
if password=='pswd':
st.cache_data.clear()
list_sdg, categorie_fonds, list_fonds = update_data()
else:
list_sdg, list_fonds, list_aum, iso_pays, categorie_fonds = load_data()
What do you mean “it is not working”? Do you mean the data does not update, but the app still works right?
Genuinely curious why you would want to @st.cache_data a function, but then everytime you call it you start by clearing the cache. This basically means you’re never really taking advantage of the cache, right?
In order to save password in your Streamlit application you should not store them as plain text in your script, rather you should use the secrets.toml file located in the .streamlit folder of your streamlit app. More details here.
Once you’ve done that you should condition the update to the check_password() function described in the article above AND the click of the button, such as this:
mise_a_jour = st.button("Mise à jour des données:")
if mise_a_jour and check_password():
st.cache_data.clear()
list_sdg, categorie_fonds, list_fonds = update_data()
If the directory does not exist in your app folder, you can create it. Just create a new folder and give it the name:
.streamlit
After that, create a file inside it called:
secrets.toml
If you’re not familiar on how to create a file with this extension, you can create any file such as a text file called “secrets.txt” and change the “.txt” extension to “.toml” afterwards.
I created the folder and the file but get an error message in the folder where I have my app ie /Users/jacques/Library/Mobile Documents/com~apple~CloudDocs/Projets/Quantalys/
but get an error message:
FileNotFoundError: No secrets files found. Valid paths for a secrets.toml file are: /Users/jacques/.streamlit/secrets.toml, /Users/jacques/.streamlit/secrets.toml
it is where I have my program file /Users/jacques/Library/Mobile Documents/com~apple~CloudDocs/Projets/Quantalys/Analyse.py
def check_password():
"""Returns `True` if the user had the correct password."""
def password_entered():
"""Checks whether a password entered by the user is correct."""
if hmac.compare_digest(st.session_state["password"], st.secrets["password"]):
st.session_state["password_correct"] = True
del st.session_state["password"] # Don't store the password.
else:
st.session_state["password_correct"] = False
# Return True if the passward is validated.
if st.session_state.get("password_correct", False):
return True
# Show input for password.
st.text_input(
"Password", type="password", on_change=password_entered, key="password"
)
if "password_correct" in st.session_state:
st.error("😕 Password incorrect")
return False
ok I found my error. It is not in directory /Users/jacques/Library/Mobile Documents/com~apple~CloudDocs/Projets/Quantalys/ that I should put the file but instead in /Users/jacques/
But it is still the same as before ie in does not update my file
Now that we got the password thing out of the way, the problem seems to be on your button. st.button does not retain state, that is, it is always “False”. When the user clicks it, it turns to “True” and immediately turns back to “False”.
An easy way to solve this is to use st.toggle instead, that is, redefine your button this way:
mise_a_jour = st.toggle("Mise à jour des données:", value=False)
The other way is to make your st.button stateful, by the method described in this tutorial.
I don’t understand why the password is just asked once ? How to have each time I click on ‘Mise à jour des données’ a relaunch of the password demand ?
Moreover I don’t know why I have to click twice on “Mise à jour des données” button to get the data updated ?
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.