Skip to content

Commit 509640d

Browse files
authored
Merge pull request #13 from Robert-Ziegltrum/geo_visualization_and_input
Adding Geo input and visualization
2 parents fa7e72e + 50c6991 commit 509640d

File tree

7 files changed

+888
-1
lines changed

7 files changed

+888
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"label": "Visualizations",
3+
"position": 5,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Display data and collect user input using charts and maps."
7+
}
8+
}
9+
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# Charts
6+
7+
Use this recipe to visualize data using Streamlit's built-in chart components: area charts, line charts, and bar charts. This example demonstrates loading data from a Unity Catalog table and creating various business insights through different chart visualizations.
8+
9+
## Code snippet
10+
11+
### Load data from a table
12+
13+
```python title="app.py"
14+
import streamlit as st
15+
from databricks import sql
16+
from databricks.sdk.core import Config
17+
from databricks.sdk import WorkspaceClient
18+
import pandas as pd
19+
20+
cfg = Config()
21+
w = WorkspaceClient()
22+
23+
# List available SQL warehouses
24+
warehouses = w.warehouses.list()
25+
warehouse_paths = {wh.name: wh.odbc_params.path for wh in warehouses}
26+
27+
# Connect to SQL warehouse
28+
@st.cache_resource
29+
def get_connection(http_path):
30+
return sql.connect(
31+
server_hostname=cfg.host,
32+
http_path=http_path,
33+
credentials_provider=lambda: cfg.authenticate,
34+
)
35+
36+
# Read table
37+
def read_table(table_name, conn):
38+
with conn.cursor() as cursor:
39+
cursor.execute(f"SELECT * FROM {table_name} LIMIT 1000")
40+
return cursor.fetchall_arrow().to_pandas()
41+
42+
# Get data
43+
warehouse_name = "your_warehouse_name"
44+
table_name = "samples.nyctaxi.trips"
45+
46+
http_path = warehouse_paths[warehouse_name]
47+
conn = get_connection(http_path)
48+
df = read_table(table_name, conn)
49+
50+
# Process datetime columns
51+
df["tpep_pickup_datetime"] = pd.to_datetime(df["tpep_pickup_datetime"])
52+
df["tpep_dropoff_datetime"] = pd.to_datetime(df["tpep_dropoff_datetime"])
53+
df["pickup_hour"] = df["tpep_pickup_datetime"].dt.hour
54+
df["trip_duration_minutes"] = (df["tpep_dropoff_datetime"] - df["tpep_pickup_datetime"]).dt.total_seconds() / 60
55+
```
56+
57+
### Demand analysis: Trips by hour
58+
59+
```python title="app.py"
60+
import streamlit as st
61+
62+
# Count trips by hour to understand demand patterns
63+
hourly_demand = df["pickup_hour"].value_counts().sort_index()
64+
st.bar_chart(hourly_demand)
65+
66+
peak_hour = hourly_demand.idxmax()
67+
st.info(f"Peak demand hour: {peak_hour}:00 with {hourly_demand.max()} trips")
68+
```
69+
70+
### Revenue analysis: Average fare by hour
71+
72+
```python title="app.py"
73+
import streamlit as st
74+
75+
# Analyze when fares are highest
76+
avg_fare_by_hour = df.groupby("pickup_hour")["fare_amount"].mean()
77+
st.line_chart(avg_fare_by_hour)
78+
79+
best_hour = avg_fare_by_hour.idxmax()
80+
st.success(f"Best earning hour: {best_hour}:00")
81+
```
82+
83+
### Location analysis: Top pickup zones
84+
85+
```python title="app.py"
86+
import streamlit as st
87+
88+
# Identify high-demand pickup locations
89+
top_pickups = df["pickup_zip"].value_counts().head(15)
90+
st.bar_chart(top_pickups)
91+
```
92+
93+
### Cumulative revenue over time
94+
95+
```python title="app.py"
96+
import streamlit as st
97+
98+
# Track total revenue accumulation
99+
revenue_df = df.set_index("tpep_pickup_datetime")[["fare_amount"]].sort_index()
100+
revenue_df["cumulative_revenue"] = revenue_df["fare_amount"].cumsum()
101+
st.area_chart(revenue_df["cumulative_revenue"])
102+
```
103+
104+
## Resources
105+
106+
- [SQL warehouse](https://docs.databricks.com/aws/en/compute/sql-warehouse/)
107+
- [Unity Catalog table](https://docs.databricks.com/aws/en/tables/)
108+
109+
## Permissions
110+
111+
Your [app service principal](https://docs.databricks.com/aws/en/dev-tools/databricks-apps/#how-does-databricks-apps-manage-authorization) needs the following permissions:
112+
113+
- `CAN USE` on the SQL warehouse
114+
- `SELECT` on the Unity Catalog table
115+
116+
See Unity [Catalog privileges and securable objects](https://docs.databricks.com/aws/en/data-governance/unity-catalog/manage-privileges/privileges) for more information.
117+
118+
## Dependencies
119+
120+
- [Streamlit](https://pypi.org/project/streamlit/) - `streamlit`
121+
- [Databricks SDK](https://pypi.org/project/databricks-sdk/) - `databricks-sdk`
122+
- [Databricks SQL Connector](https://pypi.org/project/databricks-sql-connector/) - `databricks-sql-connector`
123+
- [Pandas](https://pypi.org/project/pandas/) - `pandas`
124+
125+
```python title="requirements.txt"
126+
streamlit
127+
databricks-sdk
128+
databricks-sql-connector
129+
pandas
130+
```
131+
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
# Map display and interaction
6+
7+
This recipe enables you to display geographic data on a map and collect user geo input through interactive map drawing. You can load location data from Unity Catalog tables or use the drawing tools to capture points, polygons, and geofences from users.
8+
9+
## Code snippet
10+
11+
### Display geo data from a table
12+
13+
```python title="app.py"
14+
import streamlit as st
15+
from databricks import sql
16+
from databricks.sdk.core import Config
17+
from databricks.sdk import WorkspaceClient
18+
import pandas as pd
19+
20+
cfg = Config()
21+
w = WorkspaceClient()
22+
23+
# List available SQL warehouses
24+
warehouses = w.warehouses.list()
25+
warehouse_paths = {wh.name: wh.odbc_params.path for wh in warehouses}
26+
27+
# Connect to SQL warehouse
28+
def get_connection(http_path):
29+
return sql.connect(
30+
server_hostname=cfg.host,
31+
http_path=http_path,
32+
credentials_provider=lambda: cfg.authenticate,
33+
)
34+
35+
# Read table
36+
def read_table(table_name, conn):
37+
with conn.cursor() as cursor:
38+
cursor.execute(f"SELECT * FROM {table_name}")
39+
return cursor.fetchall_arrow().to_pandas()
40+
41+
# Get data and display on map
42+
warehouse_name = "your_warehouse_name"
43+
table_name = "samples.accuweather.forecast_daily_calendar_metric"
44+
45+
http_path = warehouse_paths[warehouse_name]
46+
conn = get_connection(http_path)
47+
df = read_table(table_name, conn)
48+
49+
# Display map with latitude/longitude columns
50+
st.map(df, latitude="latitude", longitude="longitude")
51+
```
52+
53+
### Collect user geo input
54+
55+
```python title="app.py"
56+
import streamlit as st
57+
from streamlit_folium import st_folium
58+
import folium
59+
from folium.plugins import Draw
60+
61+
# Create a map centered on a location
62+
m = folium.Map(location=[37.7749, -122.4194], zoom_start=13)
63+
64+
# Enable drawing tools (set True for the tools you want to enable)
65+
draw = Draw(
66+
draw_options={
67+
"marker": True, # For collecting points
68+
"polygon": True, # For collecting geofences/polygons
69+
"polyline": True, # For collecting polylines
70+
"rectangle": True, # For collecting rectangles
71+
"circle": True, # For collecting circles
72+
"circlemarker": False,
73+
},
74+
edit_options={"edit": True},
75+
)
76+
draw.add_to(m)
77+
output = st_folium(m, width=700, height=500)
78+
79+
# Access the drawn geometry
80+
if output["last_active_drawing"] and "geometry" in output["last_active_drawing"]:
81+
geometry = output["last_active_drawing"]["geometry"]
82+
st.json(geometry)
83+
```
84+
85+
## Resources
86+
87+
- [SQL warehouse](https://docs.databricks.com/aws/en/compute/sql-warehouse/) _(optional, only for reading table data)_
88+
- [Unity Catalog table](https://docs.databricks.com/aws/en/tables/) _(optional, only for reading table data)_
89+
90+
## Permissions
91+
92+
Your [app service principal](https://docs.databricks.com/aws/en/dev-tools/databricks-apps/#how-does-databricks-apps-manage-authorization) needs the following permissions:
93+
94+
- `CAN USE` on the SQL warehouse _(only required if reading data from tables)_
95+
- `SELECT` on the Unity Catalog table _(only required if reading data from tables)_
96+
97+
See Unity [Catalog privileges and securable objects](https://docs.databricks.com/aws/en/data-governance/unity-catalog/manage-privileges/privileges) for more information.
98+
99+
## Dependencies
100+
101+
- [Streamlit](https://pypi.org/project/streamlit/) - `streamlit`
102+
- [Streamlit Folium](https://pypi.org/project/streamlit-folium/) - `streamlit-folium`
103+
- [Databricks SDK](https://pypi.org/project/databricks-sdk/) - `databricks-sdk` _(for table data)_
104+
- [Databricks SQL Connector](https://pypi.org/project/databricks-sql-connector/) - `databricks-sql-connector` _(for table data)_
105+
106+
```python title="requirements.txt"
107+
streamlit
108+
streamlit-folium
109+
databricks-sdk
110+
databricks-sql-connector
111+
```
112+

streamlit/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ databricks-connect==16.0.0
22
databricks-sdk[openai]==0.60.0
33
databricks-sql-connector==4.0.0
44
pandas==2.2.3
5-
streamlit==1.41.1
65
psycopg[binary]==3.2.9
76
psycopg-pool==3.2.6
7+
streamlit==1.41.1
8+
streamlit-folium==0.25.3

streamlit/view_groups.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,22 @@
152152
],
153153
},
154154
{
155+
"title": "Visualizations",
156+
"views": [
157+
{
158+
"label": "Charts",
159+
"help": "Visualize data using Streamlit's built-in chart components.",
160+
"page": "views/visualizations_charts.py",
161+
"icon": ":material/bar_chart:",
162+
},
163+
{
164+
"label": "Map display and interaction",
165+
"help": "Display geo information on a map and allow users to draw on the map.",
166+
"page": "views/visualizations_map.py",
167+
"icon": ":material/globe:",
168+
},
169+
],
170+
},
155171
"title": "External services",
156172
"views": [
157173
{

0 commit comments

Comments
 (0)