How to input a pswd to run a part of the code

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()

Hey @Jacques2101,

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?

I mean that it does not update my data (I put a stqdm that should takes some time).

What do you mean with @st.cache_data function ?

Sorry I meant @st.cache.

Why are you decorating update_data() with @st.cache and then before calling it using st.cache_data.clear()?

@Jacques2101, the problem with your code is that:

  1. 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.

  2. 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()

" Why are you decorating update_data() with @st.cache and then before calling it using st.cache_data.clear() ?"
I need to clear my data stores no ?

I will read the secret.toml option you mention. I thought it was ok the way I proceeded (but it is not the case because it did not work)

i am sorry but I did not find .streamlit/secrets.toml

What is .streamlit directory, the “.” in front especially ?

For now I am using "streamlit run “/Users/jacques/Library/Mobile Documents/com~apple~CloudDocs/Projets/Quantalys/Analyse.py” " to run my app

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.

ok but where should I create .streamlit directory. I installed python/streamlit with anaconda ?

i am sorry my question seems simple and basic…

You should save it in the same folder where the script that you run with “streamlit run app.py” is located.

Hey, don’t be sorry. We all have been new to Streamlit someday. Just be patient, it takes a little bit to get used to things.

thanks a lot

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

I used:

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 it seems to work but what I do not understand it is why I need to click twice on the button to update my data ?

@Jacques2101

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 ?

Thx

No answer to my previous question ?