Files
tao.chenandClaude cd1aba6698 dev: initial implementation of notebook-snapshot extension
Manual-commit notebook snapshot extension (JupyterLab 4):

Backend
- Storage adapter layer (Local + S3 via boto3, pip extra)
  - Dumb put/get/list/delete contract; key validation
  - S3Storage: lazy boto3 import, S3ConnectionError with friendly
    messages + connectivity check at startup
- SnapshotStore
  - Strip code outputs/execution_count, MD5 on cleaned JSON
  - Gzipped envelope (id/timestamp/name/description/hash/size/notebook)
  - Append-only manifest.json (rebuildable from version files)
  - SnapshotUnchangedError when new hash matches most recent version
- REST API (commit / list / content) under /snapshot/ namespace
- traitlets config (storage_type, local_root, s3_* + S3_* env fallback)

Frontend
- snapshot:commit command (toolbar button + command palette)
- Dialog for name + description
- SnapshotPanel (left sidebar timeline) + DiffWidget (main area)
- Cell diff: id-match first, LCS fallback; jsdiff for line diff
- Restore via model.fromJSON() + context.save() (no refresh)
- All AGENTS.md hard conventions enforced

Tests: 33 backend pytest passing (storage + store + routes)

Docs: AGENTS.md, design.md, README.md synced with implementation.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-21 17:33:55 +08:00

65 lines
2.1 KiB
Python

try:
from ._version import __version__
except ImportError:
# Fallback when using the package in dev mode without installing
# in editable mode with pip. It is highly recommended to install
# the package from a stable release or in editable mode: https://pip.pypa.io/en/stable/topics/local-project-installs/#editable-installs
import warnings
warnings.warn("Importing 'snapshot' outside a proper installation.")
__version__ = "dev"
from .config import SnapshotConfig
from .routes import setup_route_handlers
from .storage import create_storage
from .store import SnapshotStore
def _jupyter_labextension_paths():
return [{
"src": "labextension",
"dest": "snapshot"
}]
def _jupyter_server_extension_points():
return [{
"module": "snapshot"
}]
def _load_jupyter_server_extension(server_app):
"""Registers the API handler to receive HTTP requests from the frontend extension.
Parameters
----------
server_app: jupyterlab.labapp.LabApp
JupyterLab application instance
"""
config = SnapshotConfig(config=server_app.config)
storage = create_storage(
storage_type=config.storage_type,
root_dir=server_app.root_dir,
local_root=config.local_root,
endpoint=config.s3_endpoint,
access_key=config.s3_access_key,
secret_key=config.s3_secret_key,
bucket=config.s3_bucket,
secure=config.s3_secure,
prefix=config.s3_prefix,
)
server_app.web_app.settings["snapshot_config"] = config
server_app.web_app.settings["snapshot_storage"] = storage
server_app.web_app.settings["snapshot_store"] = SnapshotStore(storage)
if config.storage_type == "s3":
server_app.log.info(
f"Snapshot S3 storage ready: endpoint={config.s3_endpoint}, "
f"bucket={config.s3_bucket}, secure={config.s3_secure}"
)
else:
server_app.log.info(
f"Activated snapshot storage backend: {config.storage_type}"
)
setup_route_handlers(server_app.web_app)
name = "snapshot"
server_app.log.info(f"Registered {name} server extension")