### Install Automerge with WebSocket Support Source: https://github.com/automerge/automerge-py/blob/main/examples/README.md Install the automerge library with the necessary dependencies for WebSocket support. Use the editable install option if developing the library. ```bash pip install automerge[websocket] ``` ```bash pip install -e .[websocket] ``` -------------------------------- ### Setting up WebSocket Server for Sync Source: https://github.com/automerge/automerge-py/blob/main/README.md Example of setting up a WebSocket server using automerge.repo for real-time document synchronization. ```python import asyncio from automerge.repo import Repo, InMemoryStorage from automerge.transports import WebSocketServer, WebSocketClientTransport from automerge import ROOT, ScalarType # Server side async def run_server(): storage = InMemoryStorage() repo = await Repo.load(storage) async with repo: async with WebSocketServer(repo, "localhost", 8080): print("Server running on ws://localhost:8080") await asyncio.sleep(3600) # Keep running ``` -------------------------------- ### Install Automerge with WebSocket Support Source: https://github.com/automerge/automerge-py/blob/main/README.md Install the automerge package with optional WebSocket support for synchronization. ```bash pip install automerge[websocket] ``` -------------------------------- ### Setting up WebSocket Client for Sync Source: https://github.com/automerge/automerge-py/blob/main/README.md Example of setting up a WebSocket client using automerge.repo to connect to a server and synchronize documents. ```python import asyncio from automerge.repo import Repo, InMemoryStorage from automerge.transports import WebSocketServer, WebSocketClientTransport from automerge import ROOT, ScalarType # Client side async def run_client(): storage = InMemoryStorage() repo = await Repo.load(storage) async with repo: # Connect to server transport = await WebSocketClientTransport.connect("ws://localhost:8080") await repo.connect(transport) # Create and modify documents - changes sync automatically! handle = await repo.create() with handle.change() as doc: doc["message"] = "Hello, Automerge!" # Read document contents using direct access doc = handle.doc() print(f"Message: {doc['message']}") ``` -------------------------------- ### Install Automerge with S3 Storage Support Source: https://github.com/automerge/automerge-py/blob/main/README.md Install the automerge package with optional S3 storage support for persisting documents. ```bash pip install automerge[s3] ``` -------------------------------- ### Build Automerge-py Bindings Source: https://github.com/automerge/automerge-py/blob/main/README.md Steps to set up a virtual environment, install maturin, and build the automerge-py bindings for development. ```bash # Create venv in "env" folder (required by maturin) python3 -m venv env # Activate venv source ./env/bin/activate # Install maturin pip install maturin # Build the bindings maturin develop ``` -------------------------------- ### Install Automerge Test Dependencies Source: https://github.com/automerge/automerge-py/blob/main/README.md Install the necessary packages for testing automerge-py, including pytest and moto for S3 mocking. ```bash pip install pytest pytest-asyncio "moto[server]" ``` -------------------------------- ### Run Automerge WebSocket Client Source: https://github.com/automerge/automerge-py/blob/main/examples/README.md Connect a WebSocket client to the running Automerge server. Ensure the server is started before running the client. The client will sync documents and display its URL. ```bash # First, start the server in another terminal: python examples/websocket_server.py # Then run the client: python examples/websocket_client.py ``` -------------------------------- ### Automerge WebSocket Server Implementation Source: https://github.com/automerge/automerge-py/blob/main/examples/README.md Python code for setting up an Automerge WebSocket server. It initializes a repository with in-memory storage and starts the WebSocket server on localhost:8080. ```python from automerge.repo import Repo, InMemoryStorage from automerge.transports import WebSocketServer # Create a repository storage = InMemoryStorage() repo = await Repo.load(storage) # Start WebSocket server async with repo: async with WebSocketServer(repo, "localhost", 8080): # Server is now running await asyncio.sleep(3600) ``` -------------------------------- ### Read Automerge Document Source: https://github.com/automerge/automerge-py/blob/main/examples/README.md Example of finding an Automerge document by its URL and accessing its content. Use `handle.doc()` for read-only access. ```python # Find a document by URL handle = await repo.find(url) if handle: doc = handle.doc() title = doc["title"] print(f"Title: {title}") ``` -------------------------------- ### Run Automerge WebSocket Server Source: https://github.com/automerge/automerge-py/blob/main/examples/README.md Execute the WebSocket server script to share Automerge documents. The server will start on ws://localhost:8080, create an initial document, and accept client connections. ```bash python examples/websocket_server.py ``` -------------------------------- ### Basic Document Creation and Merging (Core API) Source: https://github.com/automerge/automerge-py/blob/main/README.md Demonstrates creating a document, performing transactions, forking, and merging using the low-level automerge.core API. ```python from automerge.core import Document, ROOT, ObjType, ScalarType doc = Document() with doc.transaction() as tx: list = tx.put_object(ROOT, "colours", ObjType.List) tx.insert(list, 0, ScalarType.Str, "blue") tx.insert(list, 1, ScalarType.Str, "red") doc2 = doc.fork() with doc2.transaction() as tx: tx.insert(list, 0, ScalarType.Str, "green") with doc.transaction() as tx: tx.delete(list, 0) doc.merge(doc2) # `doc` now contains {"colours": ["green", "red"]} ``` -------------------------------- ### Using S3 Storage with Automerge Repo Source: https://github.com/automerge/automerge-py/blob/main/README.md Demonstrates how to configure and use S3-backed storage for persisting Automerge documents with the Repo API. ```python from automerge.storages.s3 import S3Storage from automerge.repo import Repo # Create S3-backed storage storage = S3Storage( bucket="my-bucket", region="us-east-1", prefix="users/user-123" # Optional: isolate data by user/tenant ) # Use exactly like any other storage repo = await Repo.load(storage) async with repo: handle = await repo.create() with handle.change() as doc: doc["key"] = "value" ``` -------------------------------- ### Create and Initialize Automerge Document Source: https://github.com/automerge/automerge-py/blob/main/examples/README.md Code for creating a new Automerge document using a repository and initializing it with data. Use `handle.change()` for mutations. ```python # Create a new document handle = await repo.create() # Initialize with content with handle.change() as doc: doc["title"] = "Hello" doc["count"] = 0 ``` -------------------------------- ### Using the High-Level Document API Source: https://github.com/automerge/automerge-py/blob/main/README.md Shows how to use the Pythonic interface of automerge.Document with proxies for creating and modifying documents. Use ImmutableString for non-editable string values. ```python from automerge import Document, ImmutableString doc = Document() # Use Python dict/list syntax with doc.change() as d: d["title"] = "My Document" # Creates collaborative Text by default d["version"] = ImmutableString("1.0.0") # Use ImmutableString for non-editable strings d["tags"] = [] d["tags"][0] = "python" d["tags"][1] = "automerge" # Read values naturally print(doc["title"]) # TextReadProxy that acts like a string print(doc["version"]) # Regular Python string print(len(doc["tags"])) # 2 # Edit collaborative text with doc.change() as d: d["title"].insert(0, "✨ ") # Text objects support insert, delete, splice d["tags"][0].insert(6, " 3.12") print(str(doc["title"])) # "✨ My Document" ``` -------------------------------- ### Automerge WebSocket Client Implementation Source: https://github.com/automerge/automerge-py/blob/main/examples/README.md Python code for connecting an Automerge WebSocket client to a server. It creates a repository, establishes a connection to the server, and then connects the repository to the transport. ```python from automerge.repo import Repo, InMemoryStorage from automerge.transports import WebSocketClientTransport # Create a repository storage = InMemoryStorage() repo = await Repo.load(storage) # Connect to server async with repo: transport = await WebSocketClientTransport.connect("ws://localhost:8080") await repo.connect(transport) ``` -------------------------------- ### Modify Automerge Document Source: https://github.com/automerge/automerge-py/blob/main/examples/README.md Demonstrates how to modify an existing Automerge document. Changes should be made within a `handle.change()` context to ensure proper mutation. ```python # Make changes to a document with handle.change() as doc: current = doc["count"] doc["count"] = current + 1 ``` -------------------------------- ### Run Automerge-py Tests Source: https://github.com/automerge/automerge-py/blob/main/README.md Commands to execute all tests or specific test files using pytest. ```bash # Run all tests pytest tests/ -v # Run specific test file pytest tests/test_s3_storage_mock.py -v ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.