-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
345c10c
commit 7039999
Showing
3 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# DB Connection | ||
|
||
This is a sample app for demonstrating DB connections in Streamlit. To run this app ensure you set the environment variable: | ||
|
||
```sh | ||
export DB_URI=<uri> | ||
``` | ||
|
||
Note that this URI is a [SQLAlchemy URI](https://docs.sqlalchemy.org/en/20/core/engines.html#database-urls). | ||
|
||
## Deployment | ||
|
||
When deploying this app on Ploomber Cloud you need to set the DB_URI as a secret. Refer to the [documentation](https://docs.cloud.ploomber.io/en/latest/user-guide/secrets.html) to learn more. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import streamlit as st | ||
from sqlalchemy.sql import text | ||
from os import environ | ||
|
||
# Create the SQL connection to pets_db as specified in your secrets file. | ||
conn = st.connection(name="sqlite", type='sql', url=environ["DB_URI"]) | ||
|
||
# Insert some data with conn.session. | ||
with conn.session as s: | ||
s.execute(text('CREATE TABLE IF NOT EXISTS numbers (x INT, y INT);')) | ||
s.execute(text('DELETE FROM numbers;')) | ||
num_pairs = {'1': '1', '2': '4', '3': '9'} | ||
for k in num_pairs: | ||
s.execute( | ||
text('INSERT INTO numbers (x, y) VALUES (:numx, :numy);'), | ||
params=dict(numx=k, numy=num_pairs[k]) | ||
) | ||
s.commit() | ||
|
||
# Query and display the data you inserted | ||
numbers = conn.query('select * from numbers') | ||
st.dataframe(numbers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
streamlit | ||
SQLAlchemy |