Skip to content

Commit 40efea3

Browse files
proddataamotl
authored andcommitted
Framework/Streamlit: Add basic example about Streamlit, reading sys.summits
1 parent 7c8d99b commit 40efea3

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# .streamlit/secrets.toml
2+
3+
[connections.cratedb]
4+
dialect = "crate"
5+
host = "localhost"
6+
port = "4200"
7+
username = "crate"
8+
password = ""

framework/streamlit/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Streamlit with CrateDB Example
2+
3+
## About
4+
Demonstrate connectivity from Streamlit to CrateDB.
5+
6+
## Configuration
7+
Configure database connection address and credentials within
8+
`.streamlit/secrets.toml` in your working directory. Please
9+
make sure to use valid credentials matching your environment.
10+
11+
## Usage
12+
To start a CrateDB instance on your machine, invoke:
13+
```shell
14+
docker run -it --rm \
15+
--publish=4200:4200 --publish=5432:5432 \
16+
--env=CRATE_HEAP_SIZE=2g \
17+
crate:latest -Cdiscovery.type=single-node
18+
```
19+
20+
Install dependencies.
21+
```shell
22+
pip install -r requirements.txt
23+
```
24+
25+
Invoke Streamlit to serve the application.
26+
```shell
27+
streamlit run basic_sys_summits.py
28+
```
29+
30+
## Screenshot
31+
32+
Enjoy the list of mountains.
33+
34+
![image](https://github.com/crate/cratedb-examples/assets/453543/7dc54224-06d0-4cfb-a5e0-b216c03bf3d2)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
A basic Streamlit application connecting to CrateDB using SQLAlchemy.
3+
4+
It reads the built-in `sys.summits` table into a dataframe, and
5+
displays its contents.
6+
7+
- https://docs.streamlit.io/develop/tutorials/databases
8+
- https://cratedb.com/docs/sqlalchemy-cratedb/
9+
"""
10+
import streamlit as st
11+
12+
# Set a title for the page.
13+
st.title("Streamlit with CrateDB Example")
14+
15+
# Connect to CrateDB, and read the built-in `sys.summits` table.
16+
conn = st.connection("cratedb", type="sql")
17+
df = conn.query('SELECT * FROM "sys"."summits";', ttl="10m")
18+
19+
# Output data as dataframe and table.
20+
st.dataframe(df)
21+
st.table(df)
22+
23+
# Output data as Markdown.
24+
for row in df.itertuples():
25+
st.write(f"{row.mountain}")

framework/streamlit/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
streamlit==1.*
2+
sqlalchemy-cratedb==0.38.0

0 commit comments

Comments
 (0)