Jupytext

1.19.1 · active · verified Fri Apr 10

Jupytext is a Jupyter plugin that allows users to save Jupyter notebooks as various plain text formats, including Markdown documents, Julia, Python, or R scripts. This enables easier version control with tools like Git and allows editing notebooks in standard IDEs. It supports two-way synchronization between the `.ipynb` file and its paired text representation. The current version is 1.19.1, and the library maintains an active release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic `.ipynb` file, then use the `jupytext` command-line interface to pair it with a Python script (`.py:percent` format) and synchronize the two. The pairing and synchronization ensure that changes in one file are reflected in the other.

# Create a sample Jupyter notebook file
with open('my_notebook.ipynb', 'w') as f:
    f.write('{\"cells\": [{\"cell_type\": \"code\", \"execution_count\": null, \"metadata\": {}, \"outputs\": [], \"source\": [\"print(\\\"Hello from Jupytext!\\\")\"]}, {\"cell_type\": \"markdown\", \"metadata\": {}, \"source\": [\"# A Markdown Cell\"]}], \"metadata\": {\"kernelspec\": {\"display_name\": \"Python 3\", \"language\": \"python\", \"name\": \"python3\"}}, \"nbformat\": 4, \"nbformat_minor\": 4}')

# Pair the .ipynb notebook with a Python script in percent format
# and then synchronize them
import subprocess

print("Pairing notebook.ipynb with notebook.py:percent...")
subprocess.run(['jupytext', '--set-formats', 'ipynb,py:percent', 'my_notebook.ipynb'], check=True)

print("Synchronizing files...")
subprocess.run(['jupytext', '--sync', 'my_notebook.ipynb'], check=True)

print("Generated files:")
subprocess.run(['ls', 'my_notebook.*'])

# Optionally, read the created .py file content
with open('my_notebook.py', 'r') as f:
    print('\n--- my_notebook.py content ---')
    print(f.read())

# Clean up (optional)
# import os
# os.remove('my_notebook.ipynb')
# os.remove('my_notebook.py')

view raw JSON →