eagle0504's picture
app updated
746d2f1

A newer version of the Streamlit SDK is available: 1.48.1

Upgrade
metadata
title: Connections and databases
slug: /develop/api-reference/connections

Connections and databases

Setup your connection

screenshot

Create a connection

Connect to a data source or API

conn = st.connection('pets_db', type='sql')
pet_owners = conn.query('select * from pet_owners')
st.dataframe(pet_owners)

Built-in connections

screenshot

SnowflakeConnection

A connection to Snowflake.

conn = st.connection('snowflake')
screenshot

SQLConnection

A connection to a SQL database using SQLAlchemy.

conn = st.connection('sql')

Third-party connections

Connection base class

Build your own connection with BaseConnection.

class MyConnection(BaseConnection[myconn.MyConnection]):
    def _connect(self, **kwargs) -> MyConnection:
        return myconn.connect(**self._secrets, **kwargs)
    def query(self, query):
        return self._instance.query(query)

Secrets

Secrets singleton

Access secrets from a local TOML file.

key = st.secrets["OpenAI_key"]

Secrets file

Save your secrets in a per-project or per-profile TOML file.

OpenAI_key = "<YOUR_SECRET_KEY>"

Deprecated classes

SnowparkConnection

A connection to Snowflake.

conn = st.connection("snowpark")