Simple canvas for ipywidgets
Project description
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
orline_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
toa4
(or empty for a non-pre-release).
- For pre-releases, set
- Update
package.json
- For pre-releases, use the
1.2.3-alpha.4
syntax.
- For pre-releases, use the
To push the tag:
VERSION="v1.2.3-alpha.4"
git commit -m "$VERSION"
git tag "$VERSION"
git push
git push --tags
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ipydraw-0.2.2.tar.gz
.
File metadata
- Download URL: ipydraw-0.2.2.tar.gz
- Upload date:
- Size: 37.1 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08fc62610666e7d96421cc107e588c2efc9119079ed1e87f8137afa9e2535398 |
|
MD5 | db4861abee2ec87a5988db5dd9d27949 |
|
BLAKE2b-256 | cbcdf6096838e64a6f2fda7d2af0f79f4a5beefb12d3f54a8776fa235cb4f663 |
File details
Details for the file ipydraw-0.2.2-py2.py3-none-any.whl
.
File metadata
- Download URL: ipydraw-0.2.2-py2.py3-none-any.whl
- Upload date:
- Size: 50.8 kB
- Tags: Python 2, Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a42ad218111a8d4710e2c3bb114ce9d2bf7996b19130f4290694c2a993dbed8 |
|
MD5 | a6d72a97e2fe57052a7b018a95c3a47a |
|
BLAKE2b-256 | c6b9575407eb5b7f5d882fdbf2b78ccfe16c1811eadb4b434ee03feded96a096 |