Last active
January 9, 2024 06:37
-
-
Save blackary/68d3b31ee5546e4b50862b74f192ed15 to your computer and use it in GitHub Desktop.
Caching Snowflake queries in Streamlit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_pandas_from_sql(sql: str) -> pd.DataFrame: | |
start = time.start() | |
with st.expander("Show the SQL query that generated this data"): | |
st.code(sql, language="sql") | |
@st.cache_data(ttl=TTL) | |
def get_df(sql): | |
dataframe = session.sql(sql).to_pandas() | |
return dataframe | |
dataframe = get_df(sql) | |
st.info(f"⏱️ Data loaded in {time.time() - start_time:.2f} seconds") | |
return dataframe | |
def get_pandas_from_snowpark(df: snowpark.DataFrame) -> pd.DataFrame: | |
query = df._plan.queries[0].sql # Get the SQL that will be run when .to_pandas() is called | |
start = time.start() | |
with st.expander("Show the SQL query that generated this data"): | |
st.code(sql, language="sql") | |
@st.cache_data(ttl=TTL) | |
def get_df( | |
_df: snowpark.DataFrame, | |
query_str: str, | |
) -> pd.DataFrame: | |
return _df.to_pandas() | |
pd_df = get_df(_df=df, query_str=query) | |
st.info(f"⏱️ Data loaded in {time.time() - start_time:.2f} seconds") | |
return pd_df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment