How to cache connection to Snowflake?

Hello Experts,

Below is my code:


### Connect to Snowflake ###
#@st.cache
@st.experimental_singleton()
def create_session():
    with open('creds.json',encoding='utf-8') as f:
        cp = json.load(f)
    conn = snowflake.connector.connect(
                    user=cp["user"],
                    password=cp["password"],
                    account=cp["account"],
                    warehouse=cp["warehouse"],
                    database=cp["database"],
                    role=cp["role"],
                    schema=cp["schema"]
                    )

    return conn

curr_sess = create_session()
df_database=pd.read_sql("select * from cycle_time",curr_sess)

But it is very slow, every time, I select different options in radio button, or from sidebar, it reruns whole thing. how to cache above connection ?

Hey @Parthib_Rathinam,

Your code looks good in terms of caching the connection to Snowflake. Can you share the full code with the radio buttons you mentioned?

Just from the snippet you shared, the cause of the slowness might be the fact that you’re selecting everything from cycle_time (depending on the size of cycle_time).