Tip
This page only contains the st.connections.SnowflakeConnection class. For a deeper dive into creating and managing data connections within Streamlit apps, see Connect Streamlit to Snowflake and Connecting to data.
st.connections.SnowflakeConnection
A connection to Snowflake using the Snowflake Connector for Python.
For standard connections, create an instance of this using st.connection("snowflake") or st.connection("<name>", type="snowflake"). Connection parameters for a SnowflakeConnection can be specified using secrets.toml and/or **kwargs. Connection parameters are passed to snowflake.connector.connect().
When an app is running in Streamlit in Snowflake, st.connection("snowflake") connects automatically using the app owner's role without further configuration. **kwargs are ignored in this case. Use secrets.toml and **kwargs to configure your connection for local development.
When an app is running in Snowpark Container Services and has caller's rights enabled, st.connection("snowflake-callers-rights") connects automatically using the current user's identity tokens. This is a session-scoped connection to ensure that the identity stays tied to the active user. Unlike with "snowflake" connections, it will use the Snowpark Container Services connection settings even when other **kwargs are provided.
The Snowflake connection includes several convenience methods. For example, you can directly execute a SQL query with .query() or access the underlying Snowflake Connector object with .raw_connection.
Important
snowflake-snowpark-python must be installed in your environment to use this connection. You can install it as an extra with Streamlit:
TerminalAccount identifiers must be of the form <orgname>-<account_name> where <orgname> is the name of your Snowflake organization and <account_name> is the unique name of your account within your organization. This is dash-separated, not dot-separated like when used in SQL queries. For more information, see Account identifiers.
Caller's rights connections rely on credentials provided when a user first connects to a Streamlit app. These credentials are only valid for about two minutes. Therefore, caller's rights connections must be created at the top of an app or else the connection may fail.
To develop locally with a caller's rights connection, use an environment variable to logically switch between a "snowflake" connection locally and a "snowflake-callers-rights" connection in Snowpark Container Services.
| Class description[source] | |
|---|---|
st.connections.SnowflakeConnection(connection_name, **kwargs) | |
| Methods | |
close() | Closes the underlying Snowflake connection. |
cursor() | Create a new cursor object from this connection. |
query(sql, *, ttl=None, show_spinner="Running `snowflake.query(...)`.", params=None, **kwargs) | Run a read-only SQL query. |
reset() | Reset this connection so that it gets reinitialized the next time it's used. |
session() | Create a new Snowpark session from this connection. |
write_pandas(df, table_name, database=None, schema=None, chunk_size=None, **kwargs) | Write a pandas.DataFrame to a table in a Snowflake database. |
| Attributes | |
Access the underlying connection object from the Snowflake Connector for Python. | |
Examples
Example 1: Configuration with Streamlit secrets
You can configure your Snowflake connection using Streamlit's Secrets management. For example, if you have MFA enabled on your account, you can connect using key-pair authentication.
Example 2: Configuration with keyword arguments and external authentication
You can configure your Snowflake connection with keyword arguments. The keyword arguments are merged with (and take precedence over) the values in secrets.toml. However, if you name your connection "snowflake" and don't have a [connections.snowflake] dictionary in your secrets.toml file, Streamlit will ignore any keyword arguments and use the default Snowflake connection as described in Example 5 and Example 6. To configure your connection using only keyword arguments, declare a name for the connection other than "snowflake".
For example, if your Snowflake account supports SSO, you can set up a quick local connection for development using browser-based SSO. Because there is nothing configured in secrets.toml, the name is an empty string and the type is set to "snowflake". This prevents Streamlit from ignoring the keyword arguments and using a default Snowflake connection.
Example 3: Named connection with Snowflake's connection configuration file
Snowflake's Python Connector supports a connection configuration file, which is well integrated with Streamlit's SnowflakeConnection. If you already have one or more connections configured, all you need to do is pass the name of the connection to use.
Example 4: Named connection with Streamlit secrets and Snowflake's connection configuration file
If you have a Snowflake configuration file with a connection named my_connection as in Example 3, you can pass the connection name through secrets.toml.
Example 5: Default connection with an environment variable
If you don't have a [connections.snowflake] dictionary in your secrets.toml file and use st.connection("snowflake"), Streamlit will use the default connection for the Snowflake Python Connector.
If you have a Snowflake configuration file with a connection named my_connection as in Example 3, you can set an environment variable to declare it as the default Snowflake connection.
Example 6: Default connection in Snowflake's connection configuration file
If you have a Snowflake configuration file that defines your default connection, Streamlit will automatically use it if no other connection is declared.
Example 7: Caller's rights connection when running in Snowpark Container Services
You can use "snowflake-callers-rights" type connections in any environment running on Snowpark Container Services, including Streamlit in Snowflake on containers and any self-managed caller's rights Service.
This will use the Snowpark-provided account, host, database, and schema to connect. Additionally, it will set client_session_keep_alive to True. These values may be overridden with **kwargs in st.connection. For a complete list of keyword arguments, see the documentation for snowflake.connector.connect().
If you want to develop locally with a caller's rights connection, use an environment variable to logically switch between a "snowflake" connection locally and a "snowflake-callers-rights" connection in Snowpark Container Services.
SnowflakeConnection.cursor
Create a new cursor object from this connection.
Snowflake Connector cursors implement the Python Database API v2.0 specification (PEP-249). For more information, see the Snowflake Connector for Python documentation.
| Function signature[source] | |
|---|---|
SnowflakeConnection.cursor() | |
| Returns | |
(snowflake.connector.cursor.SnowflakeCursor) | A cursor object for the connection. |
Example
The following example uses a cursor to insert multiple rows into a table. The qmark parameter style is specified as an optional keyword argument. Alternatively, the parameter style can be declared in your connection configuration file. For more information, see the Snowflake Connector for Python documentation.
SnowflakeConnection.query
Run a read-only SQL query.
This method implements query result caching and simple error handling/retries. The caching behavior is identical to that of using @st.cache_data.
Note
Queries that are run without a specified ttl are cached indefinitely.
| Function signature[source] | |
|---|---|
SnowflakeConnection.query(sql, *, ttl=None, show_spinner="Running `snowflake.query(...)`.", params=None, **kwargs) | |
| 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. If this is None (default), cached results do not expire with time. |
show_spinner (boolean or string) | Whether to enable the spinner. When a cached query is executed, no spinner is displayed because the result is immediately available. When a new query is executed, the default is to show a spinner with the message "Running snowflake.query(...)." If this is False, no spinner displays while executing the query. If this is a string, the string will be used as the message for the spinner. |
params (list, tuple, dict or None) | List of parameters to pass to the Snowflake Connector for Python Cursor.execute() method. This connector supports binding data to a SQL statement using qmark bindings. For more information and examples, see the Snowflake Connector for Python documentation. This defaults to None. |
| Returns | |
(pandas.DataFrame) | The result of running the query, formatted as a pandas DataFrame. |
Example
SnowflakeConnection.raw_connection
Access the underlying connection object from the Snowflake Connector for Python.
For information on how to use the Snowflake Connector for Python, see the Snowflake Connector for Python documentation.
| Function signature[source] | |
|---|---|
SnowflakeConnection.raw_connection | |
| Returns | |
(snowflake.connector.connection.SnowflakeConnection) | The connection object. |
Example
The following example uses a cursor to submit an asynchronous query, saves the query ID, then periodically checks the query status through the connection before retrieving the results.
SnowflakeConnection.reset
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] | |
|---|---|
SnowflakeConnection.reset() | |
| Returns | |
(None) | No description |
Example
SnowflakeConnection.session
Create a new Snowpark session from this connection.
For information on how to use Snowpark sessions, see the Snowpark developer guide and Snowpark API Reference.
| Function signature[source] | |
|---|---|
SnowflakeConnection.session() | |
| Returns | |
(snowflake.snowpark.Session) | A new Snowpark session for this connection. |
Example
The following example creates a new Snowpark session and uses it to run a query.
SnowflakeConnection.write_pandas
Write a pandas.DataFrame to a table in a Snowflake database.
This convenience method is a thin wrapper around snowflake.connector.pandas_tools.write_pandas() using the underlying connection. The conn parameter is passed automatically. For more information and additional keyword arguments, see the Snowflake Connector for Python documentation.
| Function signature[source] | |
|---|---|
SnowflakeConnection.write_pandas(df, table_name, database=None, schema=None, chunk_size=None, **kwargs) | |
| Parameters | |
df (pandas.DataFrame) | The pandas.DataFrame object containing the data to be copied into the table. |
table_name (str) | Name of the table where the data should be copied to. |
database (str) | Name of the database containing the table. By default, the function writes to the database that is currently in use in the session. Note If you specify this parameter, you must also specify the schema parameter. |
schema (str) | Name of the schema containing the table. By default, the function writes to the table in the schema that is currently in use in the session. |
chunk_size (int) | Number of elements to insert at a time. By default, the function inserts all elements in one chunk. |
**kwargs (Any) | Additional keyword arguments for snowflake.connector.pandas_tools.write_pandas(). |
| Returns | |
(tuple[bool, int, int]) | A tuple containing three values:
|
Example
The following example uses the database and schema currently in use in the session and copies the data into a table named "my_table."
Still have questions?
Our forums are full of helpful information and Streamlit experts.
