Skip to main content

Automatically launch p5js sketches written in python

Project description

Write your p5js sketches with python and automatically launch them in a new window just by running your script.

  • no html
  • no javascript
  • just python

Also included: autoreload on save!

Installation

pip install p52

Usage

open your favourite editor and create a new file <script.py> and save it wherever you want

from p52 import *

def setup():
    createCanvas(windowWidth, windowHeight)
    stroke(100, 100, 100, 100)
    noFill()


def draw():
    background(51)
    ellipse(mouseX, mouseY, 30, 30)

open a terminal, move to the folder where you saved the script and run it with python3 <script.py> or just use your editor "run" button if available.

A new window should popup with your sketch!

Bonus: try changing your code and saving, the changes should automatically be reflected in the sketch.

Extra functionalities

I decided to put some extra functionalities that diverge from standard python and p5js practices because I found them more convenient:

magic self

Tired of having to define vars before setup() in order to have them available inside draw()? now you can define vars inside a "fake" self object like this:

from p52 import *

def setup():
    createCanvas(windowWidth, windowHeight)
    stroke(100, 100, 100, 100)
    noFill()
    self.point = (100, 100)


def draw():
    background(51)
    line(0, 0, *self.point)

sketch saving and reloading

when viewing your sketch you can press "s" to save a screenshot of it inside the current folder. Likewise you can press "r" to reload the sketch.

utility functions

  • canvas() creates a canvas without having to specify its dimensions, defaulting to full window size
  • center() changes the coordinate system: (0, 0) is now in the center of the sketch and the y-coordinates go upwards

more to come...

inject js libraries (experimental!)

if you have a js library that you would like to use inside the sketch you can download it and put it into the same folder as your python sketch and use injectJs('library.js'). Doing so automatically include the library inside the generated html file.

How does it work?

This package is heavily inspired by the library pyp5js and leverages transcrypt to compile python to javascript behind the scenes.

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

p5two-0.0.1.tar.gz (51.0 kB view details)

Uploaded Source

Built Distribution

p5two-0.0.1-py3-none-any.whl (51.9 kB view details)

Uploaded Python 3

File details

Details for the file p5two-0.0.1.tar.gz.

File metadata

  • Download URL: p5two-0.0.1.tar.gz
  • Upload date:
  • Size: 51.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.28.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.4

File hashes

Hashes for p5two-0.0.1.tar.gz
Algorithm Hash digest
SHA256 0b9103c10d577353e496bff558fc4ec062caf2e75f4acb5b9e21ee447ababeac
MD5 e2bcf60dbff35bdf973cac41a9999e8d
BLAKE2b-256 f7fce29fb8083ce99fa6c42b6d01c60ff602a33710dc1edde5044a067c5a4940

See more details on using hashes here.

File details

Details for the file p5two-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: p5two-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 51.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.28.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.4

File hashes

Hashes for p5two-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f111078f7926b4d2c4f8ffda97167d3e5c172805e72049af96566c1382fe1968
MD5 f0e79ff0cd2631b08b9a9d727a3e0bd1
BLAKE2b-256 a8a78b96f028a58f432ddace66954ed61d733199b65189f6117a0987f8bc2a50

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