Basic usage:
SQLAlchemy and any required drivers must be installed to use this connection.
import streamlit as st
conn = st.connection("sql")
df = conn.query("select * from pet_owners")
st.dataframe(df)
In case you want to pass a connection URL (or other parameters) directly, it also works:
conn = st.connection(
"local_db",
type="sql",
url="mysql://user:pass@localhost:3306/mydb"
)
Or specify parameters in secrets:
# .streamlit/secrets.toml
[connections.mydb]
dialect = "mysql"
username = "myuser"
password = "password"
host = "localhost"
database = "mydb"
# streamlit_app.py
conn = st.connection("mydb", type="sql", autocommit=True)
As described above, some cloud databases use extra **kwargs
to specify credentials. These can be passed via secrets using the create_engine_kwargs
section:
# .streamlit/secrets.toml
[connections.snowflake]
url = "snowflake://<username>@<account>/"
[connections.snowflake.create_engine_kwargs.connect_args]
authenticator = "externalbrowser"
role = "..."
# ...