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

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

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.
  • 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/

Build and test the Pyodide example:

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

Metadata

Release files for spaday-codemirror 0.1.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.1.1
File Size Uploaded
spaday_codemirror-0.1.1.tar.gz 848.9 kB Details

Built distribution (wheel)

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

Total release size: 1.7 MB

Release files / spaday_codemirror-0.1.1.tar.gz

Download URL spaday_codemirror-0.1.1.tar.gz
Size 848.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c500e413923d13c061702e622b824b07f0db527255fcd413d85869f55a500d15
BLAKE2b-256 checksum
How to use checksums
b91920393ff73c29c6e291c5f913a3823111ef3b3a93bee9735aa0bc00657b31
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.1.1-py3-none-any.whl

Download URL spaday_codemirror-0.1.1-py3-none-any.whl
Size 812.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e9883f8d8d9741d723f4de5cb5a7cc8adc7062b9fa152246c80a38de60e3789f
BLAKE2b-256 checksum
How to use checksums
6d3f38c470b5a607312802673e0bcdd7c5821dd2cf33af7a0d92e7d919b13426
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

0.2.0

2 release files

This release

0.1.1 This release

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