You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This guide explains how to securely access a **_remote_** PostgreSQL database from Streamlit Community Cloud. It uses the [psycopg2](https://www.psycopg.org/) library and Streamlit's [Secrets management](/streamlit-community-cloud/deploy-your-app/secrets-management).
10
+
This guide explains how to securely access a **_remote_** PostgreSQL database from Streamlit Community Cloud. It uses [st.experimental_connection](/library/api-reference/connections/st.experimental_connection)and Streamlit's [Secrets management](/library/advanced-features/secrets-management). The below example code will **only work on Streamlit version >= 1.22**, when `st.experimental_connection` was added.
11
11
12
12
## Create a PostgreSQL database
13
13
@@ -36,17 +36,18 @@ Your local Streamlit app will read secrets from a file `.streamlit/secrets.toml`
36
36
```toml
37
37
# .streamlit/secrets.toml
38
38
39
-
[postgres]
39
+
[connections.postgresql]
40
+
dialect = "postgresql"
40
41
host = "localhost"
41
-
port = 5432
42
-
dbname = "xxx"
43
-
user = "xxx"
42
+
port = "5432"
43
+
database = "xxx"
44
+
username = "xxx"
44
45
password = "xxx"
45
46
```
46
47
47
48
<Important>
48
49
49
-
When copying your app secrets to Streamlit Community Cloud, be sure to replace the values of **host**, **port**, **dbname**, **user**, and **password** with those of your _remote_ PostgreSQL database!
50
+
When copying your app secrets to Streamlit Community Cloud, be sure to replace the values of **host**, **port**, **database**, **username**, and **password** with those of your _remote_ PostgreSQL database!
50
51
51
52
Add this file to `.gitignore` and don't commit it to your GitHub repo!
52
53
@@ -58,13 +59,14 @@ As the `secrets.toml` file above is not committed to GitHub, you need to pass it
Add the [psycopg2](https://www.psycopg.org/)package to your `requirements.txt` file, preferably pinning its version (replace `x.x.x` with the version you want installed):
64
+
Add the [psycopg2-binary](https://www.psycopg.org/)and [SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy) packages to your `requirements.txt` file, preferably pinning its version (replace `x.x.x` with the version you want installed):
64
65
65
66
```bash
66
67
# requirements.txt
67
68
psycopg2-binary==x.x.x
69
+
sqlalchemy==x.x.x
68
70
```
69
71
70
72
## Write your Streamlit app
@@ -75,32 +77,19 @@ Copy the code below to your Streamlit app and run it. Make sure to adapt `query`
# Uses st.cache_data to only rerun when the query changes or after 10 min.
90
-
@st.cache_data(ttl=600)
91
-
defrun_query(query):
92
-
with conn.cursor() as cur:
93
-
cur.execute(query)
94
-
return cur.fetchall()
95
-
96
-
rows = run_query("SELECT * from mytable;")
85
+
df = conn.query('SELECT * FROM mytable;', ttl="10m")
97
86
98
87
# Print results.
99
-
for row inrows:
100
-
st.write(f"{row[0]} has a :{row[1]}:")
88
+
for row indf.itertuples():
89
+
st.write(f"{row.name} has a :{row.pet}:")
101
90
```
102
91
103
-
See `st.cache_data` above? Without it, Streamlit would run the query every time the app reruns (e.g. on a widget interaction). With `st.cache_data`, it only runs when the query changes or after 10 minutes (that's what `ttl` is for). Watch out: If your database updates more frequently, you should adapt `ttl` or remove caching so viewers always see the latest data. Learn more in [Caching](/library/advanced-features/caching).
92
+
See `st.experimental_connection` above? This handles secrets retrieval, setup, query caching and retries. By default, `query()` results are cached without expiring. In this case, we set `ttl="10m"` to ensure the query result is cached for no longer than 10 minutes. You can also set `ttl=0` to disable caching. Learn more in [Caching](/library/advanced-features/caching).
104
93
105
94
If everything worked out (and you used the example table we created above), your app should look like this:
This guide explains how to securely access a Supabase instance from Streamlit Community Cloud. It uses the [Supabase Python Client Library](https://github.com/supabase-community/supabase-py) and Streamlit's [Secrets management](/streamlit-community-cloud/deploy-your-app/secrets-management). Supabase is the open source Firebase alternative and is based on PostgreSQL.
10
+
This guide explains how to securely access a Supabase instance from Streamlit Community Cloud. It uses [st.experimental_connection](/library/api-reference/connections/st.experimental_connection), [Streamlit Supabase Connector](https://github.com/SiddhantSadangi/st_supabase_connection/tree/main) (a community-built connection developed by [@SiddhantSadangi](https://github.com/SiddhantSadangi)) and Streamlit's [Secrets management](/streamlit-community-cloud/deploy-your-app/secrets-management). Supabase is the open source Firebase alternative and is based on PostgreSQL.
11
+
12
+
<Note>
13
+
14
+
Community-built connections, such as the [Streamlit Supabase Connector](https://github.com/SiddhantSadangi/st_supabase_connection/tree/main), extend and build on the `st.experimental_connection` interface and make it easier than ever to build Streamlit apps with a wide variety of data sources. These type of connections work exactly the same as [the ones built into Streamlit](/library/api-reference/connections) and have access to all the same capabilities.
15
+
16
+
</Note>
11
17
12
18
## Sign in to Supabase and create a project
13
19
@@ -68,13 +74,14 @@ With your Supabase database created, you can now connect to it from Streamlit!
68
74
69
75
### Add Supabase Project URL and API key to your local app secrets
70
76
71
-
Your local Streamlit app will read secrets from a file `.streamlit/secrets.toml` in your app's root directory. Create this file if it doesn't exist yet and add the `supabase_url` and `supabase_key` here:
77
+
Your local Streamlit app will read secrets from a file `.streamlit/secrets.toml` in your app's root directory. Create this file if it doesn't exist yet and add the `SUPABASE_URL` and `SUPABASE_KEY` here:
72
78
73
79
```toml
74
80
# .streamlit/secrets.toml
75
81
76
-
supabase_url = "xxxx"
77
-
supabase_key = "xxxx"
82
+
[connections.supabase]
83
+
SUPABASE_URL = "xxxx"
84
+
SUPABASE_KEY = "xxxx"
78
85
```
79
86
80
87
Replace `xxxx` above with your Project URL and API key from [Step 1](/knowledge-base/tutorials/databases/supabase#sign-in-to-supabase-and-create-a-project).
@@ -91,15 +98,23 @@ As the `secrets.toml` file above is not committed to GitHub, you need to pass it
## Add st-supabase-connection to your requirements file
95
102
96
-
Add the [`supabase`](https://github.com/supabase-community/supabase-py) Python Client Library to your `requirements.txt` file, preferably pinning its version (replace `x.x.x` with the version you want installed):
103
+
Add the [`st-supabase-connection`](https://pypi.org/project/st-supabase-connection/) community-built connection library to your `requirements.txt` file, preferably pinning its version (replace `x.x.x` with the version you want installed):
97
104
98
105
```bash
99
106
# requirements.txt
100
-
supabase==x.x.x
107
+
st-supabase-connection==x.x.x
101
108
```
102
109
110
+
<Tip>
111
+
112
+
We've used the `st-supabase-connection` library here in combination with `st.experimental_connection` to benefit from the ease of setting up the data connection, managing your credentials, and Streamlit's caching capabilities that native and community-built connections provide.
113
+
114
+
You can however still directly use the [Supabase Python Client Library](https://pypi.org/project/supabase/) library if you prefer, but you'll need to write more code to set up the connection and cache the results. See [Using the Supabase Python Client Library](/knowledge-base/tutorials/databases/supabase#using-the-supabase-python-client-library) below for an example.
115
+
116
+
</Tip>
117
+
103
118
## Write your Streamlit app
104
119
105
120
Copy the code below to your Streamlit app and run it.
@@ -108,36 +123,83 @@ Copy the code below to your Streamlit app and run it.
See `st.cache_data` above? Without it, Streamlit would run the query every time the app reruns (e.g. on a widget interaction). With `st.cache_data`, it only runs when the query changes or after 10 minutes (that's what `ttl` is for). Watch out: If your database updates more frequently, you should adapt `ttl` or remove caching so viewers always see the latest data. Learn more in [Caching](/library/advanced-features/caching).
140
+
See `st.experimental_connection` above? This handles secrets retrieval, setup, query caching and retries. By default, `query()` results are cached without expiring. In this case, we set `ttl="10m"` to ensure the query result is cached for no longer than 10 minutes. You can also set `ttl=0` to disable caching. Learn more in [Caching](/library/advanced-features/caching).
138
141
139
142
If everything worked out (and you used the example table we created above), your app should look like this:
As Supabase uses PostgresSQL under the hood, you can also connect to Supabase by using the connection string Supabase provides under Settings > Databases. From there, you can refer to the [PostgresSQL tutorial](/knowledge-base/tutorials/databases/postgresql) to connect to your database.
147
+
148
+
## Using the Supabase Python Client Library
149
+
150
+
If you prefer to use the [Supabase Python Client Library](https://pypi.org/project/supabase/) directly, you can do so by following the steps below.
151
+
152
+
1. Add your Supabase Project URL and API key to your local app secrets:
153
+
154
+
Your local Streamlit app will read secrets from a file `.streamlit/secrets.toml` in your app's root directory. Create this file if it doesn't exist yet and add the SUPABASE_URL and SUPABASE_KEY here:
155
+
156
+
```toml
157
+
# .streamlit/secrets.toml
158
+
159
+
SUPABASE_URL = "xxxx"
160
+
SUPABASE_KEY = "xxxx"
161
+
```
162
+
163
+
2. Add `supabase` to your requirements file:
164
+
165
+
Add the [`supabase`](https://github.com/supabase-community/supabase-py) Python Client Library to your `requirements.txt` file, preferably pinning its version (replace `x.x.x` with the version you want installed):
166
+
167
+
```bash
168
+
# requirements.txt
169
+
supabase==x.x.x
170
+
```
171
+
172
+
3. Write your Streamlit app:
173
+
174
+
Copy the code below to your Streamlit app and run it.
175
+
176
+
```python
177
+
# streamlit_app.py
178
+
179
+
import streamlit as st
180
+
from supabase import create_client, Client
181
+
182
+
# Initialize connection.
183
+
# Uses st.cache_resource to only run once.
184
+
@st.cache_resource
185
+
definit_connection():
186
+
url = st.secrets["SUPABASE_URL"]
187
+
key = st.secrets["SUPABASE_KEY"]
188
+
return create_client(url, key)
189
+
190
+
supabase = init_connection()
191
+
192
+
# Perform query.
193
+
# Uses st.cache_data to only rerun when the query changes or after 10 min.
See `st.cache_data` above? Without it, Streamlit would run the query every time the app reruns (e.g. on a widget interaction). With `st.cache_data`, it only runs when the query changes or after 10 minutes (that's what `ttl` is for). Watch out: If your database updates more frequently, you should adapt `ttl` or remove caching so viewers always see the latest data. Learn more in [Caching](/library/advanced-features/caching).
0 commit comments