Connections and databases
Setup your connection
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
SnowflakeConnection
A connection to Snowflake.
conn = st.connection('snowflake')
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
delete
SnowparkConnection
A connection to Snowflake.
conn = st.connection("snowpark")
Still have questions?
Our forums are full of helpful information and Streamlit experts.