Jupyter Y-document
jupyter-ydoc provides document structures for collaborative editing in Jupyter environments, leveraging the Ypy library. It enables real-time synchronization of Jupyter notebooks and other file types using Yjs CRDTs. The library is actively maintained by the Jupyter project, with frequent patch and minor releases.
Warnings
- breaking Starting with version 3.3.0, `jupyter-ydoc` requires Python 3.10 or newer. Installations on older Python versions will fail.
- gotcha Older versions of `jupyter-ydoc` (prior to 3.3.3) had issues handling multi-byte Unicode characters in files, potentially leading to data corruption or incorrect display.
- gotcha Several bugs in versions prior to 3.3.4 could lead to cell duplication, incorrect reloading, or inconsistent states during collaborative editing of notebooks.
- gotcha Version 3.4.0 introduced asynchronous `get` and `set` methods for documents. While existing synchronous methods remain, developers building new integrations or optimizing performance might need to adapt to the new async patterns.
Install
-
pip install jupyter-ydoc
Imports
- YDoc
from jupyter_ydoc import YDoc
- YFile
from jupyter_ydoc import YFile
- YNotebook
from jupyter_ydoc import YNotebook
Quickstart
import json
from jupyter_ydoc import YDoc, YNotebook
# 1. Initialize a YDoc (the core Yjs document)
ydoc = YDoc()
# 2. Create a YNotebook wrapper for the YDoc
# This initializes the YDoc with an empty notebook structure if it's new.
ynotebook = YNotebook(ydoc)
# 3. Set content from a standard Jupyter notebook dictionary
notebook_content = {
"cells": [
{"cell_type": "code", "source": "print('Hello from jupyter-ydoc!')", "metadata": {}},
{"cell_type": "markdown", "source": "## Getting Started", "metadata": {}}
],
"metadata": {
"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"},
"language_info": {"codemirror_mode": {"name": "ipython", "version": 3}}
},
"nbformat": 4,
"nbformat_minor": 5
}
ynotebook.set(notebook_content)
print("Notebook content set successfully.")
# 4. Retrieve content as a standard Jupyter notebook dictionary
retrieved_content = ynotebook.get()
print("\nRetrieved notebook content:")
print(json.dumps(retrieved_content, indent=2))
# 5. Manipulate the notebook content (e.g., add a new cell)
ynotebook.append_cell({"cell_type": "code", "source": "x = 10\ny = 20\nx + y", "metadata": {}})
print("\nAdded a new code cell.")
# 6. Get the updated content
updated_content = ynotebook.get()
print("\nUpdated notebook content after adding cell:")
print(json.dumps(updated_content, indent=2))