Skip to main content

Create excalidraw drawings with python

Project description

Excalidraw_Interface

Tests Badge Python Version Badge License Badge

A pythonic interface for creating diagrams in Excalidraw.

Based on https://github.com/BardOfCodes/procXD by Aditya Ganeshan (MIT License) and updated with modern python support and API improvements.

Example: Flowchart

flowchart.py

flowchart image

from Excalidraw_Interface import SketchBuilder

flowchart_items = ['First Step', 'Second Step', 'Third Step']

sb = SketchBuilder() # Create a Sketch

prev_item = sb.TextBox("Start Here", x = 0, y = 0) # Create a Text Box
for index, item in enumerate(flowchart_items):
    new_item = sb.TextBox(item, x = 0, y = (index+1) * 150) # Create a Text Box
    sb.create_binding_arrows(prev_item, new_item) # Create arrows between boxes
    prev_item = new_item

hcb = sb.HeaderContentBox("Header", "Content", x = -200, y = 400) # Create a multiline text box
circle = sb.Ellipse(200, 400, width=50, height=50, backgroundColor = 'red',
                    roughness=1) # Create a red circle in hand drawn style

sb.create_binding_arrows(prev_item, hcb, sb.DoubleArrow) # Create a double headed arrow
sb.create_binding_arrows(prev_item, circle, strokeColor = 'blue') # Create an blue arrow

sb.export_to_file('out.excalidraw')

Documenation

SketchBuilder

Every sketch starts by creating a sketch builder object.

from Excalidraw_Interface import SketchBuilder

sb = SketchBuilder() # Create a Sketch

Default settings for the sketch can be set using kwargs. For instance:

from Excalidraw_Interface import SketchBuilder

sb = SketchBuilder(roughness=2) # Create a hand drawn sketch

The list of configurable settings can be found in defaults.py

Exporting a Sketch

from Excalidraw_Interface import SketchBuilder

sb = SketchBuilder()
...
sb.export_to_file('my_sketch')
# OR
data = sb.export_to_json()

Creating Sketch Objects

Rectangles, Diamonds, Ellipses can be created with a center_x and center_y position. Width and height can also be set (defaults to 100). Other params can be set in kwargs.

from Excalidraw_Interface import SketchBuilder

sb = SketchBuilder()
sb.Rectangle(x = 0, y = 0)
sb.Diamond(x = 0, y = 0, width=50, height=20)
sb.Ellipse(x = 0, y = 0, backgroundColor='red')

Text, Lines, and Arrows have similar functions.

from Excalidraw_Interface import SketchBuilder

sb = SketchBuilder()
sb.Text('some text', x = 0, y = 0)
sb.Line((0,0), (100,100))
sb.Arrow((0,0), (100,100))
sb.DoubleArrow((0,0), (100,100))

TextBoxes and HeaderContentBoxes have special functions to create multiple objects at once. Config can be passed to txt_kwargs, rect_kwargs, header_kwargs, or content_kwargs.

from Excalidraw_Interface import SketchBuilder

sb = SketchBuilder()
sb.TextBox('some text', x = 0, y = 0)
sb.HeaderContentBox('TITLE', 'content', x = 0, y = 0)

Advanced Sketch Objects

To create arrows between two objects:

from Excalidraw_Interface import SketchBuilder

sb = SketchBuilder()
a = sb.TextBox('some text', x = 0, y = 0)
b = sb.TextBox('some text', x = 200, y = 200)
sb.create_binding_arrows(a, b)
sb.create_binding_arrows(a, b, sb.DoubleArrow, strokeColor='blue') #makes a blue double arrow

To create shape around an object (or Group):

from Excalidraw_Interface import SketchBuilder

sb = SketchBuilder()
a = sb.TextBox('some text', x = 0, y = 0)
outer_rect = sb.create_bounding_element(a)

To create groups:

from Excalidraw_Interface import SketchBuilder, Group

sb = SketchBuilder()
a = sb.TextBox('some text', x = 0, y = 0)
b = sb.TextBox('some text', x = 200, y = 200)

g = Group([a, b])

For more examples see advanced_groups.py

Notes:

Groups can be nested.

A special feature exists that allows one element of a group to be treated as the whole group for the purpose of further grouping (useful for bounding boxes).

if first_is_group:
    elems[0].apply_recursive_groups.extend(elems[1:])

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

excalidraw_interface-0.1.1.tar.gz (398.7 kB view details)

Uploaded Source

Built Distribution

Excalidraw_Interface-0.1.1-py3-none-any.whl (398.2 kB view details)

Uploaded Python 3

File details

Details for the file excalidraw_interface-0.1.1.tar.gz.

File metadata

  • Download URL: excalidraw_interface-0.1.1.tar.gz
  • Upload date:
  • Size: 398.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for excalidraw_interface-0.1.1.tar.gz
Algorithm Hash digest
SHA256 89887e559c16f5565fb1f235665f7be102cf268818beaed2f457da1db5dc63ba
MD5 e70f3a599d465c453dc5795bee574901
BLAKE2b-256 6b15b0cd0ffb509f947fe1cad8d072f3f83fe5fa737047201a4828f32a687f40

See more details on using hashes here.

File details

Details for the file Excalidraw_Interface-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for Excalidraw_Interface-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 da4bf3df2a1e93e1568e65db15e87f7dae9c43a39cfc640738a86b2c0b23eaa1
MD5 8c0b9f22e7742c5a9309aa9916f91916
BLAKE2b-256 7a1f9c92cb061f165222ca21d2238eed750f7e4fdfd28c7609ff6d828980ca76

See more details on using hashes here.

Supported by

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