star

Tip

This page only contains the st.connections.SnowparkConnection class. For a deeper dive into creating and managing data connections within Streamlit apps, read Connecting to data.

delete

Deprecation notice

st.connections.SnowParkConnection was deprecated in version 1.28.0. Use st.connections.SnowflakeConnection instead.

A connection to Snowpark using snowflake.snowpark.session.Session. Initialize using

st.connection("<name>", 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.

Class description[source]

st.connections.SnowparkConnection(connection_name, **kwargs)

Methods

query(sql, ttl=None)

Run a read-only SQL query.

reset()

Reset this connection so that it gets reinitialized the next time it's used.

Grab the underlying Snowpark session in a thread-safe manner.

Attributes

Access the underlying Snowpark session.

Run 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.

Function signature[source]

SnowparkConnection.query(sql, ttl=None)

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)

Reset this connection so that it gets reinitialized the next time it's used.

This method can be useful when a connection has become stale, an auth token has expired, or in similar scenarios where a broken connection might be fixed by reinitializing it. Note that some connection methods may already use reset() in their error handling code.

Function signature[source]

SnowparkConnection.reset()

Returns

(None)

No description

Example

import streamlit as st

conn = st.connection("my_conn")

# Reset the connection before using it if it isn't healthy
# Note: is_healthy() isn't a real method and is just shown for example here.
if not conn.is_healthy():
    conn.reset()

# Do stuff with conn...

Grab 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.

Function signature[source]

SnowparkConnection.safe_session()

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)

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.

Function signature[source]

SnowparkConnection.session

Example

import streamlit as st

session = st.connection("snowpark").session
df = session.table("mytable").limit(10).to_pandas()
st.dataframe(df)
forum

Still have questions?

Our forums are full of helpful information and Streamlit experts.