L iddlmZddlZddlmZddlmZddlmZm Z m Z ddl m Z ddl mZmZmZddlmZdd lmZerdd lmZdd lmZdd lmZdd lmZdhZGdde dZy)) annotationsN)ChainMap)contextmanager) TYPE_CHECKINGAnycast)BaseConnection)SNOWSQL_CONNECTION_FILEload_from_snowsql_config_filerunning_in_sis)StreamlitAPIException) cache_data)Iterator) timedelta) DataFrameSessionaccountcfeZdZdZdfd ZddZ d d dZed dZe d dZ xZ S) SnowparkConnectionaA connection to Snowpark using snowflake.snowpark.session.Session. Initialize using ``st.connection("", type="snowpark")``. In addition to providing access to the Snowpark Session, SnowparkConnection supports direct SQL querying using ``query("...")`` and thread safe access using ``with conn.safe_session():``. See methods below for more information. SnowparkConnections should always be created using ``st.connection()``, **not** initialized directly. .. note:: We don't expect this iteration of SnowparkConnection to be able to scale well in apps with many concurrent users due to the lock contention that will occur over the single underlying Session object under high load. c Xtj|_t||fi|yN) threadingRLock_locksuper__init__)selfconnection_namekwargs __class__s o/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/streamlit/connections/snowpark_connection.pyrzSnowparkConnection.__init__@s#__&  3F3c ddlm}ddlm}t r|St ||j jt|j}t|stdtdtD]}||vstd|td|jj!|j#S)Nr)get_active_sessionrzZMissing Snowpark connection configuration. Did you forget to set this in `secrets.toml`, `z#`, or as kwargs to `st.connection`?z#Missing Snowpark connection param: r)snowflake.snowpark.contextr%snowflake.snowpark.sessionrr r_secretsto_dictr _connection_namelenr r _REQUIRED_CONNECTION_PARAMSrbuilderconfigscreate)rr r%r conn_paramsps r"_connectzSnowparkConnection._connectDsA6  %' '  MM ! ! # )$*?*? @ ;'BBYAZ[33  - WA #+.QRSQT,UVV WIw66{CJJLMMr#c 0ddlm}ddlm}m}m}m}|fd|dd|||ddfd }t|jd d } |jd jd | |_ td | |}||S)aRun a read-only SQL query. This method implements both query result caching (with caching behavior identical to that of using ``@st.cache_data``) as well as simple error handling/retries. .. note:: Queries that are run without a specified ttl are cached indefinitely. Parameters ---------- sql : str The read-only SQL query to execute. ttl : float, int, timedelta or None The maximum number of seconds to keep results in the cache, or None if cached results should not expire. The default is None. Returns ------- pandas.DataFrame The result of running the query, formatted as a pandas DataFrame. Example ------- >>> import streamlit as st >>> >>> conn = st.connection("snowpark") >>> df = conn.query("SELECT * FROM pet_owners") >>> st.dataframe(df) r)SnowparkServerException)retryretry_if_exception_typestop_after_attempt wait_fixedc$jSr)reset)_rs r"z*SnowparkConnection.query..s DJJLr#T)afterstopreraiser5waitcj5jj|jcdddS#1swYyxYwr)r _instancesql to_pandas)rErs r"_queryz(SnowparkConnection.query.._querys< ;~~))#.88: ; ; ;s )AA .r;zRunning `snowpark.query(...)`.) show_spinnerttl)rEstrreturnr) snowflake.snowpark.exceptionsr4tenacityr5r6r7r8rKreplace __qualname__r*r) rrErJr4r5r6r7r8rGttl_strs ` r"queryzSnowparkConnection.query`sD    (#A&)*ABA   ;  ;  '#s  "(!4!4 5Qt7L7L6MQwiX 9   c{r#c|jS)a,Access the underlying Snowpark session. .. note:: Snowpark sessions are **not** thread safe. Users of this method are responsible for ensuring that access to the session returned by this method is done in a thread-safe manner. For most users, we recommend using the thread-safe safe_session() method and a ``with`` block. Information on how to use Snowpark sessions can be found in the `Snowpark documentation `_. Example ------- >>> import streamlit as st >>> >>> session = st.connection("snowpark").session >>> df = session.table("mytable").limit(10).to_pandas() >>> st.dataframe(df) )rDrs r"sessionzSnowparkConnection.sessions*~~r#c#jK|j5|jdddy#1swYyxYww)aGrab the underlying Snowpark session in a thread-safe manner. As operations on a Snowpark session are not thread safe, we need to take care when using a session in the context of a Streamlit app where each script run occurs in its own thread. Using the contextmanager pattern to do this ensures that access on this connection's underlying Session is done in a thread-safe manner. Information on how to use Snowpark sessions can be found in the `Snowpark documentation `_. Example ------- >>> import streamlit as st >>> >>> conn = st.connection("snowpark") >>> with conn.safe_session() as session: ... df = session.table("mytable").limit(10).to_pandas() >>> >>> st.dataframe(df) N)rrUrTs r" safe_sessionzSnowparkConnection.safe_sessions-.ZZ ,,    s 3' 303)rrKr rrLNone)r rrLrr)rErKrJzfloat | int | timedelta | NonerLr)rLr)rLzIterator[Session]) __name__ __module__rP__doc__rr2rRpropertyrUrrW __classcell__)r!s@r"rr0sf 4N>/3D D,D  DL,r#rr) __future__rr collectionsr contextlibrtypingrrrstreamlit.connectionsr streamlit.connections.utilr r r streamlit.errorsr streamlit.runtime.cachingrcollections.abcrdatetimerpandasrr'rr,rr#r"rjsZ*# %++0 30(" 2 )ke 2er#