Skip to main content
spaday-codemirror logo, a code editor inside a browser window

CodeMirror 6 code editor for spaday.

Build Status codecov License PyPI

Usage

spaday-codemirror provides one custom element, <spaday-codemirror>, and its typed spaday binding, CodeMirror. The package registers itself as the codemirror component package, so pages load its assets with packages=["codemirror"].

from spaday.backends.starlette import serve
from spaday_codemirror import CodeMirror


def page():
    return CodeMirror(doc="print('hello')\n", language="python", theme="dark", tab_size=4)


app = serve(page, packages=["codemirror"])

To send edits to Python, bind editor-change to an action, e.g. .on("editor-change", SendPatch("editor", "doc", event_value("doc"))), and route the spaday:patch intent to your model. example.py does this over a websocket.

Without Python, load cdn/index.js and css/index.css from spaday_codemirror/extension/ (or js/dist/) and use the tag directly:

<spaday-codemirror language="json" tab_size="2"></spaday-codemirror>

Properties

Attributes share the property names.

Property Type Default Description
doc string "" Editor contents
language "python" | "javascript" | "json" | "markdown" | "plain" "plain" Syntax mode
theme "light" | "dark" "light" Color theme (dark uses One Dark)
read_only boolean false Disallow user edits
line_numbers boolean true Show the line-number gutter
tab_size number 4 Tab width and indent unit, in spaces
selection {anchor: number, head?: number} | null null Main selection; property only. head defaults to anchor; null leaves it as-is
remote_cursors {peer, anchor, head?, label?, color?}[] [] Remote selections; positions are tracked as the document changes. Labels and colors are optional

Setting a property updates the existing EditorView in place, so focus and scroll position are kept. Property changes never emit events.

Theming

Editor chrome follows spaday's shell palette. Component tokens can be set on the editor or any ancestor; spaday_codemirror.TOKENS exposes the same names for Python css() calls.

Token Controls Shell fallback
--spa-codemirror-surface Editor background --spa-surface
--spa-codemirror-text Editor and panel text --spa-muted
--spa-codemirror-gutter-surface Gutter and panel background --spa-surface-2
--spa-codemirror-gutter-text Line numbers --spa-muted
--spa-codemirror-border Editor, gutter, panel borders --spa-border
--spa-codemirror-focus Focused editor border --spa-accent
--spa-codemirror-cursor Caret and drop cursor --spa-accent
--spa-codemirror-selection Selection background Theme default
--spa-codemirror-active-line Active-line background Theme default
--spa-codemirror-active-line-gutter Active line-number background Theme default

Syntax colors remain owned by CodeMirror's light and One Dark themes. For example, CodeMirror(...).css(spa_codemirror_surface="#111", spa_codemirror_text="#ddd") changes one editor without changing the rest of the application.

Events

Both events bubble and are composed.

Event detail Fired when
editor-change {doc, changes: [{from, to, insert}], selection: {anchor, head}} The user edits the document
editor-selection {selection: {anchor, head}} The user moves the selection without editing

Cursor awareness

connectCursorAwareness(editor, client, modelId) publishes local selections through transports awareness, renders remote selections, maps their positions across document changes, and removes them when peers disconnect. Its optional local and remote callbacks attach and present application metadata. The collaboration example uses those callbacks for names and colors; the shared cursor state only requires a peer ID and selection.

Browser examples

  • Hosted Pyodide example: the example page running in a Python Web Worker.
  • spaday_codemirror/example.py: two editors wired to a transports-hosted model, plus a gallery covering every language and representative settings. Python computes metrics and a normalized preview on each edit, and has Normalize and Reset actions.
  • spaday_codemirror/collaboration.py: one CRDT-backed document shared by Alice and Bob, with a read-only Viewer. Open two user links in separate tabs to see character-level edits and ephemeral cursor awareness flow through Spaday and transports. The example assigns names and colors in its browser adapter; production applications should derive identity from authenticated websocket sessions.
  • js/examples/: the Pyodide page and worker.

Run the examples locally

make develop
make build
python -m spaday_codemirror.example  # http://127.0.0.1:8031/
python -m spaday_codemirror.collaboration  # http://127.0.0.1:8032/

Build and test the Pyodide example:

make test-pyodide-example
cd dist/lite && python -m http.server

Metadata

Release files for spaday-codemirror 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spaday-codemirror 0.2.0
File Size Uploaded
spaday_codemirror-0.2.0.tar.gz 863.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for spaday-codemirror 0.2.0
File Interpreter ABI Platform
spaday_codemirror-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 1.7 MB

Release files / spaday_codemirror-0.2.0.tar.gz

Download URL spaday_codemirror-0.2.0.tar.gz
Size 863.1 kB
Tags Source
SHA-256 checksum
How to use checksums
752d182e02afceab465e611192c0456681839ea8fa4ded376201c00da708e4b4
BLAKE2b-256 checksum
How to use checksums
eef329b173f34f22a5f1b246c14335857747c2396fbadc706c11637f3ff12e90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / spaday_codemirror-0.2.0-py3-none-any.whl

Download URL spaday_codemirror-0.2.0-py3-none-any.whl
Size 823.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9c231940bb815c41dc536f4a4c0b921e55ad4e5c71ba61f63377171e16142fcd
BLAKE2b-256 checksum
How to use checksums
a3e60805b1882ab36ada07af23b786d0011bf49e983b0890b55e10c95198d170
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page