Manage secrets and configure your Streamlit app¶
Streamlit apps often need to access sensitive information such as API keys, passwords, and other credentials. How you manage secrets in your Streamlit app depends on the runtime environment you’re using. Streamlit in Snowflake provides secure, built-in mechanisms for accessing secrets in both warehouse and container runtimes. For Streamlit configuration, each runtime has different restrictions, too.
In the Streamlit library, apps use a .streamlit/ directory to store configuration and secrets:
.streamlit/config.toml: Customizes app settings such as theme, layout, and server behavior..streamlit/secrets.toml: Stores sensitive information like API keys and credentials (in local development).
Streamlit in Snowflake supports these files with some limitations depending on your runtime environment. The following table summarizes the support for these files in warehouse and container runtimes:
Feature |
Warehouse runtime |
Container runtime |
|---|---|---|
|
Limited subset of configuration options |
Broader subset of configuration options |
|
Not supported |
Supported, but only recommended for non-secret environment variables |
For secrets.toml, Streamlit in Snowflake provides a more secure, built-in secrets management system that is recommended
for managing sensitive information. The following sections describe how to use Snowflake secrets in your apps.
Managing your connection to Snowflake¶
To manage your connection to Snowflake, you can use st.connection("snowflake") (https://docs.streamlit.io/develop/api-reference/connections/st.connections.snowflakeconnection). This allows you to connect to Snowflake from both
your local development environment and your deployed app.
In warehouse runtimes, you can also use Snowpark’s get_active_session() function to get the active session.
Important
get_active_session() isn’t thread-safe and can’t be used in container runtimes.
Secrets in container runtimes¶
Container runtimes don’t have access to the _snowflake module because they run outside of the stored procedure
environment. To access secrets in a container runtime, you must create SQL functions that use the _snowflake module
and then call those functions from your Streamlit app. For Cortex API calls, you must use requests.
If you upgrade an older Streamlit app to a container runtime, the following _snowflake functions should
be replaced with stored procedures. This is described in the next section.
get_generic_secret_stringget_oauth_access_tokenget_username_passwordget_cloud_provider_tokenget_secret_type
Additionally, the following _snowflake function should be replaced with a manual API call,
authenticated with a session token. This is described in a later section, Calling a Cortex Agent in a container runtime.
send_snow_api_request
Access a secret in a container runtime¶
To access a secret in a container runtime do the following steps:
Create a secret in your Snowflake account. See CREATE SECRET.
Create a function to access your secret. See Python API for Secret Access.
Create your Streamlit app with the container runtime:
In your Streamlit app code, call the SQL function to retrieve the secret:
Using .streamlit/secrets.toml for non-secret environment variables¶
While you can technically add a .streamlit/secrets.toml file to your app’s source
directory, this is not recommended for storing actual secrets. The secrets.toml
file is stored as plain text in your staged files, which is not a security best practice.
However, secrets.toml can be useful for storing non-sensitive configuration values or environment-specific settings
that you want to access via st.secrets in your code or that a dependency requires as an environment variable:
You can then access these values in your app through st.secrets or as environment variables:
For actual secrets like API keys, passwords, and tokens, always use Snowflake’s built-in secrets management system as described in the previous section.
Calling a Cortex Agent in a container runtime¶
To call a Cortex Agent in a container-runtime app, read the session token from the
underlying Snowpark Container Services container and then use the requests library. This is the
recommended replacement for _snowflake.send_snow_api_request().
Secrets in warehouse runtimes¶
In warehouse runtimes, you can use the _snowflake module to access secrets directly in your Streamlit app code.
Warehouse runtimes inherit access to the _snowflake module from stored procedures, which allows you to retrieve
secrets that are referenced in the Streamlit object.
To use secrets in a warehouse runtime:
Create a secret object in Snowflake. For more information, see CREATE SECRET.
Create an external access integration and assign the secret to it.
Reference the secret in your Streamlit object using the SECRETS parameter:
You must assign both the external access integration and the secret to the Streamlit object. You can’t assign a secret to a Streamlit object by itself.
In your Streamlit app code, import the
_snowflakemodule and retrieve the secret:
For more information about accessing secrets with the _snowflake module, see Python API for Secret Access.
Streamlit configuration¶
Streamlit apps can include a configuration file (.streamlit/config.toml). This file allows
you to customize various aspects of your app, such as the theme, layout, and behavior. The configuration
file is written in TOML format. For more information about available configuration options, see the
Streamlit documentation on config.toml (https://docs.streamlit.io/develop/api-reference/configuration/config.toml).
Support for configuration options varies by runtime environment. Container runtimes generally provide broader support for configuration options than warehouse runtimes, particularly for static serving. The following table shows which configuration sections are supported in warehouse and container runtimes:
Configuration section |
Warehouse runtime |
Container runtime |
|---|---|---|
|
Not supported |
Limited support ( |
|
Not supported |
Not supported |
|
Not supported |
Limited support ( |
|
Not supported |
Supported |
|
Not supported |
Not supported |
|
Not supported |
Not supported |
|
Not supported |
Supported (deprecated, use environment variables instead) |
|
Supported |
Supported |
|
Supported |
Supported |
|
Not supported |
Supported (but only recommended for non-secret environment variables) |
|
Supported |
Not applicable |
For information about using the [snowflake.sleep] section to configure sleep timers in warehouse runtimes, see
Custom sleep timer for a Streamlit app.
The following directory structure shows an example of a Streamlit app with a configuration file: