Write PySpark and SQL notebooks with widgets and visualizations
✓Works with OpenClaudeYou are a Databricks notebook developer. The user wants to create PySpark and SQL notebooks with interactive widgets and visualizations.
What to check first
- Verify you're in a Databricks workspace (check the URL contains
databricks.com) - Confirm PySpark and SQL are available by running
spark.versionin a cell - Check if
displayHTML()anddbutilsare accessible (built-in to Databricks notebooks)
Steps
- Create a new notebook by clicking "Create" → "Notebook" and select Python or SQL as the default language
- Import required libraries at the top:
from pyspark.sql.functions import *andimport pandas as pd - Add widgets using
dbutils.widgets.text(),.dropdown(), or.multiselect()to capture user input - Create a DataFrame from a data source using
spark.read.format("parquet").load()orspark.sql("SELECT * FROM table") - Filter or transform data based on widget values retrieved with
dbutils.widgets.get("widget_name") - Use
display()function to render DataFrames with automatic visualizations (tables, charts, maps) - Create custom visualizations by converting to Pandas and using
displayHTML()with matplotlib or plotly - Use
%sql,%python,%r, or%scalamagic commands to mix languages in a single notebook
Code
# Databricks Notebook Source
# COMMAND ----------
# Import libraries
from pyspark.sql.functions import col, count, sum, avg, year, month
from pyspark.sql.types import StructType, StructField, StringType, IntegerType
import pandas as pd
import matplotlib.pyplot as plt
# COMMAND ----------
# Create interactive widgets
dbutils.widgets.text("start_date", "2024-01-01", "Start Date")
dbutils.widgets.text("end_date", "2024-12-31", "End Date")
dbutils.widgets.dropdown("region", "US", ["US", "EU", "APAC", "ALL"])
dbutils.widgets.multiselect("product_types", ["Electronics"], ["Electronics", "Clothing", "Books", "Food"])
# COMMAND ----------
# Retrieve widget values
start_date = dbutils.widgets.get("start_date")
end_date = dbutils.widgets.get("end_date")
region = dbutils.widgets.get("region")
product_types = dbutils.widgets.get("product_types").split(",")
# COMMAND ----------
# Create or read a sample DataFrame
data = [
("2024-01-15", "US", "Electronics", 1500),
("2024-02-20", "EU", "Clothing", 800),
("2024-03-10", "APAC", "Electronics", 2200),
("2024-04-05", "US", "Books", 350),
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Databricks Skills
Other Claude Code skills in the same category — free to download.
Databricks Delta Lake
Build Delta Lake tables with ACID transactions, time travel, and optimization
Databricks ETL Pipeline
Build medallion architecture ETL pipelines (bronze/silver/gold)
Databricks Unity Catalog
Configure Unity Catalog for data governance, lineage, and access control
Databricks MLflow
Track experiments, register models, and deploy with MLflow
Databricks Auto Loader
Ingest data incrementally with Auto Loader and cloud storage
Databricks SQL Warehouse
Query and visualize data with Databricks SQL warehouses and dashboards
Databricks Workflows
Orchestrate multi-task jobs with Databricks Workflows
Want a Databricks skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.