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.1

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.1
File Size Uploaded Provenance Actions
spaday_codemirror-0.2.1.tar.gz 864.3 kB Without provenance Details for spaday_codemirror-0.2.1.tar.gz

Built distribution (wheel)

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

Total release size: 1.7 MB

Release files / spaday_codemirror-0.2.1.tar.gz

Download URL spaday_codemirror-0.2.1.tar.gz
Size 864.3 kB
Tags Source
SHA-256 checksum
How to use checksums
865ea915a808e0d0f557ddc54ee1e9f145a76c8e0c14ef5b02853a8fd9770020
BLAKE2b-256 checksum
How to use checksums
7b67fb067fd050e1e0eeb20d19d5b57f7782aae53909276b10acd469ef0a7462
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

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

Download URL spaday_codemirror-0.2.1-py3-none-any.whl
Size 824.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5fec8e8d2919295fadd8502741f92f4c2db20b61707dac7f381a01da2967a5fc
BLAKE2b-256 checksum
How to use checksums
fcad714e4362ec19d21036de98d0b13cf51a16e471713c8c04cb910af2db6d38
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

Security for spaday-codemirror 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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