# Export to Python (/docs/projects/python-files.md)



Any notebook can be saved as a Python file in one click, and any `.py` file can be opened as a notebook.
Nothing is locked in: the exported file is a normal script that runs with open-source packages, and
[SQL cells](/docs/sql-and-data/sql-cells.md) are included.

## Export a notebook [#export-a-notebook]

Export works on one notebook at a time and exports **all of its cells**. There is no dialog.

<Shot id="py-export-menu" alt="The Export button in the notebook toolbar, open, with Export as Python (.py) and Download .ipynb" />

There are two ways to start:

* **In the notebook toolbar,** choose the **Export** button (the download icon), then **Export as Python (.py)**.
  The same menu has **Download .ipynb**, which downloads the notebook as it is right now, saved or not.
* **In the file tree,** open the **⋯** menu on a `.ipynb` file and choose **Export as Python**.

<Shot id="py-file-menu" alt="The actions menu on a notebook in the file tree: Rename, Export as Python, Delete" />

The file is saved **in the same folder, with the same name**, for example `churn.ipynb` becomes `churn.py`.
An existing file is never overwritten: the new one becomes `churn (1).py`. Rename it afterwards if you want
another name. Playground notebooks export the same way.

### The notice after export [#the-notice-after-export]

A notice appears at the bottom of the Studio, with an **Open** link to the new file.

<Shot id="py-export-notice" alt="The notice after export: 1 note, with the file name, an Open link and the note listed below" />

* A green **saved** badge means there is nothing to do.
* An amber **N notes** badge lists what needs attention before the file runs elsewhere:
  * **Connections:** for each data connection the notebook uses, the environment variable to set, such as
    `Connection "warehouse": set DB_WAREHOUSE_URL to a SQLAlchemy URL`.
  * **Cells that need ByteRun:** cells that use `byterun.*`, `connections[...]`, `load_result` or `%%sql` still need
    ByteRun to run.
  * **Row limits:** a SQL cell's row `limit` is kept in the file but not applied outside ByteRun.
  * **Playground notebooks:** the playground's tables are CSV files, so put them in a `data/` folder next
    to the script.
* A red **not saved** badge means nothing was written, with the reason. Only Python notebooks can be exported.

## What the file looks like [#what-the-file-looks-like]

The file uses the [jupytext](https://jupytext.readthedocs.io) **percent format**, which VS Code, PyCharm and
Spyder also understand. Each cell starts with `# %%`; markdown cells are `# %% [markdown]` with every line
commented. A short header records the kernel and the key `byterun_export: 1`.

```python
# ---
# jupyter:
#   byterun_export: 1
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# %% [markdown]
# # Orders

# %% byterun_sql=true
orders_df = _query(
    "warehouse",
    """SELECT * FROM orders WHERE total > 100""",
)
```

[Native SQL cells](/docs/sql-and-data/sql-cells.md) become a call to a small `_query` helper. Right after the header,
the file carries generated helper cells: the `_query` helper with a PEP 723 dependency block, one comment per
connection naming its `DB_<NAME>_URL` variable, and for playground notebooks a loader for the CSV files.
They run as ordinary Python, and ByteRun leaves them out when you open the file again.

To run the file elsewhere, set each `DB_<NAME>_URL` variable to a SQLAlchemy URL and run it with a tool that
reads PEP 723 blocks, such as `uv run`, or install the packages yourself.

### What is never exported [#what-is-never-exported]

* **Outputs.** Run the file to get fresh results.
* **Credentials and host names.** The file names a connection but never where it points or how to sign in.
  You supply that through the environment variable.
* **ByteRun-only metadata** on cells other than SQL cells.

## Open a `.py` file as a notebook [#open-a-py-file-as-a-notebook]

In the file tree, open the **⋯** menu on a `.py` file and choose **Open as notebook**. ByteRun creates a new
notebook next to it (`name.ipynb`, or `name (1).ipynb` if that exists). The `.py` file and any existing
notebook are left alone.

Code, markdown, raw and SQL cells come back, including native SQL cells and `%%sql` cells. If a native SQL cell
names a connection that is not in this project, the notice says so and you choose another in the cell.

Files that ByteRun did not write open too, as long as they use the percent format: files from jupytext,
VS Code or Spyder. Only files with `byterun_export: 1` in the header have ByteRun's own escaping reversed, so a
comment that happens to look like a cell marker is never changed in other people's files. A file in another
format (light, Rmd) opens as a single code cell, and the notice says so.

A file must be **4 MB or less**.

## Create a Python file [#create-a-python-file]

Use **+** at the top of the file tree and choose **Python** to create an empty `.py` file. See
[files](/docs/projects/files.md).
