Skip to main content

A Python package for creative coding on the web.

Project description

proceso

A Python package for creative coding on the web

proceso is a Python package for creative coding on the web, with a focus on making coding accessible and inclusive for artists, designers, educators, beginners, and anyone else! The package provides a Pythonic interface to the p5.js library and is heavily inspired by py5. proceso is designed for Pyodide-based environments with a focus on PyScript.

Here is an example of how to create a proceso sketch with PyScript using Python, HTML, and CSS:

sketch.py

from proceso import Sketch


p5 = Sketch()
p5.describe("A screen reader accessible description for the canvas.")

index.html

<!DOCTYPE html>
<html lang="en-us">

<head>
    <title>My Sketch</title>
    <meta charset="utf-8" />

    <link rel="stylesheet" href="https://pyscript.net/releases/2023.03.1/pyscript.css" />
    <script defer src="https://pyscript.net/releases/2023.03.1/pyscript.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.min.js"></script>
    <py-config>
        packages = ["numpy", "proceso"]
    </py-config>
</head>

<body>
    <main></main>
    <py-script src="sketch.py"></py-script>
</body>

</html>

style.css

html,
body {
    margin: 0;
    padding: 0;
}

canvas {
    display: block;
}

Static Sketches

Similar to Processing, proceso enables beginners to start programming with "static sketches" before introducing animation and interaction. The following example draws a few shapes and a flower on the screen.

from proceso import Sketch


p5 = Sketch()
p5.describe("A rectangle, circle, triangle, and flower drawn in pink on a gray background.")

# Create the canvas
p5.create_canvas(720, 400)
p5.background(200)

# Set colors
p5.fill(204, 101, 192, 127)
p5.stroke(127, 63, 120)

# A rectangle
p5.rect(40, 120, 120, 40)
# A circle
p5.circle(240, 240, 80)
# A triangle
p5.triangle(300, 100, 320, 100, 310, 80)

# A design for a simple flower
p5.translate(580, 200)
p5.no_stroke()
for _ in range(10):
    p5.ellipse(0, 30, 20, 80)
    p5.rotate(p5.PI / 5)

View sketch

Active Sketches

proceso's "active sketches" provide the run_sketch() method to handle initialization, looping, and events. The sketch below simulates the synchronization behavior observed in some species of fireflies.

from proceso import Sketch


p5 = Sketch()
p5.describe("Ten white circles moving like fireflies on a dark blue background.")

bugs = []
num_bugs = 10
coupling: object


def setup():
    p5.create_canvas(720, 400)
    global coupling
    coupling = p5.create_slider(0, 10, 5)
    for _ in range(num_bugs):
        bug = Bug()
        bugs.append(bug)


def draw():
    p5.background("midnightblue")

    for bug in bugs:
        bug.sync()

    for bug in bugs:
        bug.update()
        bug.check_edges()
        bug.draw()


class Bug:
    def __init__(self):
        self.x = p5.width * 0.5
        self.y = p5.height * 0.5
        self.r = 5
        self.angle = p5.random(p5.TWO_PI)
        self.da_dt = 1
        self.dt = 0.01
        self.freq = p5.random(5, 10)

    def draw(self):
        a = p5.remap(self.angle % p5.TWO_PI, 0, p5.TWO_PI, 0, 255)
        p5.fill(255, a)
        p5.stroke(255, a)
        p5.circle(self.x, self.y, 2 * self.r)

    def update(self):
        self.x += p5.cos(self.angle)
        self.y += p5.sin(self.angle)
        self.angle += self.da_dt * self.dt
        self.da_dt = 0

    def check_edges(self):
        if self.x > p5.width + self.r:
            self.x = -self.r
        if self.x < -self.r:
            self.x = p5.width + self.r
        if self.y > p5.height + self.r:
            self.y = -self.r
        if self.y < -self.r:
            self.y = p5.height + self.r

    def sync(self):
        K_N = coupling.value() / num_bugs
        self.da_dt = self.freq
        for bug in bugs:
            self.da_dt += K_N * p5.sin(bug.angle - self.angle)


p5.run_sketch(setup=setup, draw=draw)

View sketch

Getting Started

Cloud: PyScript (account required)

PyScript is a great way to run proceso sketches with PyScript. Here is a project template.

Local: Anaconda + VS Code

Here is one possible setup for running sketches on your local machine:

  1. Install the Anaconda Distribution of Python.
  2. Install Visual Studio Code.
  3. Get started with Anaconda Navigator and create a new Python environment with Python 3.10.
  4. Use the Navigator to open a terminal with your new Python environment and pip install proceso.
  5. Open VS Code and install the Microsoft Python extension to enable helpful features such as documentation, autocompletion, and so on.
  6. Select the interpreter created by your new Python environment.
  7. Add your HTML, CSS, and Python files and start coding.
  8. Open the terminal in VS Code and run python -m http.server to view your sketch in the browser.

Roadmap

  • Improve documentation
  • Finish IO API
  • Translate p5.js test suite?
  • Add CLI with hot reload
  • Add bindings for p5.js addon libraries

Acknowledgements

  • proceso is, first and foremost, an interface to the p5.js library. Nearly all of the package's documentation and examples are adapted from their p5.js counterparts. Portions of the source code are also adapted from the original JavaScript implementation.
  • The Vector class is lovingly borrowed from py5 as are most of py5's naming conventions.
  • Basthon, Py5.js, and pyp5js all pointed the way.

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

proceso-0.0.12.tar.gz (61.2 kB view details)

Uploaded Source

Built Distribution

proceso-0.0.12-py3-none-any.whl (66.1 kB view details)

Uploaded Python 3

File details

Details for the file proceso-0.0.12.tar.gz.

File metadata

  • Download URL: proceso-0.0.12.tar.gz
  • Upload date:
  • Size: 61.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for proceso-0.0.12.tar.gz
Algorithm Hash digest
SHA256 4a8821803fc611b80e65378f3916c3fec1dfc3051ca5c8348c87f08b919da2e2
MD5 a9d9bc603987f329b622127599ffd3aa
BLAKE2b-256 03541a5b3177425e3df6316e8e1c036460f7d04cafddd5a9b5960274f850edc9

See more details on using hashes here.

File details

Details for the file proceso-0.0.12-py3-none-any.whl.

File metadata

  • Download URL: proceso-0.0.12-py3-none-any.whl
  • Upload date:
  • Size: 66.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for proceso-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 1176f09b4909680bc456705aefd69e75a5f5c1b7ce34bba65fde28c75f23186e
MD5 dbfdce2c4c25a6b263b27c7cad847a69
BLAKE2b-256 0283f061f0d528a9e31dc8b3c42efe0b25ae97fe643b579770fc95c2ea831042

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