Skip to main content

ipydraw

Simple canvas for ipywidgets

Installation

You can install using pip:

# Using pip
pip install ipydraw

# Using poetry
poetry add ipydraw

If you are using Jupyter Notebook 5.2 or earlier, you may also need to enable the nbextension:

jupyter nbextension enable --py [--sys-prefix|--user|--system] ipydraw

Usage

Canvas

Canvases can be used to draw an image using a pointing device (like a mouse) on the frontend.

You can display the canvas in Jupyter frontends by creating a Canvas widget:

import ipydraw
canvas = ipydraw.Canvas()
canvas

You can access the image data (as a data url) by examining the data attribute:

print(canvas.data)
# data:image/png;base64,...

Arguments

# Default values shown below
ipydraw.Canvas(
    # The width of the strokes drawn on the Canvas
    line_width=10,

    # If true, enable drawing in multiple colors.
    # The user can pick the color before drawing each stroke.
    color=False,

    # The size (in pixels) of the canvas.
    size=(100, 100),
)

Limitations

  • It is not currently possible to set an initial image for the canvas.
  • The canvas only supports syncing from the frontend (browser) to the kernel (Python).
  • It is not possible to update any attributes (e.g., size or line_width) after the widget has been created.

Usage with Pillow

To load the canvas image into a Python Pillow Image, we need to parse the data URL (which is essentially a base64-encoded PNG image).

Make sure to install Pillow before continuing.

pip3 install Pillow
import base64, io
from Pillow import Image

data_url = canvas.data

# Strip the data: url prefix to get just the base64 encoded PNG data
data_encoded = data_url[len('data:image/png;base64,'):]

# Get the raw PNG data
data_bin = base64.b64decode(data_encoded)

# Read the image into a Pillow Image.
# We use a BytesIO since Image.open expects a file-like object.
image_from_canvas = Image.open(io.BytesIO(data_bin))

Point Picker

The point picker can be used to select points on an image.

from PIL import Image
import ipydraw

# Create a PointPicker with a given background image
bg = Image.open("some_image.jpeg")
pp = ipydraw.PointPicker.for_image(bg, n_points=2)
pp

To get the points after they've been selected:

points = pp.get_points()
# example: [[308, 400.734375], [427, 349.734375]]

Arguments

# Default values shown below
ipydraw.PointPicker(
    # The src of the image to use as the background. This is set automatically if you
    # use the PointPicker.for_image(...) API. Otherwise, this must be set manually.
    image_src="...",

    # The number of points that should be selected on the image. If set to None, there
    # is no limit on the number of points.
    n_points=False,
)

Development Installation

Create a dev environment:

conda create -n ipydraw-dev -c conda-forge nodejs yarn python jupyterlab
conda activate ipydraw-dev

Install the python. This will also build the TS package.

pip install -e ".[test, examples]"

When developing your extensions, you need to manually enable your extensions with the notebook / lab frontend. For lab, this is done by the command:

jupyter labextension develop --overwrite .
yarn run build

For classic notebook, you need to run:

jupyter nbextension install --sys-prefix --symlink --overwrite --py ipydraw
jupyter nbextension enable --sys-prefix --py ipydraw

Note that the --symlink flag doesn't work on Windows, so you will here have to run the install command every time that you rebuild your extension. For certain installations you might also need another flag instead of --sys-prefix, but we won't cover the meaning of those flags here.

How to see your changes

Typescript:

If you use JupyterLab to develop then you can watch the source directory and run JupyterLab at the same time in different terminals to watch for changes in the extension's source and automatically rebuild the widget.

# Watch the source directory in one terminal, automatically rebuilding when needed
yarn run watch
# Run JupyterLab in another terminal
jupyter lab

After a change wait for the build to finish and then refresh your browser and the changes should take effect.

Python:

If you make a change to the python code then you will need to restart the notebook kernel to have it take effect.

Publishing

Tags are automatically published by CI. The version must be updated manually in the Python and JavaScript packages. Assuming you're publishing version 1.2.3 (or 1.2.3-alpha.4):

  • Update ipydraw/_version.py
    • For pre-releases, set version_suffix to a4 (or empty for a non-pre-release).
  • Update package.json
    • For pre-releases, use the 1.2.3-alpha.4 syntax.

To push the tag:

VERSION="v1.2.3-alpha.4"
git commit -m "$VERSION"
git tag "$VERSION"
git push
git push --tags

Metadata

Release files for ipydraw 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ipydraw 0.2.2
File Size Uploaded Provenance Actions
ipydraw-0.2.2.tar.gz 37.1 kB Without provenance Details for ipydraw-0.2.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ipydraw 0.2.2
File Interpreter ABI Platform Actions
ipydraw-0.2.2-py2.py3-none-any.whl Python 2, Python 3 none any Details for ipydraw-0.2.2-py2.py3-none-any.whl

Total release size: 87.9 kB

Release files / ipydraw-0.2.2.tar.gz

Download URL ipydraw-0.2.2.tar.gz
Size 37.1 kB
Tags Source
SHA-256 checksum
How to use checksums
08fc62610666e7d96421cc107e588c2efc9119079ed1e87f8137afa9e2535398
BLAKE2b-256 checksum
How to use checksums
cbcdf6096838e64a6f2fda7d2af0f79f4a5beefb12d3f54a8776fa235cb4f663
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

Release files / ipydraw-0.2.2-py2.py3-none-any.whl

Download URL ipydraw-0.2.2-py2.py3-none-any.whl
Size 50.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
2a42ad218111a8d4710e2c3bb114ce9d2bf7996b19130f4290694c2a993dbed8
BLAKE2b-256 checksum
How to use checksums
c6b9575407eb5b7f5d882fdbf2b78ccfe16c1811eadb4b434ee03feded96a096
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

Security for ipydraw 0.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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