Skip to content

Adding Geo input and visualization #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions streamlit/view_groups.py
Original file line number Diff line number Diff line change
@@ -105,6 +105,36 @@
},
],
},

{
"title": "Alerts",
"views": [
{
"label": "List alerts",
"help": "List all alerts.",
"page": "views/alerts.py",
"icon": ":material/notifications_none:",
},
],
},
{
"title": "Geo Visualization",
"views": [
{
"label": "Display Geo Input",
"help": "Display geo information on a map.",
"page": "views/maps_display.py",
"icon": ":material/globe:",
},
{
"label": "User Geo Input",
"help": "Enable users to select own geo input.",
"page": "views/maps_draw.py",
"icon": ":material/pin_drop:",
},
],
},

{
"title": "Unity Catalog",
"views": [
@@ -133,16 +163,4 @@
},
],
},

{
"title": "Unity Catalog",
"views": [
{
"label": "Get Catalogs",
"help": "Get meta data.",
"page": "views/unity_catalog_get.py",
"icon": ":material/lan:",
},
],
}
]
41 changes: 41 additions & 0 deletions streamlit/views/alerts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import streamlit as st
from databricks.sdk import WorkspaceClient
import pandas as pd

w = WorkspaceClient()

def get_all_alerts():
alerts = [alert.__dict__ for alert in w.alerts.list()]
return pd.DataFrame(alerts)

st.header("List alert", divider=True)
st.subheader("Enable users to display all alerts")
st.write("This receipt enables users to display all alerts.")

tab_a, tab_b, tab_c = st.tabs(["**Try it**", "**Code snippet**", "**Requirements**"])

with tab_a:
st.write("""### See all alerts""")
st.dataframe(get_all_alerts())

table = [
{
"type": "Get alerts",
"param": "",
"description": "Get all alerts",
"code": """
```python
def get_all_alerts():
alerts = [alert.__dict__ for alert in w.alerts.list()]
return pd.DataFrame(alerts)
```
""",
},
]


with tab_b:
for i, row in enumerate(table):
with st.expander(f"**{row['type']} ({row['param']})**", expanded=(i == 0)):
st.markdown(f"**Description**: {row['description']}")
st.markdown(row["code"])
130 changes: 130 additions & 0 deletions streamlit/views/maps_display.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@

import streamlit as st
from databricks import sql
from databricks.sdk.core import Config
import pandas as pd


st.header("Geo Visualization", divider=True)
st.subheader("Read a table and display as a map")
st.write("This receipt loads a table from a delta table and displays the data on a map.")

cfg = Config()

@st.cache_resource
def get_connection(http_path):
return sql.connect(
server_hostname=cfg.host,
http_path=http_path,
credentials_provider=lambda: cfg.authenticate,
)


def read_table(table_name, conn):
with conn.cursor() as cursor:
query = f"SELECT * FROM {table_name}"
cursor.execute(query)
return cursor.fetchall_arrow().to_pandas()

cities = [
{"name": "New York", "latitude": 40.7128, "longitude": -74.0060},
{"name": "Los Angeles", "latitude": 34.0522, "longitude": -118.2437},
{"name": "London", "latitude": 51.5074, "longitude": -0.1278},
{"name": "Tokyo", "latitude": 35.6895, "longitude": 139.6917},
{"name": "Sydney", "latitude": -33.8688, "longitude": 151.2093},
{"name": "Paris", "latitude": 48.8566, "longitude": 2.3522},
{"name": "Dubai", "latitude": 25.276987, "longitude": 55.296249},
{"name": "Rio de Janeiro", "latitude": -22.9068, "longitude": -43.1729},
{"name": "Moscow", "latitude": 55.7558, "longitude": 37.6173},
{"name": "Cape Town", "latitude": -33.9249, "longitude": 18.4241}
]

data = pd.DataFrame(cities)


tab_a, tab_b, tab_c, tab_d = st.tabs(["**Try it**","**Try it with a delta table**", "**Code snippet**", "**Requirements**"])

with tab_a:
col1, col2 = st.columns(2)
with col1:
if st.button("Try It with a random sample"):
st.map(data, latitude="Latitude", longitude="Longitude")
st.dataframe(data)


with tab_b:
col1, col2 = st.columns(2)
with col1:
http_path_input = st.text_input(
"Enter your Databricks HTTP Path:", placeholder="/sql/1.0/warehouses/xxxxxx"
)

table_name = st.text_input(
"Specify a Unity Catalog table name:", placeholder="catalog.schema.table"
)
st.info("For displaying a sample, please use the table samples.accuweather.forecast_daily_calendar_metric")

if http_path_input and table_name:
conn = get_connection(http_path_input)
df = read_table(table_name, conn)

st.dataframe(df)

if 'latitude' in df.columns and 'longitude' in df.columns:
df['latitude'] = pd.to_numeric(df['latitude'], errors='coerce')
df['longitude'] = pd.to_numeric(df['longitude'], errors='coerce')
df = df.dropna(subset=['latitude', 'longitude'])

if not df.empty:
st.map(df, latitude="latitude", longitude="longitude")
else:
st.warning("no longitude, latitude found in the table")


table = [
{
"type": "Get Tables",
"param": "Get long lat from the tables",
"description": "Get long lat from the tables.",
"code": """
```python
def read_table(table_name, conn):
with conn.cursor() as cursor:
query = f"SELECT * FROM {table_name} LIMIT 1000"
cursor.execute(query)
return cursor.fetchall_arrow().to_pandas()
```
""",
},
{
"type": "Display Maps",
"param": "Display pandas df as maü",
"description": "Display the streamlit map",
"code": """
```python
conn = get_connection(http_path_input)
df = read_table(table_name, conn)

st.dataframe(df)

if 'latitude' in df.columns and 'longitude' in df.columns:
df['latitude'] = pd.to_numeric(df['latitude'], errors='coerce')
df['longitude'] = pd.to_numeric(df['longitude'], errors='coerce')
df = df.dropna(subset=['latitude', 'longitude'])

if not df.empty:
st.map(df, latitude="latitude", longitude="longitude")
else:
st.warning("no longitude, latitude found in the table")


```
""",
},
]

with tab_c:
for i, row in enumerate(table):
with st.expander(f"**{row['type']} ({row['param']})**", expanded=(i == 0)):
st.markdown(f"**Description**: {row['description']}")
st.markdown(row["code"])
Loading
Oops, something went wrong.