Sub topic 1
What a Databricks Notebook Is
Prerequisites
You should know basic Python or SQL, understand variables and functions, and recognize the Git terms commit, branch, and pull. You should also know how to open a Databricks workspace and attach a notebook to compute.
Notebook and Compute
A Databricks notebook is a document containing runnable cells. Cells can hold code, Markdown, or SQL. The notebook stores the code and results, while compute provides the machines that execute the code.
Attach a notebook to a compute resource, such as a cluster or SQL warehouse, before running code. The selected compute determines available libraries, permissions, processing power, and cost. Notebook variables normally remain available during the current attached session.
Cells, Languages, and Magic Commands
Each cell runs independently, but cells in the same session can share state. A notebook usually has a default language, such as Python or SQL. Magic commands switch the language or invoke notebook features.
# Python cell
df = spark.range(5)
display(df)A SQL cell can begin with %sql:
%sql
SELECT current_date() AS run_dateOther useful magics include %md for Markdown and %run for running another notebook inline.
Helper Utilities with `dbutils`
dbutils is a collection of Databricks helper utilities. For example, dbutils.fs works with files in supported storage, and dbutils.notebook controls notebook calls and return values. Availability depends on the Databricks environment and permissions.
Parameterizing a Notebook with Widgets
A widget is a notebook input control. It lets a user or job provide a parameter without editing code.
dbutils.widgets.text("run_date", "2026-01-01", "Run date")
run_date = dbutils.widgets.get("run_date")
print(f"Processing {run_date}")The default value is used when no other value is supplied. Validate important inputs before using them in queries or paths.
Reusing and Chaining Notebooks
%run executes another notebook inline. Its variables and functions become available in the current notebook, so it is useful for shared setup code.
dbutils.notebook.run starts a separate notebook run, passes widget arguments, and returns a string from dbutils.notebook.exit.
result = dbutils.notebook.run(
"./child_notebook",
60,
{"run_date": "2026-01-02"}
)
print(result)The child notebook can return a status:
run_date = dbutils.widgets.get("run_date")
dbutils.notebook.exit(f"done {run_date}")Exercise
When should you use %run instead of dbutils.notebook.run?
Show answer
Use %run when inline access to shared variables or functions is needed. Use dbutils.notebook.run when the child should run separately with parameters and a returned result.