• Stars
    star
    557
  • Rank 79,968 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Do you like Quick, Draw? Well what if you could train/predict doodles drawn inside Streamlit? Also draws lines, circles and boxes over background images for annotation.

Streamlit - Drawable Canvas

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

Streamlit App

PyPI PyPI - Downloads

Buy Me A Coffee

Features

  • Draw freely, lines, circles, boxes and polygons 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 Delete canvas contents
  • Save canvas data as JSON to reuse for another session

Installation

pip install streamlit-drawable-canvas

Example Usage

Copy this code snippet:

import pandas as pd
from PIL import Image
import streamlit as st
from streamlit_drawable_canvas import st_canvas

# Specify canvas parameters in application
drawing_mode = st.sidebar.selectbox(
    "Drawing tool:", ("point", "freedraw", "line", "rect", "circle", "transform")
)

stroke_width = st.sidebar.slider("Stroke width: ", 1, 25, 3)
if drawing_mode == 'point':
    point_display_radius = st.sidebar.slider("Point display radius: ", 1, 25, 3)
stroke_color = st.sidebar.color_picker("Stroke color hex: ")
bg_color = st.sidebar.color_picker("Background color hex: ", "#eee")
bg_image = st.sidebar.file_uploader("Background image:", type=["png", "jpg"])

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=bg_color,
    background_image=Image.open(bg_image) if bg_image else None,
    update_streamlit=realtime_update,
    height=150,
    drawing_mode=drawing_mode,
    point_display_radius=point_display_radius if drawing_mode == 'point' else 0,
    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:
    objects = pd.json_normalize(canvas_result.json_data["objects"]) # need to convert obj to str because PyArrow
    for col in objects.select_dtypes(include=['object']).columns:
        objects[col] = objects[col].astype("str")
    st.dataframe(objects)

You will find more detailed examples on the demo app.

API

st_canvas(
    fill_color: str
    stroke_width: int
    stroke_color: str
    background_color: str
    background_image: Image
    update_streamlit: bool
    height: int
    width: int
    drawing_mode: str
    initial_drawing: dict
    display_toolbar: bool
    point_display_radius: int
    key: str
)
  • fill_color : Color of fill for Rect in CSS color property. Defaults to "#eee".
  • stroke_width : Width of drawing brush in CSS color property. Defaults to 20.
  • stroke_color : Color of drawing brush in hex. Defaults to "black".
  • background_color : Color of canvas background in CSS color property. Defaults to "" which is transparent. Overriden by background_image. Changing background_color will reset the drawing.
  • background_image : Pillow Image to display behind canvas. Automatically resized to canvas dimensions. Being behind the canvas, it is not sent back to Streamlit on mouse event. Overrides background_color. Changes to this will reset canvas contents.
  • update_streamlit : Whenever True, send canvas data to Streamlit when object/selection is updated or mouse up.
  • height : Height of canvas in pixels. Defaults to 400.
  • width : Width of canvas in pixels. Defaults to 600.
  • drawing_mode : Enable free drawing when "freedraw", object manipulation when "transform", otherwise create new objects with "line", "rect", "circle" and "polygon". Defaults to "freedraw".
    • On "polygon" mode, double-clicking will remove the latest point and right-clicking will close the polygon.
  • initial_drawing : Initialize canvas with drawings from here. Should be the json_data output from other canvas. Beware: if you try to import a drawing from a bigger/smaller canvas, no rescaling is done in the canvas and the import could fail.
  • point_display_radius : To make points visible on the canvas, they are drawn as circles. This parameter modifies the radius of the displayed circle.
  • display_toolbar : If False, don't display the undo/redo/delete toolbar.

Example:

import streamlit as st
from streamlit_drawable_canvas import st_canvas

canvas_result = st_canvas()
st_canvas(initial_drawing=canvas_result.json_data)
  • display_toolbar : Display the undo/redo/reset toolbar.
  • key : An optional string to use as the unique key for the widget. Assign a key so the component is not remount every time the script is rerun.

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

More Repositories

1

streamlit-echarts

A Streamlit component to render ECharts.
Python
507
star
2

streamlit-lottie

Streamlit component to render Lottie animations
Python
164
star
3

social-media-tutorials

Code dumps of Youtube/Twitter tutorials
Jupyter Notebook
160
star
4

pyspark-tutorial

Jupyter notebooks for pyspark tutorials given at University
Jupyter Notebook
99
star
5

streamlit-component-template-vue

Vue 2/3 template for Streamlit Components
Vue
81
star
6

streamlit-d3-demo

D3 in React in Streamlit tech demo
TypeScript
77
star
7

fastapi-vue-crud

Testing a project with a FastAPI backend and Vue.js frontend in the same project
Vue
76
star
8

streamlit-echarts-demo

Demo for Streamlit ECharts component
Python
76
star
9

streamlit-drawable-canvas-demo

Streamlit Share demo for Drawable Canvas
Python
47
star
10

streamlit-light-leaflet

Streamlit quick & dirty Leaflet component that sends back coordinates on map click
TypeScript
42
star
11

writing-with-gpt2

Reproducing "Writing with Transformer" demo, using aitextgen/FastAPI in backend, Quill/React in frontend
JavaScript
28
star
12

streamlit-cytoscapejs

Cytoscape.js wrapper for Streamlit
Python
28
star
13

streamlit-custom-slider

Streamlit custom slider - Code for the Streamlit Component Tutorial blog post
Python
27
star
14

streamlit-nginx-basicauth

Reverse-proxy NGinx doing Basic Authentication, passing Auth user through Websocket headers and Streamlit extracting auth login from websocket headers to customize page per user logging in.
Dockerfile
16
star
15

streamlit_scatterplot_selection

Companion to https://dev.to/andfanilo/streamlit-components-scatterplot-with-selection-using-plotly-js-3d7n
Python
15
star
16

quickdraw-minigame

A Quickdraw minigame prototype with Vue.js and Tensorflow.js/TFVis - https://andfanilo.github.io/quickdraw-minigame/
JavaScript
14
star
17

streamlit-google-authentication-tests

Exploring different ways for Google Authentication in Streamlit
Python
13
star
18

ddsp-streamlit-ui

Streamlit version for the DDSP timbre transfer demo.
Python
11
star
19

regression-streamlit-viz

Streamlit app for somewhat arbitrary regression
Python
11
star
20

streamlit-lottie-demo

Python
11
star
21

streamlit-plotly-component-tutorial

JavaScript
11
star
22

pyspark-streamlit-tutorial

Python
9
star
23

cookiecutter-kaggle

A cookiecutter Conda template to bootstrap your Kaggle projects (and any data science Python projects anyway)
Python
8
star
24

streamlit-midi-to-wav

Streamlit app to convert MIDI to WAV with FluidSynth.
Python
8
star
25

streamlit-css-button

Pimp your Streamlit button through CSS
Python
8
star
26

streamlit-named-entity-svelte

Streamlit Component in Svelte, imitates the named recognition demo from Prodigy.
TypeScript
7
star
27

streamlit-ama

Python
7
star
28

cloudera-quickstartvm-tutorial

Hadoop tutorial for university in Cloudera Quickstart VM
7
star
29

streamlit-weekly-roundup-api

Browse through all links from Streamlit Weekly Roundup posts
Python
6
star
30

spark-project-template

A reusable Maven Apache Spark template project in Scala
Scala
5
star
31

s4a_cats_grid

A Streamlit app for a grid of random cute cats !
Python
4
star
32

ieee-fraud-detection

Analysis for IEEE Fraud Detection kaggle competition
Jupyter Notebook
4
star
33

streamlit-sandbox

A whole bunch of one-shot code I create to solve issues on the Streamlit forum
Python
4
star
34

streamlit-chat-with-youtube-playlist

Very Quick "Chat with a Youtube Playlist" Streamlit App
Python
3
star
35

streamlit-echarts-events-demo

Python
3
star
36

hdp-tutorial

Hadoop tutorial using the Hortonworks Data Platform for university lecture.
Python
3
star
37

pyspark-interactive-lecture

A RISE Pyspark lecture
Jupyter Notebook
3
star
38

ml-in-pl-23

Python
2
star
39

wikipediaforecast

Code for "Web Traffic Time Series Forecasting" Kaggle competition
Jupyter Notebook
2
star
40

regression-ipywidgets-viz

Ipywidget notebooks for an internal WL talk about ML, optimization and regression
Jupyter Notebook
2
star
41

s4a-python-challenges

PoC Streamlit Education
Python
2
star
42

s4a-local-video

Display local video on Streamlit Share
Python
2
star
43

andfanilo

It's a secret to everybody
2
star
44

s4a-selenium

Test Selenium + Firefox on Streamlit Share
Python
2
star
45

superstore-dashboard-streamlit

Streamlit Dashboard over Superstore Data stored in Postgres Docker container. With SQLAlchemy + Plotly Express
Python
2
star
46

streamlit-pedalboard-demo

Streamlit Pedalboard demo
Python
1
star
47

s4a-webrtc-colornamer

Python
1
star
48

arjs-test

Test ar.js example on Github Pages
HTML
1
star
49

s4a-css-hacking

Streamlit CSS hacks
Python
1
star
50

personal-codepen

WIP: A Vue.js codepen clone
Vue
1
star
51

devbox-vagrant

My development box on Vagrant/VirtualBox
Shell
1
star
52

kaggle-titanic-spark

Analysis of the Kaggle Titanic dataset using Spark
Scala
1
star
53

curious_notebooks

Jupyter notebooks I wrote for testing the latest Python librairies. Expect data analytics, visualisation, and the occasional strange package.
Jupyter Notebook
1
star
54

test-fasthtml-dashboard

A scrappy test of FastHTML + TailwindCSS on a live dashboard usecase.
Python
1
star
55

house-prices-advanced-regression-techniques

Some of my work on House Prices: Advanced Regression Techniques Kaggle competition
HTML
1
star
56

lyon2-docker-demo

Python
1
star
57

lyon2

Python
1
star
58

lyon2-nosql-demo

1
star