Sub topic 8
How a notebook is stored as a source file
This chapter assumes you can read basic Python or SQL and know the Git basics of commit, diff, and merge. You should also have used a Databricks notebook in the workspace. Here you will learn what a notebook looks like as a text file, why Git can version it, and which mistakes turn a notebook into a plain file.
Why the file format matters
In the workspace, a notebook shows cells, results, and charts. Git stores text files, so Databricks has to turn the notebook into text when you commit it from a Git folder.
By default, it exports the notebook as a source file. This is an ordinary file with a language extension (.py, .sql, .scala, or .r). Cell boundaries and language switches are encoded as comments, so the file is still valid code in its language.
This has three practical effects:
- Diffs and pull requests show real code changes.
- Merge conflicts happen in readable text.
- You can edit a notebook in an IDE and see it as a notebook in the workspace after you pull.
Anatomy of a Python notebook file
This is a small Python notebook as Git sees it:
# Databricks notebook source
# MAGIC %md
# MAGIC # Daily load
# COMMAND ----------
df = spark.range(5)
display(df)
# COMMAND ----------
# MAGIC %sql
# MAGIC SELECT 1 AS oneThe file has three kinds of markers.
| Marker | Meaning |
|---|---|
| `# Databricks notebook source` | First line. Tells Databricks this file is a notebook. |
| `# COMMAND ----------` | Separator between cells. |
| `# MAGIC <text>` | A line of a non-default-language cell. The text after `# MAGIC ` is the cell content, including the magic command such as `%md` or `%sql`. |
The file contains three cells:
- A Markdown cell. Each line starts with
# MAGIC, and the first one holds%md. - A Python cell. It has no prefix because Python is the notebook's default language.
- A SQL cell, written as
%sqlon a# MAGICline followed by the query.
Cells in the notebook's default language are written as plain code. Cells in any other language are written with # MAGIC lines. If you change the notebook's default language, the file's cells change shape to match.
Other languages use their own comment syntax
The markers follow the comment style of the notebook's language. A SQL notebook looks like this:
-- Databricks notebook source
-- MAGIC %md
-- MAGIC # Orders summary
-- COMMAND ----------
SELECT order_status, COUNT(*) AS n
FROM main.sales.orders
GROUP BY order_status;Here the header is -- Databricks notebook source and the separator is -- COMMAND ----------. Scala notebooks use // for the same markers. R notebooks use #, as Python does.
The file extension decides the notebook's default language. A .sql file imported as a notebook has SQL as its default language. A .py file has Python.
What the file contains and what it leaves out
The source format keeps the code, the Markdown, and the order of cells. Anything you wrote in code is versioned, including dbutils.widgets calls, %run lines, and dbutils.notebook.run calls.
It does not keep cell results. Tables, charts, and printed output stay out of Git, which keeps diffs small and avoids committing data by accident. Revision history in the workspace is a separate feature and is not part of the file.
A cell title is kept as a comment line above the cell content. It looks like # DBTITLE 1,Load orders, where the number is a flag and the text after the comma is the title.
Databricks can also work with .ipynb (Jupyter) files in Git folders. That format is JSON and can hold outputs, so diffs are noisier. Your workspace settings determine how outputs are handled when you commit. This chapter uses the source format because it is the one that gives clean reviews.
The round trip
The conversion works in both directions. The file does not store anything the markers can't recover, so a notebook you commit and pull comes back with the same cells in the same order.
Editing source files outside Databricks
You can write or edit these files in an IDE, then push them. This is useful for large refactors and for code review tools. Three rules keep the file recognized as a notebook:
- Keep the header as the very first line. Do not put a blank line, a license comment, or a shebang above it.
- Write the separator exactly as
# COMMAND ----------(or the language's equivalent), on its own line. - Prefix every line of a magic cell with
# MAGIC, including blank lines inside the cell.
A blank line in a %md cell is written as # MAGIC with nothing after it. If you leave out the prefix on a line, that line becomes part of the previous cell's code instead of the magic cell.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| A `.py` file opens as a plain file, not a notebook. | The first line is not `# Databricks notebook source`. | Make the header the first line, then pull or re-sync. |
| A SQL cell runs as Python and fails with a syntax error. | The `%sql` line or the `# MAGIC` prefix is missing. | Put `# MAGIC %sql` first, then prefix each SQL line. |
| Two cells merge into one. | The separator was changed, for example a different number of dashes. | Restore `# COMMAND ----------` exactly. |
| Outputs are missing after a pull. | Source format does not store results. | Re-run the cells. |
| A helper module imports fine in one place but not another. | A file with the header is a notebook, not a module. | Remove the header from files meant to be imported. |
The last row is the most common surprise. A .py file in a Git folder is a notebook only if it starts with the header. A file without the header is a normal Python file that you can import. Add the header to a shared module and you have turned it into a notebook, which can no longer be imported. The next chapters on files and modules build on this.
Reviewing notebook changes
Because the file is text, a pull request shows added and removed lines. Reviewers can see a changed SQL string, a new widget, or a deleted cell. Some changes still look noisy:
- Moving a cell shows as a delete and an insert.
- Changing the default language rewrites many lines between plain and
MAGICform. - Large Markdown cells add many
# MAGIClines, so keep them short.
Keep each commit small and focused to reduce this noise.
Exercise
A teammate edits load_orders.py in an IDE, adds a license comment on line 1 and moves # Databricks notebook source to line 2, then pushes. After you pull in the Git folder, load_orders.py opens as a plain file and its %sql cell does not run. What is the cause, and how do you fix it?
Show answer
The header is no longer the first line, so Databricks does not recognize the file as a notebook and treats it as a plain Python file. Move # Databricks notebook source back to line 1 (the license comment can go after it), commit, push, and pull again.