Part Tracker with Databricks Apps

# Building an Intelligent Supply Chain App with Databricks

In today’s dynamic supply chain environment, data visibility and agility are everything. From part shortages to unexpected demand surges, organizations need real-time insights to keep operations moving. That’s where Databricks Apps come in.

With the new Databricks Apps framework, you can build and deploy interactive applications directly in your Databricks workspace. No extra servers, no infrastructure overhead—just a simple way to create apps that connect to your governed data in Unity Catalog and deliver insights to business users.

In this post, we’ll walk through how to create a Supply Chain Tracker App on Databricks—helping operations, sourcing, and logistics teams answers questions such as "Where's My Part"?

---

## Why Databricks Apps for Supply Chain?

Traditional dashboards (Power BI, Tableau, etc.) are great for static reporting, but supply chain teams often need more:

- Real-time interactivity → Drill into specific part numbers, suppliers, or sites.

- Native to Databricks → Direct access to Unity Catalog, Delta tables, and MLflow models.

- Zero DevOps → Runs serverless; no need to manage VMs, Docker, or custom APIs.

- Governance built-in → Apps respect the same permissions as your Databricks workspace.

Imagine a sourcing manager typing in a part number and instantly seeing: demand forecasts, supplier availability, and consumption trends—all in a single, polished app.

---

## What We’ll Build

A lightweight Plotly Dash app that:

- Connects to a Databricks SQL Warehouse

- Queries a Unity Catalog table containing parts and demand data

- Renders a searchable data table and chart

- Provides a sidebar navigation for analytics and reports

- Is styled with custom HTML & CSS for a clean, professional look

Think of it as the starting point for an intelligent supply chain control tower.

---

## Step 1: Set Up Prerequisites

Before we dive into code, make sure you have:

- Unity Catalog enabled in your workspace

- A Databricks SQL Warehouse with proper permissions

- Workspace entitlement to use Databricks Apps

- Required Python packages (`dash`, plotly, pandas, databricks-sql-connector)

---

## Step 2: Create the App in Databricks

1. In your workspace, go to Apps → Create app

2. Choose Blank (Python)

3. Name your app (e.g., supplychain-tracker)

4. Runtime: Serverless Python (default)

5. Upload three files:

- app.py (the Dash app code)

- requirements.txt (Python dependencies)

- assets/styles.css (styling)

6. Add environment variables:

- DATABRICKS_HOST

- DATABRICKS_HTTP_PATH

- DATABRICKS_TOKEN (use Secrets in production)

Click Save → Deploy → Open.

---

## Step 3: Core App Layout

With the files created and the environment configured, the next step is to define the structure of the app.

We’ll use Dash to build the UI, with a sidebar for navigation and a main content area that displays the selected page.

Here’s a simplified version of the layout:

```python

app.layout = html.Div([

# Sidebar

html.Aside([

html.H2("📦 SupplyChain", className="sidebar-title"),

html.Nav([

dcc.Link("🏠 Home", href="/", className="nav-link", id="nav-home"),

dcc.Link("📊 Analytics", href="/analytics", className="nav-link", id="nav-analytics"),

dcc.Link("📑 Reports", href="/reports", className="nav-link", id="nav-reports")

], className="sidebar-nav")

], className="app-sidebar"),

# Main content

html.Main(id="page-content", className="app-main"),

# Hidden URL tracker

dcc.Location(id="url", refresh=False)

], className="app-container")

## Step 4: Query Unity Catalog

With the app layout in place, the next step is to connect it to your governed data in Unity Catalog.

The Databricks Apps framework makes this seamless because apps can securely authenticate and query data using the Databricks SQL Connector for Python.

This ensures:

- ✅ Governed access — queries respect Unity Catalog permissions (USAGE, SELECT).

- ✅ Security — credentials are managed with environment variables and Databricks Secrets.

- ✅ Scalability — queries run on Databricks SQL Warehouses, not inside the app server.

---

### Example: Querying Parts Inventory

The snippet below demonstrates how to connect to a Databricks SQL Warehouse and fetch supply chain data for a given part number:

```python

from databricks.sdk.core import Config

from databricks import sql

# Initialize Databricks config (picks up env vars for host, http_path, token)

cfg = Config()

# Connect to Databricks SQL Warehouse

with sql.connect(

server_hostname=cfg.host,

http_path="/sql/1.0/warehouses/your-warehouse-id",

credentials_provider=lambda: cfg.authenticate

) as connection:

cursor = connection.cursor()

# Example: Fetch supply chain details for a specific part

part_number = 12345

cursor.execute(

"""

SELECT

part_number,

supplier_id,

available_qty,

forecast_demand,

last_updated

FROM catalog.schema.parts_inventory

WHERE part_number = ?

""",

[part_number]

)

rows = cursor.fetchall()

cols = [desc[0] for desc in cursor.description]

# Convert results to a list of dictionaries for easy display in Dash

results = [dict(zip(cols, row)) for row in rows]

### Integrating with Dash

Once the query returns results, you can display them in a Dash DataTable. For example:

from dash import dash_table, html

if results:

table = dash_table.DataTable(

columns=[{"name": c, "id": c} for c in results[0].keys()],

data=results,

style_table={"width": "90%", "margin": "20px auto"},

style_cell={"textAlign": "left", "padding": "10px"},

style_header={"fontWeight": "bold", "backgroundColor": "#e3f2fd"}

)

else:

table = html.P(f"⚠️ No records found for part number {part_number}.")

## Step 5: Style with HTML and CSS

A clean, professional design is critical if business users are going to rely on your app every day.

By default, Dash apps are functional but plain. To give the app a polished look, we’ll add a stylesheet under assets/styles.css.

Dash automatically serves any CSS files stored in the assets/ folder, so no extra configuration is needed.

---

### Example: Stylesheet (`assets/styles.css`)

```css

/* General layout */

.app-container {

display: flex;

height: 100vh;

font-family: 'Segoe UI', Tahoma, sans-serif;

}

/* Sidebar */

.app-sidebar {

width: 220px;

background: #1e3a8a; /* Navy blue */

color: white;

padding: 20px;

display: flex;

flex-direction: column;

}

.sidebar-title {

margin: 0 0 20px;

font-size: 1.3rem;

text-align: center;

}

.sidebar-nav {

display: flex;

flex-direction: column;

}

.nav-link {

color: white;

text-decoration: none;

padding: 12px;

margin: 5px 0;

border-radius: 6px;

transition: background 0.2s;

}

.nav-link:hover {

background: #3b82f6; /* Lighter blue on hover */

}

/* Main content */

.app-main {

flex: 1;

padding: 20px;

background: #f9fafc;

overflow-y: auto;

}

/* Header */

.app-header {

text-align: center;

margin-bottom: 30px;

}

.app-title {

font-size: 2rem;

margin: 0;

}

.subtitle {

font-size: 1rem;

color: #555;

}

/* Inputs */

.input-section {

text-align: center;

margin: 20px auto;

}

.input-label {

display: block;

font-weight: bold;

margin-bottom: 8px;

}

.input-box {

padding: 10px;

border-radius: 8px;

border: 1px solid #ccc;

margin-right: 8px;

}

.btn-primary {

padding: 10px 20px;

background: #2563eb;

border: none;

border-radius: 8px;

color: white;

cursor: pointer;

}

.btn-primary:hover {

background: #1d4ed8;

}

/* Results */

.results {

margin-top: 20px;

}

## Step 6: Deploy and Share

Once your app is built and styled, the final step is to deploy it so that business users can access it directly from Databricks.

The deployment process is straightforward—no servers to manage, no containers to configure.

---

### Deployment Steps

1. Upload Files

- Go to your app in the Databricks workspace.

- In the Files tab, upload:

- app.py

- requirements.txt

- assets/styles.css

2. Set Environment Variables

- In Settings → Environment Variables, add:

- DATABRICKS_HOST → workspace URL

- DATABRICKS_HTTP_PATH → SQL Warehouse HTTP path

- DATABRICKS_TOKEN → Access token (or reference a Databricks Secret in production)

- Optionally add TABLE_FQN for the fully qualified table name (`catalog.schema.table`).

3. Deploy

- Click Deploy.

- Wait for the status to change to Running.

- Click Open to launch the app.

4. Set Permissions

- Open the Permissions panel.

- Grant users or groups the ability to View or Run the app.

- Choose the Run as mode:

- Owner → runs as you (quick setup).

- Viewer → runs with the user’s own credentials.

- Service Principal → recommended for production apps with shared access.

---

### Troubleshooting Tips

- 401/403 errors → Ensure the app’s run-as identity has USAGE on the catalog and schema, and SELECT on the table.

- Warehouse stopped → Start the SQL Warehouse, or enable auto-start in settings.

- Long tables render slowly → Limit rows in queries or implement server-side filtering.

- Assets not loading → Confirm files are inside the assets/ folder and don’t begin with /.

---

### Why This Matters

Deploying inside Databricks means:

- No need to manage VMs, Docker, or external web servers.

- Apps run serverless with the same governance and security as the rest of your Databricks workspace.

- Users get a simple URL they can bookmark or share, unlocking real-time supply chain visibility with minimal IT overhead.

## Final Thoughts

Databricks Apps turn the Lakehouse into a platform not just for data teams, but for the business operators who rely on data every day.

With just a few files, you can empower your supply chain teams with real-time, interactive insights—all within the same platform where your data already lives.

The intelligent supply chain isn’t just about having more data—it’s about making that data accessible, actionable, and trusted. Databricks Apps are the missing link to make it happen.

To view or add a comment, sign in

More articles by Ben Schmirler

Others also viewed

Explore content categories