L i<UddlmZddlZddlZddlmZmZmZmZm Z m Z ddl m Z m Z mZmZddlmZddlmZddlmZddlmZdd lmZerdd lmZe eed Zd ed <ej<dZdddddddZ ded<dZ!ded<e de eZ"ed d( d)dZ#d*dZ$e d+ d,dZ%e d+ d-d Z%e d+ d.d!Z%e d+ d/d"Z%e d( d0d#Z%e d( d1d$Z%e d( d2d%Z%e d3 d4d&Z% d3d'Z%y)5) annotationsN) TYPE_CHECKINGAnyFinalLiteralTypeVaroverload)BaseConnectionSnowflakeConnectionSnowparkConnection SQLConnection)deprecate_obj_name)StreamlitAPIException)cache_resource)gather_metrics)secrets_singleton) timedelta) snowflakesnowparksqlz+Final[dict[str, type[BaseConnection[Any]]]]_FIRST_PARTY_CONNECTIONSzNo module named \'(.+)\' mysqlclientzpsycopg2-binary sqlalchemyzsnowflake-connector-pythonzsnowflake-snowpark-python)MySQLdbpsycopg2rrzsnowflake.connectorzsnowflake.snowparkzFinal[dict[str, str]]_MODULES_TO_PYPI_PACKAGESzenv:r_USE_ENV_PREFIXConnectionClass)bound connectionc  dd}t|tst|dt|j dd}|j d|d||_t |d||}|||fi|S)apCreate an instance of connection_class with the given name and kwargs. The weird implementation of this function with the @cache_resource annotated function defined internally is done to: - Always @gather_metrics on the call even if the return value is a cached one. - Allow the user to specify ttl and max_entries when calling st.connection. c|dd|i|S)Nconnection_namer$)nameconnection_classkwargss j/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/streamlit/runtime/connection_factory.py__create_connectionz/_create_connection..__create_connectionOs ????z% is not a subclass of BaseConnection!._zRunning `st.connection(...)`.) max_entries show_spinnerttl)r%strr&type[ConnectionClass]r'rreturnr) issubclassr rr0replace __qualname__r)r%r&r-r/r'r)ttl_strs r(_create_connectionr7?s @@%:@FI@ @ & 7# E F  #h SG  + + ,AgYa }E$.4   t%5 @ @@r*cN|tvr t|Std|dt)NzInvalid connection 'z!'. Supported connection classes: )rr)r&s r(_get_first_party_connectionr9ks>33'(899  /01))A(B D r*c yNr$r%r-r/ autocommitr's r(connection_factoryr>u r*c yr;r$r%typer-r/r=r's r(r>r> r*c yr;r$r<s r(r>r>r?r*c yr;r$rAs r(r>r>rCr*c yr;r$)r%r-r/r's r(r>r>s r*c yr;r$r%rBr-r/r's r(r>r>r?r*c yr;r$rHs r(r>r>r?r*c yr;r$rHs r(r>r>r?r*c "|jtr(|jt}tj|}|}|7|t vr t |}n#tjtd|d}t|trad|vrR|jd}|j}ddl } | jdj|} t!| |}n t |} t#||f||d|} t| t$rt'| ddd } | S#t($rj} t| } t+j,t.| }d }|r,t0j3|j5d }|rd |d }t)| d|d} ~ wwxYw)a3Create a new connection to a data store or API, or return an existing one. Configuration options, credentials, and secrets for connections are combined from the following sources: - The keyword arguments passed to this command. - The app's ``secrets.toml`` files. - Any connection-specific configuration files. The connection returned from ``st.connection`` is internally cached with ``st.cache_resource`` and is therefore shared between sessions. Parameters ---------- name : str The connection name used for secrets lookup in ``secrets.toml``. Streamlit uses secrets under ``[connections.]`` for the connection. ``type`` will be inferred if ``name`` is one of the following: ``"snowflake"``, ``"snowpark"``, or ``"sql"``. type : str, connection class, or None The type of connection to create. This can be one of the following: - ``None`` (default): Streamlit will infer the connection type from ``name``. If the type is not inferable from ``name``, the type must be specified in ``secrets.toml`` instead. - ``"snowflake"``: Streamlit will initialize a connection with |SnowflakeConnection|_. - ``"snowpark"``: Streamlit will initialize a connection with |SnowparkConnection|_. This is deprecated. - ``"sql"``: Streamlit will initialize a connection with |SQLConnection|_. - A string path to an importable class: This must be a dot-separated module path ending in the importable class. Streamlit will import the class and initialize a connection with it. The class must extend ``st.connections.BaseConnection``. - An imported class reference: Streamlit will initialize a connection with the referenced class, which must extend ``st.connections.BaseConnection``. .. |SnowflakeConnection| replace:: ``SnowflakeConnection`` .. _SnowflakeConnection: https://docs.streamlit.io/develop/api-reference/connections/st.connections.snowflakeconnection .. |SnowparkConnection| replace:: ``SnowparkConnection`` .. _SnowparkConnection: https://docs.streamlit.io/develop/api-reference/connections/st.connections.snowparkconnection .. |SQLConnection| replace:: ``SQLConnection`` .. _SQLConnection: https://docs.streamlit.io/develop/api-reference/connections/st.connections.sqlconnection max_entries : int or None The maximum number of connections to keep in the cache. If this is ``None`` (default), the cache is unbounded. Otherwise, when a new entry is added to a full cache, the oldest cached entry is removed. ttl : float, timedelta, or None The maximum number of seconds to keep results in the cache. If this is ``None`` (default), cached results do not expire with time. **kwargs : any Connection-specific keyword arguments that are passed to the connection's ``._connect()`` method. ``**kwargs`` are typically combined with (and take precedence over) key-value pairs in ``secrets.toml``. To learn more, see the specific connection's documentation. Returns ------- Subclass of BaseConnection An initialized connection object of the specified ``type``. Examples -------- **Example 1: Inferred connection type** The easiest way to create a first-party (SQL, Snowflake, or Snowpark) connection is to use their default names and define corresponding sections in your ``secrets.toml`` file. The following example creates a ``"sql"``-type connection. ``.streamlit/secrets.toml``: >>> [connections.sql] >>> dialect = "xxx" >>> host = "xxx" >>> username = "xxx" >>> password = "xxx" Your app code: >>> import streamlit as st >>> conn = st.connection("sql") **Example 2: Named connections** Creating a connection with a custom name requires you to explicitly specify the type. If ``type`` is not passed as a keyword argument, it must be set in the appropriate section of ``secrets.toml``. The following example creates two ``"sql"``-type connections, each with their own custom name. The first defines ``type`` in the ``st.connection`` command; the second defines ``type`` in ``secrets.toml``. ``.streamlit/secrets.toml``: >>> [connections.first_connection] >>> dialect = "xxx" >>> host = "xxx" >>> username = "xxx" >>> password = "xxx" >>> >>> [connections.second_connection] >>> type = "sql" >>> dialect = "yyy" >>> host = "yyy" >>> username = "yyy" >>> password = "yyy" Your app code: >>> import streamlit as st >>> conn1 = st.connection("first_connection", type="sql") >>> conn2 = st.connection("second_connection") **Example 3: Using a path to the connection class** Passing the full module path to the connection class can be useful, especially when working with a custom connection. Although this is not the typical way to create first party connections, the following example creates the same type of connection as one with ``type="sql"``. Note that ``type`` is a string path. ``.streamlit/secrets.toml``: >>> [connections.my_sql_connection] >>> url = "xxx+xxx://xxx:xxx@xxx:xxx/xxx" Your app code: >>> import streamlit as st >>> conn = st.connection( ... "my_sql_connection", type="streamlit.connections.SQLConnection" ... ) **Example 4: Importing the connection class** You can pass the connection class directly to the ``st.connection`` command. Doing so allows static type checking tools such as ``mypy`` to infer the exact return type of ``st.connection``. The following example creates the same connection as in Example 3. ``.streamlit/secrets.toml``: >>> [connections.my_sql_connection] >>> url = "xxx+xxx://xxx:xxx@xxx:xxx/xxx" Your app code: >>> import streamlit as st >>> from streamlit.connections import SQLConnection >>> conn = st.connection("my_sql_connection", type=SQLConnection) N connectionsrBr+r)r-r/zconnection("snowpark")zconnection("snowflake")z 2024-04-01z@You may be missing a dependency required to use this connection.zYou need to install the 'z!' package to use this connection.z. ) startswithr removeprefixosenvironrr9rload_if_toml_exists isinstancer0splitpop importlib import_modulejoingetattrr7r rModuleNotFoundErrorresearch_MODULE_EXTRACTION_REGEXrgetgroup)r%rBr-r/r' envvar_namer&parts classnamerVconnection_moduleconne err_stringmissing_module extra_info pypi_packages r(r>r>sJ '''8 zz+&  + + ;4@   1 1 30?EfM "C( " "$**3/E I  ) 7 7 H &'8)D :;KL 8! " 0; HN  d. /%() D   8V #;ZHW 4889M9Ma9PQL8Fgh !QCr*"677 8s+/D F$A%F  F)NN) r%r0r&r1r- int | Noner/float | timedelta | Noner'rr2r)r&r0r2ztype[BaseConnection[Any]])NNF) r%Literal['sql']r-rjr/rkr=boolr'rr2r )r%r0rBrlr-rjr/rkr=rmr'rr2r ) r%Literal['snowflake']r-rjr/rkr=rmr'rr2r )r%r0rBrnr-rjr/rkr=rmr'rr2r ) r%Literal['snowpark']r-rjr/rkr'rr2r ) r%r0rBror-rjr/rkr'rr2r ) r%r0rBr1r-rjr/rkr'rr2r)NNN) r%r0rBz str | Noner-rjr/rkr'rr2zBaseConnection[Any])& __future__rrPr[typingrrrrrr streamlit.connectionsr r r r streamlit.deprecation_utilrstreamlit.errorsrstreamlit.runtime.cachingrstreamlit.runtime.metrics_utilrstreamlit.runtime.secretsrdatetimerr__annotations__compiler]rrrr7r9r>r$r*r(r{s# HH :2497"%" IE &2::&AB!-75 40  +>#3FG #$( (A (A+(A(A " (A  (A  (A(AV #$(     "          #$(      "          #$(     "          #$(      "          #$(    "        #$(      "         #$(      "         "$(      "          c8r*