Sub topic 1
Working in a Databricks notebook
A Databricks notebook is a document of ordered cells that runs code on a cluster. This chapter covers the parts of the notebook you use before writing any DataFrame code: attaching compute, running cells, mixing languages, and keeping state under control.
Prerequisites
This chapter assumes you have:
- Basic Python or SQL.
- Familiarity with tabular data: rows, columns and types.
- A Databricks workspace where you can create a notebook and attach it to compute.
How a notebook runs your code
Your browser shows the notebook, but the code does not run there. Each cell is sent to the compute resource the notebook is attached to. That compute can be a cluster or serverless compute. The attached compute holds a Python process, called the notebook's session, that keeps variables alive between cells.
Because state lives on the compute, two facts follow:
- A variable defined in one cell is available in any cell that runs after it.
- If the compute stops or you detach the notebook, the variables are lost. You must re-run the cells that create them.
Running cells
Run a cell with the Run button or with Shift+Enter. Cells run in the order you trigger them, not in the order they appear on the page. This is the most common source of confusion. If you edit a cell near the top and run only that cell, cells below it still hold results from the old code.
A reliable habit is to run the whole notebook from top to bottom before you trust it. Use Run all for this. After a restart or detach, run all again to rebuild state.
Languages and magic commands
Each notebook has a default language, such as Python. A magic command at the start of a cell changes the language for that cell only.
| Magic command | Effect |
|---|---|
| `%python` | Run the cell as Python |
| `%sql` | Run the cell as SQL |
| `%md` | Render the cell as Markdown text |
| `%sh` | Run shell commands on the driver node |
A magic command must be the first line of the cell. Python variables are not automatically visible in a %sql cell. Chapter content later in this tutorial shows how temporary views let the two languages share data.
Output and display
The last expression in a cell is shown below it. Python's print writes plain text. Databricks also provides display(), which renders a DataFrame as an interactive table that you can sort and chart. You will use both throughout the tutorial.
message = "Notebook state persists between cells"
print(message)Run that cell, then use message in a second cell. It works because the Python process kept the variable.
Common errors
- NameError after a restart. The variable existed before the compute restarted. Run the cells that define it again.
- Stale results. You edited an earlier cell but did not re-run the cells that depend on it. Use Run all.
- Cell does not start. The notebook is not attached to running compute. Check the compute selector at the top right.
- Magic command ignored. The
%line was not the first line of the cell.
Exercise
You change the definition of a variable in cell 2 and run only cell 2. Cell 5 uses that variable and still shows the old result. Why, and what is the fix?
Show answer
Cell 5 has not been re-run, so its output comes from the earlier run with the old value. Re-run cell 5, or use Run all to run every cell in order.