Skip to main content

Streamlit Drawable Konva

Streamlit custom component (Components v2) that provides a sketching canvas using Konva.js / react-konva.

The public Python API is intentionally compatible with streamlit-drawable-canvas-fix (st_canvas → CanvasResult(image_data, json_data)). Object JSON uses Konva-oriented fields (x/y/points/…), not Fabric.js schemas.

Features

  • Freehand, line, rect, circle, point, polygon drawing
  • Transform mode (move / scale / rotate); double-click to delete
  • Background color or image
  • Realtime or on-demand updates to Streamlit
  • Undo / redo / clear toolbar
  • Returns RGBA image_data and scene json_data

Why Konva.js (vs Fabric.js)?

Konva is not universally “better” than Fabric — both are mature 2D canvas libraries. For this Streamlit component, Konva is the better fit.

Concern Konva Fabric
React integration First-class via react-konva (declarative scene tree) Imperative fabric.Canvas; React wrappers are thinner / more brittle
Mental model Stage → Layer → Shape (clear export boundaries) Single canvas object model; background vs drawing export needs extra care
Bundle / focus Leaner, scene-graph oriented Heavier; strong textile/image-editing heritage
Transforms Built-in Transformer for select / scale / rotate Excellent selection UX; historically the gold standard for editors
Ecosystem Strong for interactive UIs and annotation tools Huge installed base; many Fabric JSON examples in the wild

Why we chose Konva here

  1. React + Streamlit Components v2 — the official CCv2 React template maps cleanly onto react-konva. Drawing state stays in React; updates go out via setStateValue without fighting an imperative canvas singleton.
  2. Layered export — background images can live on a non-exported layer while strokes/shapes export cleanly to image_data, matching the upstream “background is not round-tripped” behavior with less glue code.
  3. Maintainability — freerdraw / shapes / polygon / transform are ordinary React event handlers and Konva nodes, which is easier to evolve next to a modern Vite + TypeScript frontend than a Fabric-centric CRA-era stack.
  4. Performance for annotation UIs — Konva’s layer/stage model scales well for ROI / sketching workloads typical in Streamlit apps.

When Fabric may still win

  • You need byte-compatible Fabric JSON (existing pipelines, demos, or tutorials that assume left/top/path Fabric schemas).
  • You want Fabric’s mature free-transform / skew / complex text editing out of the box without reimplementing edge cases.

This package keeps a compatible Python API (st_canvas → CanvasResult) so apps can migrate from streamlit-drawable-canvas-fix, while accepting that json_data is Konva-oriented rather than a Fabric dump.

Install (uv)

uv sync

Build the frontend once (required before first Streamlit run):

cd streamlit_drawable_konva/frontend
npm install
npm run build
cd ../../

Editable install is handled by uv sync via the local pyproject.toml.

Demo

uv run streamlit run app.py

Development

CCv2 watches a production-style build into frontend/build (no separate webpack dev server URL). Use two terminals:

Terminal A — frontend watch build

cd streamlit_drawable_konva/frontend
npm install
npm run dev

Terminal B — Streamlit

uv sync
uv run streamlit run app.py

Refresh the browser after frontend rebuilds.

API

from streamlit_drawable_konva import st_canvas

result = st_canvas(
    fill_color="rgba(255, 165, 0, 0.3)",
    stroke_width=3,
    stroke_color="#000000",
    background_color="#eee",
    background_image=None,
    update_streamlit=True,
    height=400,
    width=600,
    drawing_mode="freedraw",  # freedraw|line|rect|circle|point|polygon|transform
    initial_drawing=None,
    display_toolbar=True,
    point_display_radius=3,
    key="canvas",
)

# result.image_data -> np.ndarray | None
# result.json_data  -> dict | None

Polygon controls

  • Left-click: add point
  • Right-click: close polygon
  • Double-click: remove latest point

Transform controls

  • Click object to select
  • Drag / resize / rotate with the transformer
  • Double-click selected object to delete

Packaging notes

  • Registered as streamlit-drawable-konva.st_canvas via the in-package streamlit_drawable_konva/pyproject.toml manifest (asset_dir = frontend/build).
  • Requires Streamlit >= 1.51 (Components v2).
  • Frontend toolchain targets Node >= 18 (Vite 5).

Step-by-step instructions: PUBLISHING.md.

Order: push to GitHub → publish to PyPI → deploy app.py on Community Cloud → submit gallery/streamlit-drawable-konva.json as a PR to streamlit/gallery.

License

MIT

Metadata

Release files for streamlit-drawable-konva 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 streamlit-drawable-konva 0.1.0
File Size Uploaded
streamlit_drawable_konva-0.1.0.tar.gz 164.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for streamlit-drawable-konva 0.1.0
File Interpreter ABI Platform
streamlit_drawable_konva-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 328.6 kB

Release files / streamlit_drawable_konva-0.1.0.tar.gz

Download URL streamlit_drawable_konva-0.1.0.tar.gz
Size 164.9 kB
Tags Source
SHA-256 checksum
How to use checksums
eb145afe1613a2578e67cb60b4539eef0d67480723a193d167e384d7e4dea412
BLAKE2b-256 checksum
How to use checksums
59456d75d2445c68e4608b7b91597f452870d5cb247c82b7a8d6f08235713440
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.4

Release files / streamlit_drawable_konva-0.1.0-py3-none-any.whl

Download URL streamlit_drawable_konva-0.1.0-py3-none-any.whl
Size 163.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8574c341e6e38a723515481c087b0820e0f1d48fea85ebc1575c4b5846fbcd6b
BLAKE2b-256 checksum
How to use checksums
5d0c73414a27cdc08aea3b5f6cce4cddc6caf1827f7a5114291803db60b91525
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.4

Release history Release notifications | RSS feed

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

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