Jupyter Y-document

3.4.0 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a YDoc, wrap it with YNotebook, set content from a standard Jupyter notebook dictionary, retrieve the content, and programmatically add a new cell to the Y-document structure.

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))

view raw JSON →