Import and export Databricks notebooks
Databricks can import and export notebooks in multiple formats, convert script files into notebooks, and export entire folders as a ZIP archive.
Supported notebook formats
Databricks can import and export notebooks in the following formats:
- Source file: A file containing only source code statements with the extension
.scala,.py,.sql, or.r. - HTML: A Databricks notebook with the extension
.html. - Databricks
.dbcarchive. - IPython notebook: A Jupyter notebook with the extension
.ipynb. - RMarkdown: An R Markdown document with the extension
.Rmd.
Import a notebook
You can import an external notebook from a URL or a file. You can also import a ZIP archive of notebooks exported in bulk from a Databricks workspace.
- Click
Workspace in the sidebar. Do one of the following:
- Right-click on a folder and select Import.
- To import a notebook at the top level of the current workspace folder, click the kebab menu at the upper right and select Import.
- Specify the URL or browse to a file containing a supported external format or a ZIP archive of notebooks exported from a Databricks workspace.
- Click Import.
- If you choose a single notebook, it is exported in the current folder.
- If you choose a DBC or ZIP archive, its folder structure is recreated in the current folder and each notebook is imported.
Convert a file to a notebook
An asset in the workspace is identified as a notebook if it has an .ipynb extension, or contains the string Databricks notebook source in a comment in the first line and has one of the following extensions: .py, .r, .scala, .sql. Any write or rename of a notebook or file that changes these conditions, also changes the type of the notebook or file. For example, if you have a file myfile.txt that has # Databricks notebook source as the first line, renaming that file to myfile.py converts it into a notebook.
Converting between file and notebook types can have unexpected results, as notebooks have a specific IPYNB or source format. See Notebook formats.
To convert Python, SQL, Scala, and R scripts to single-cell notebooks, add a comment to the first cell of the file:
- Python
- SQL
- Scala
- R
# Databricks notebook source
-- Databricks notebook source
// Databricks notebook source
# Databricks notebook source
To define cells in a script, use the special comment shown below. When you import the script to Databricks, cells are created as marked by the COMMAND lines shown.
- Python
- SQL
- Scala
- R
# COMMAND ----------
-- COMMAND ----------
// COMMAND ----------
# COMMAND ----------
Export notebooks
When you export a notebook as HTML, IPython notebook (.ipynb), or archive (DBC), and you have not cleared the command outputs, the outputs are included in the export.
To export a notebook:
- With the notebook open, click File in the notebook toolbar, then click Export....
- Select the export file type from the dropdown menu.
- If you selected IPython notebook, HTML, or DBC archive as the export file type, you can choose to include cell outputs or not by selecting or clearing Include outputs.
- Click Export.
To export all notebooks in a workspace folder as a ZIP archive:
-
Click
Workspace in the sidebar.
-
Right-click the folder and select Download as.
-
Select the download format:
- DBC Archive (notebooks only): Export a Databricks archive, a binary format that includes metadata and notebook command outputs.
- Zip - Source (notebook + files only): Export a ZIP archive of notebook source files, which can be imported into a Databricks workspace, used in a CI/CD pipeline, or viewed as source files in each notebook's default language. Notebook command outputs are not included.
- Zip - HTML (notebooks only): Export a ZIP archive of the notebooks in HTML format. Each notebook's HTML file can be imported into a Databricks workspace or viewed as HTML. Notebook command outputs are included.