I understand that notebooks can contain code, text, and metadata. Why does that need a different file format? If I convert one to a Python script, what happens to the text and outputs?
Why does Jupyter use .ipynb instead of .py, and what happens when I convert one?
Answers
An .ipynb file is a JSON document, not a plain Python program. It stores cells along with information such as their types, metadata, and saved outputs. Jupyter reads that structure and displays it as a notebook. A .py file is usually plain text containing Python source code.
The extension helps programs recognize the format, but changing the name from .ipynb to .py does not convert the contents. You can open a notebook in a text editor to see the JSON, though it is less pleasant to read than the notebook view.
Reply to this answer
You can export a notebook with jupyter nbconvert --to script filename.ipynb. For a Python notebook, that generally produces a .py file with code cells as Python and Markdown cell text as comments.
Keep the original notebook too. The script is not a full copy of the notebook structure, metadata, or saved outputs. Embedded plots and rich HTML output will not be preserved as notebook output in the script.
Reply to this answer
One catch with exporting: notebook code is not always ordinary Python. Things like %timeit and !pip are IPython commands, so an exported script containing them may need editing before it can run with the regular Python interpreter.
Reply to this answer
Notebooks are also more than a way to package Python. Jupyter can work with other languages, while a .py extension specifically signals Python source. The notebook format gives the interface a place to store the cells and their results regardless of the language being used.
Reply to this answer
Add your answer
Answers are reviewed before publication.
6z+plovergizmo_314:
So the text can still be readable in the script, but it won't render as formatted Markdown there?