Skip to main content

A Streamlit custom component for a free drawing canvas using Fabric.js.

Project description

Streamlit - Drawable Canvas

Streamlit component which provides a sketching canvas using Fabric.js.

Features

  • Draw freely, lines, circles and boxes on the canvas, with options on stroke & fill
  • Rotate, skew, scale, move any object of the canvas on demand
  • Select a background color or image to draw on
  • Get image data and every drawn object properties back to Streamlit !
  • Choose to fetch back data in realtime or on demand with a button
  • Undo, Redo or Drop canvas

Installation

pip install streamlit-drawable-canvas

Example Usage

import streamlit as st
from streamlit_drawable_canvas import st_canvas

# Specify canvas parameters in application
stroke_width = st.sidebar.slider("Stroke width: ", 1, 25, 3)
stroke_color = st.sidebar.beta_color_picker("Stroke color hex: ")
bg_color = st.sidebar.beta_color_picker("Background color hex: ", "#eee")
bg_image = st.sidebar.file_uploader("Background image:", type=["png", "jpg"])
drawing_mode = st.sidebar.selectbox(
    "Drawing tool:", ("freedraw", "line", "rect", "circle", "transform")
)
realtime_update = st.sidebar.checkbox("Update in realtime", True)

# Create a canvas component
canvas_result = st_canvas(
    fill_color="rgba(255, 165, 0, 0.3)",  # Fixed fill color with some opacity
    stroke_width=stroke_width,
    stroke_color=stroke_color,
    background_color="" if bg_image else bg_color,
    background_image=Image.open(bg_image) if bg_image else None,
    update_streamlit=realtime_update,
    height=150,
    drawing_mode=drawing_mode,
    key="canvas",
)

# Do something interesting with the image data and paths
if canvas_result.image_data is not None:
    st.image(canvas_result.image_data)
if canvas_result.json_data is not None:
    st.dataframe(pd.json_normalize(canvas_result.json_data["objects"]))

Development

Install

  • JS side
cd frontend
npm install
  • Python side
conda create -n streamlit-drawable-canvas python=3.7
conda activate streamlit-drawable-canvas
pip install -e .

Run

Both webpack dev server and Streamlit should run at the same time.

  • JS side
cd frontend
npm run start
  • Python side
streamlit run app.py

Cypress integration tests

  • Install Cypress: cd e2e; npm i or npx install cypress (with --force if cache problem)
  • Start Streamlit frontend server: cd streamlit_drawable_canvas/frontend; npm run start
  • Start Streamlit test script: streamlit run e2e/app_to_test.py
  • Start Cypress app: cd e2e; npm run cypress:open

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit-drawable-canvas-0.5.1.tar.gz (3.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_drawable_canvas-0.5.1-py3-none-any.whl (1.3 MB view details)

Uploaded Python 3

File details

Details for the file streamlit-drawable-canvas-0.5.1.tar.gz.

File metadata

  • Download URL: streamlit-drawable-canvas-0.5.1.tar.gz
  • Upload date:
  • Size: 3.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for streamlit-drawable-canvas-0.5.1.tar.gz
Algorithm Hash digest
SHA256 e3a5af7789341ecca96e898c79271d2fdcf086744b5a16f215c001175b865b5c
MD5 892e5c7603f31f12ac68d0a1681b8244
BLAKE2b-256 dc8e7b0bd39d8c74b4b7628be65fefaf550192de03a9b98adbfb0a83687566d2

See more details on using hashes here.

File details

Details for the file streamlit_drawable_canvas-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: streamlit_drawable_canvas-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for streamlit_drawable_canvas-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fbc83d2ed48260098d15f6b6cfb58c2fe541dfb3550a7a02cf7ef03c4ee96299
MD5 e066cb0f0a389a14835635ae2ae6bca1
BLAKE2b-256 0326e9921701fdc90ef741672dcebee3263d5b9bed80d029a834994f056e0c9c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page