A declarative drawing API
Project description
Chalk is a declarative drawing library built on top of PyCairo. The API draws heavy inspiration from Haskell's diagrams, Scala's doodle and Jeremy Gibbons's lecture notes on Functional Programming for Domain−Specific Languages.
⚠️ The library is still very much work in progress and subject to change.
Overview
Below we provide a brief introduction of the main functionality of the library.
These examples are available in the examples/intro.py
file.
We start by importing the colour
module and the diagrams
functions:
from colour import Color
from chalk import *
We also define some colors that will be shortly used:
papaya = Color("#ff9700")
blue = Color("#005FDB")
We can easily create basic shapes (the functions circle
, square
, triangle
) and style them with various attributes (the methodsfill_color
, line_color
, line_width
).
For example:
d = circle(1).fill_color(papaya)
The diagram can be saved to an image using the render
method:
d.render("examples/output/intro-01.png", height=64)
We can glue together two diagrams using the combinators atop
(or +
), beside
(or |
), above
(or /
).
For example:
circle(2).fill_color(papaya) | square(1).fill_color(blue)
which is equivalent to
circle(2).fill_color(papaya).beside(square(1).fill_color(blue))
This code produces the following image:
We also provide combinators for a list of diagrams:
hcat
for horizontal composition, vcat
for vertical composition.
For example:
hcat(circle(0.1 * i) for i in range(1, 6)).fill_color(blue)
We can use Python functions to build more intricate diagrams:
def sierpinski(n: int, size: int) -> Diagram:
if n <= 1:
return triangle(size)
else:
smaller = sierpinski(n - 1, size / 2)
return smaller.above(smaller.beside(smaller).center_xy())
d = sierpinski(5, 4).fill_color(papaya)
For more examples, please check the examples
folder.
These scripts can be run using Streamlit:
streamlit run examples/squares.py
streamlit run examples/escher_square_limit.py
streamlit run examples/hanoi.py
Acknowledgements
Special thanks to Alexander Rush for contributing significantly to this library and also to Ionuț G. Stan for providing many useful insights and comments!
TODO
- Add example: Brent–Kung parallel prefix circuit.
- Remove dependency on streamlit.
- Decide on a more inspired name for the library.
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
Hashes for chalk_diagrams-0.1.dev0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 801602f54790dd3c0b72e11e62c905b8b9098b68d21f9d055839df80cbea9cbd |
|
MD5 | 35c51cd4c396776570584faafd34d990 |
|
BLAKE2b-256 | 0c228f049532d448de65638f4d3191714cd126b9540a2f4ad2585e8c5ffd5126 |