Skip to content

Commit

Permalink
sample app
Browse files Browse the repository at this point in the history
  • Loading branch information
neelasha23 committed May 20, 2024
1 parent 345c10c commit 7039999
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/streamlit/db-connection/README.md
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.
22 changes: 22 additions & 0 deletions examples/streamlit/db-connection/app.py
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)
2 changes: 2 additions & 0 deletions examples/streamlit/db-connection/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
streamlit
SQLAlchemy

0 comments on commit 7039999

Please sign in to comment.