|
| 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 | + |
0 commit comments