Sub topic 3
Cells, languages, and magic commands
This chapter assumes you can write basic Python or SQL and have already created a notebook and attached it to compute. It covers how a notebook is divided into cells, how each cell picks a language, and how magic commands change what a cell does.
Cells and the default language
A notebook is a list of cells. Each cell holds code or text, and you run it on its own. Cells run on the attached compute, and they share state. A variable defined in one Python cell is available in the next Python cell.
Every notebook has a default language, chosen when you create it: Python, SQL, Scala, or R. A cell without a magic command uses that default.
Magic commands
A magic command is a line that starts with % on the first line of a cell. It changes how Databricks treats that cell.
| Magic | Effect |
|---|---|
| `%python`, `%sql`, `%scala`, `%r` | Run this cell in that language |
| `%md` | Render the cell as Markdown text |
| `%sh` | Run shell commands on the driver node |
| `%run` | Run another notebook inline |
A Python notebook can mix languages like this:
df = spark.range(5)
df.createOrReplaceTempView("numbers")
display(df)The next cell is SQL. It reads the temporary view that the Python cell created:
%sql
SELECT count(*) AS n FROM numbersThe two cells share the Spark session, so the temporary view is visible to both. Plain variables are different, and the next section explains why.
What is shared between languages
Each language runs in its own interpreter. A Python variable such as df does not exist in a Scala or R cell. To pass data between languages, register a temporary view or write a table. Both are visible to every language in the notebook.
flowchart TDRunning another notebook inline
%run must be alone in its cell. It runs the other notebook in the current context, so its functions and variables become available here:
%run ./shared_setupThis differs from dbutils.notebook.run, which starts a separate run. A later chapter compares the two.
Common errors
- Magic not on the first line. A
%sqlline placed below other code is treated as a syntax error in Python. The magic must be the first line of the cell. - Expecting variables to cross languages. A
NameErrorin a Python cell that follows a Scala cell usually means the value never left Scala. Use a temporary view. - Mixing
%runwith other code. If%runshares a cell with other statements, it does not work as expected. Give it its own cell. - Forgetting the default language. A cell with no magic runs in the notebook's default language. Check the language label if you get unexpected syntax errors.
Exercise
A notebook's default language is Python. A cell begins with a comment line, and the next line is %sql. Followed by SELECT 1. Why does this cell fail, and how do you fix it?
Show answer
The magic command must be on the first line of the cell. Here a comment comes first, so %sql is parsed as Python and raises a syntax error. Move %sql to the first line, or put the SQL in its own cell.
Exercise
A Python cell sets threshold = 10. A later %scala cell tries to use threshold and fails. What is a way to share the value?
Show answer
Each language has its own interpreter, so the variable does not cross over. Store the value in a temporary view or table, for example a one-row DataFrame registered with createOrReplaceTempView, and read it from the Scala cell. You can also pass it through a widget, which a later chapter covers.