Convert py to ipynb

Turn a Python script into a Jupyter notebook in your browser. Free, and the file never leaves your computer.

.py → Jupyter notebook Local processing

Your script starts here

Open a .py file or paste the code. No account needed.

or drop it anywhere on this page, or

See the result first.

Your file is processed in this browser and is never uploaded. Privacy details
No uploadNo accountNo installOpen source

Where the cells come from

A .py file is one long run of code, and a notebook is a list of cells, so the only real question is where to cut. Many scripts already say: they came out of a notebook or an editor that marks cells with a comment. The converter looks for those marks first.

# %% markers

The convention VS Code, Spyder, PyCharm and Jupytext share. Each marker starts a cell; # %% [markdown] starts a Markdown cell made from the comment lines under it.

# %% [markdown]
# ## Load the data

# %%
df = pd.read_csv("x.csv")

# In[ ]: markers

What jupyter nbconvert --to script writes. Each marker is a code cell; the comment blocks between cells were Markdown cells and become Markdown again.

# ## Load the data

# In[2]:


df = pd.read_csv("x.csv")

Neither

A new cell wherever two blank lines sit at the top level, which in a PEP 8 script is between functions and classes. A module docstring becomes the opening Markdown.

import pandas as pd


def load(path):
    return pd.read_csv(path)

How to convert a Python script to a Jupyter notebook

  1. 1

    Open the script

    Choose a .py file, drop it on the page, or paste the code. Nothing is installed and nothing is uploaded.

  2. 2

    Check the cells

    The notebook is shown cell by cell as Jupyter would show it. Switch how cells are split if the automatic choice isn't what you want.

  3. 3

    Download .ipynb

    The notebook is written straight to disk. Or send it on to the viewer, or to the PDF or HTML converter, already loaded.

Four ways to turn a .py into a notebook

They all end with your code in cells. They differ on how the cuts are found, whether comments can become text, and what happens to the IPython lines a notebook export left behind.

MethodNeeds installingCells split atMarkdown cells%magics
This converterNothing# %%, # In[ ]: or blank linesFrom # %% [markdown] and nbconvert commentsRestored
JupytextPython + jupytext# %% and several other formatsFrom # %% [markdown]Restored
VS CodeVS Code + Python and Jupyter extensions# %%From # %% [markdown]As written
Paste into JupyterJupyterBy handBy handAs written

The command-line route, for when you have Python anyway:

pip install jupytext
jupytext --to notebook script.py

Jupytext reads the same # %% markers, so a script that converts well here converts the same way there, and a notebook from either goes back to the same script.

What the notebook is for

Run it in Colab

Colab opens notebooks, not scripts. Convert, then in Colab choose File → Upload notebook and pick the .ipynb.

Run it a piece at a time

A long script becomes cells you can run, change and rerun one at a time, keeping the variables from the cells before.

Hand in a notebook

Some courses and reviewers want an .ipynb. Comments marked as Markdown become proper text cells between the code.

Going the other way? Convert ipynb to py · View a notebook online · Convert a notebook to PDF

How it works, and why you can trust it

The converter is JavaScript running in this page. It reads the script as text, finds the cell boundaries, and writes standard nbformat 4 JSON with a Python 3 kernel; the preview uses the same renderer as the rest of this site. There is no upload endpoint, so the file cannot leave your machine.

The source is on GitHub under the MIT licence.

Questions

Is my script uploaded?
No. The file is read and converted by JavaScript on your own computer. There is no server to send it to.
Does it run my code?
No. The notebook has your code in cells and no outputs, exactly like a notebook nobody has run yet. Open it in Jupyter, Colab or VS Code and run the cells to get the outputs.
How do I choose where the cells start?
Put a # %% line where you want each cell to begin, and # %% [markdown] above comment lines that should be text. VS Code, Spyder and PyCharm already treat those lines as cells, so the script stays a normal script.
What happens to %matplotlib and get_ipython() lines?
They go back to the magics they were. Scripts exported from a notebook carry them either commented out (# %matplotlib inline) or rewritten by nbconvert (get_ipython().run_line_magic('matplotlib', 'inline')); both become %matplotlib inline again, and !pip lines likewise. Choose Leave as written to keep the script's lines untouched.
Can I convert a notebook back to a script?
Yes, with the ipynb to py converter on this site. It writes # %% markers, so a script from there comes back here with the same cells.
How do I open the .ipynb in Colab?
In Colab, File → Upload notebook, then choose the downloaded file. In Jupyter or JupyterLab, open it from the file browser; in VS Code, open it like any file.
Does it work with R or Julia scripts?
Not yet. The notebook is written for a Python 3 kernel, so only Python scripts are converted as they should be.
Is it free?
Yes. No account, no watermark, no file limit. The code is open source under the MIT licence.