Function keeps running over and over again

Iโ€™m running the app locally as follows:

Streamlit 1.28.2
Python 3.12

I have the following single file that I run streamlit run sample.py. I want GetToken() to run once, but it runs over and over again.

import toml
import requests
import json
import pandas as pd
import streamlit as st

accounts = st.secrets["accounts"]
initial_token = st.secrets["refresh_token"]
url = st.secrets["api_server"]

def getToken():
    # get a new refresh token and populate variables

    r = json.loads(
        requests.get(
            f"https://login.something.com/oauth2/token?grant_type=refresh_token&refresh_token={initial_token}"
        ).text
    )

    # url = r["api_server"]
    access_token = r["access_token"]
    refresh_token = r["refresh_token"]
    header = {"Authorization": f"Bearer {refresh_token}"}

    data = toml.load(".streamlit/secrets.toml")
    data["refresh_token"] = refresh_token
    data["access_token"] = access_token
    data["header"] = header
    # data["api_server"] = url
    f = open(".streamlit/secrets.toml", "w")
    toml.dump(data, f)
    f.close()

    pass


if __name__ == "__main__":
    getToken()

Deactivate โ€œRun on savingโ€ in the settings.

Try use

def main():
    getToken()

if __name__ == "__main__":
    main()

Thanks. This was the solution. I didnโ€™t realize that the runOnSave is triggered even when a change is done to files in the .streamlit dir. I assumed it would only run on files in the root dir.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.