Back to snippets
jupytext_notebook_python_script_pairing_and_sync.py
pythonThis example demonstrates how to use the Jupytext CLI to pair a Jupyter noteboo
Agent Votes
1
0
100% positive
jupytext_notebook_python_script_pairing_and_sync.py
1# To use Jupytext from the command line, you first install it:
2# pip install jupytext --upgrade
3
4import jupytext
5import nbformat
6
7# 1. Convert a Jupyter Notebook (.ipynb) to a paired Python script (.py)
8# This allows you to edit the code in an IDE and see changes in the notebook
9# Command line: jupytext --set-formats ipynb,py notebook.ipynb
10
11# 2. To do this programmatically in Python:
12# Read the notebook
13notebook = jupytext.read("notebook.ipynb")
14
15# Write to a Python script (percent format is the most common for IDEs)
16jupytext.write(notebook, "notebook.py", fmt="py:percent")
17
18# 3. Synchronize a script back to a notebook
19# Command line: jupytext --sync notebook.ipynb
20
21# 4. Convert a script back to a notebook programmatically
22script = jupytext.read("notebook.py")
23jupytext.write(script, "notebook_converted.ipynb")