Skip to main content

spaday-codemirror

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.

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.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.1.0
File Size Uploaded
spaday_codemirror-0.1.0.tar.gz 845.9 kB Details

Built distribution (wheel)

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

Total release size: 1.7 MB

Release files / spaday_codemirror-0.1.0.tar.gz

Download URL spaday_codemirror-0.1.0.tar.gz
Size 845.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b67c76d85e97742a5ed74816782d507a48469e9e729f6c98fdf3a431165c0e1f
BLAKE2b-256 checksum
How to use checksums
397bc43d859483f626dfa0dea099a3c063100c6079c6ac6c3b7b3d058cfafa2d
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.0-py3-none-any.whl

Download URL spaday_codemirror-0.1.0-py3-none-any.whl
Size 810.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
283e3f593cb550d4d2937b8c41c45a3ca7c3eaec29d0c54c9479c8a6a1b9bc98
BLAKE2b-256 checksum
How to use checksums
913265bf7165d8af431d842b2a4dd3d90154e8b5d3e9356ae02ee97a0795593a
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

0.1.1

2 release files

This release

0.1.0 This release

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