Skip to main content

pygame wasm, package and run python/pygame directly in modern web browsers.

Reason this release was yanked:

cache cleaning not working on win32

Project description

pygbag

Python WebAssembly for everyone ( packager + test server )

Runs python code directly in modern web browsers, including mobile versions.

"your.app.folder" must contain a main.py and its loop must be async aware eg:

import asyncio

# Try explicitly to declare all your globals at once to facilitate compilation later.
COUNT_DOWN = 3

# Do init here and load any assets right now to avoid lag at runtime or network errors.


async def main():
    global COUNT_DOWN

    COUNT_DOWN = 3

    while True:

        # Do your rendering here, note that it's NOT an infinite loop,
        # and it is fired only when VSYNC occurs
        # Usually 1/60 or more times per seconds on desktop, maybe less on some mobile devices

        print(f"""

            Hello[{COUNT_DOWN}] from Python

""")

        await asyncio.sleep(0)  # Very important, and keep it 0

        if not COUNT_DOWN:
            return

        COUNT_DOWN = COUNT_DOWN - 1

# This is the program entry point:
asyncio.run(main())

# Do not add anything from here
# asyncio.run is non-blocking on pygame-wasm

Usage:

pip3 install pygbag --user --upgrade
pygbag your.app.folder

Command help:

pygbag --help your.app.folder

Example :

user@pp /data/git/pygbag $ python3 -m pygbag --help your.app.folder
 *pygbag 0.6.0*

Serving python files from [/data/git/pygbag/your.app.folder/build/web]

with no security/performance in mind, i'm just a test tool : don't rely on me
usage: __main__.py [-h] [--bind ADDRESS] [--directory DIRECTORY] [--PYBUILD PYBUILD] [--app_name APP_NAME] [--ume_block UME_BLOCK] [--can_close CAN_CLOSE] [--cache CACHE] [--package PACKAGE] [--title TITLE] [--version VERSION] [--build] [--html] [--no_opt]
                   [--archive] [--icon ICON] [--cdn CDN] [--template TEMPLATE] [--ssl SSL] [--port [PORT]]

options:
  -h, --help            show this help message and exit
  --bind ADDRESS        Specify alternate bind address [default: localhost]
  --directory DIRECTORY
                        Specify alternative directory [default:/data/git/pygbag/your.app.folder/build/web]
  --PYBUILD PYBUILD     Specify python version [default:3.11]
  --app_name APP_NAME   Specify user facing name of application [default:your.app.folder]
  --ume_block UME_BLOCK
                        Specify wait for user media engagement before running [default:1]
  --can_close CAN_CLOSE
                        Specify if window will ask confirmation for closing [default:0]
  --cache CACHE         md5 based url cache directory
  --package PACKAGE     package name, better make it unique
  --title TITLE         App nice looking name
  --version VERSION     override prebuilt version path [default:0.6.0]
  --build               build only, do not run test server
  --html                build as html with embedded assets (pygame-script)
  --no_opt              turn off assets optimizer
  --archive             make build/web.zip archive for itch.io
  --icon ICON           icon png file 32x32 min should be favicon.png
  --cdn CDN             web site to cache locally [default:https://pygame-web.github.io/archives/0.6/]
  --template TEMPLATE   index.html template [default:default.tmpl]
  --ssl SSL             enable ssl with server.pem and key.pem
  --port [PORT]         Specify alternate port [default: 8000]

Now navigate to http://localhost:8000 with a modern Browser.

Use http://localhost:8000#debug for getting a terminal and a sized down canvas.

For pygame-script go to http://localhost:8000/test.html for a game folder named "test".

V8 based browsers are preferred ( chromium/brave/chrome ... ) starting with 81.0.4044 ( android 4.4 ). Because they set baseline restrictions on WebAssembly loading. Using them while testing ensure proper operation on all browsers.

NOTES:

  • first load will be slower, because setting up local cache from cdn to avoid useless network transfer for getting pygame and cpython prebuilts.

  • each time there's a change in the code or template you must run pygbag your.app.folder but cache is not destroyed.

  • if you want to reset prebuilts cache, remove the build/web-cache folder in your.app.folder

BUILDING:

Pygbag is not only a python module, and rebuilding all the toolchain can be quite hard.

https://github.com/pygame-web/python-wasm-sdk <= build CPython (not pyodide)

The default is to build only pygame, but feel free to fork and add yours.

So read/use pygbag CI to see how to build pygame + the C loader (pymain) and how it is linked it to libpython + libpygame.

https://github.com/pygame-web/pygbag

Default prebuilts (pygame) used by pygbag are stored on github from the repo https://github.com/pygame-web/archives under versioned folders.

TEST REPL:

[interactive repl](http://pygame-web.github.io/showroom/python310.html?-d&noapp#pygame-scripts/hello.py%20arg1%20arg2)

ADDING STATIC MODULES:

see in package.d directory and use vendor/vendor.sh

SUPPORT FOR STATIC MODULES :

see in package.d/<vendor>/README.md for module <vendor> specific support

vendor readme (if applicable)

GENERIC PYGBAG SUPPORT OR PYGAME MODULE:

for generic help around pygbag

GENERIC CPYTHON WASM:

python.org forum

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

pygbag-0.6.5.tar.gz (32.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pygbag-0.6.5-py3-none-any.whl (34.6 kB view details)

Uploaded Python 3

File details

Details for the file pygbag-0.6.5.tar.gz.

File metadata

  • Download URL: pygbag-0.6.5.tar.gz
  • Upload date:
  • Size: 32.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for pygbag-0.6.5.tar.gz
Algorithm Hash digest
SHA256 64acda4587691eeedc3ad3c69d9e31c26e110f9eb28a4c91f8866a6d20e8a28c
MD5 361f0b22a446014feb472b63097928b5
BLAKE2b-256 3cffb73aa1c64c0889b7ddffbaa94913a1f118caccdd73e2f0231d1691442436

See more details on using hashes here.

File details

Details for the file pygbag-0.6.5-py3-none-any.whl.

File metadata

  • Download URL: pygbag-0.6.5-py3-none-any.whl
  • Upload date:
  • Size: 34.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for pygbag-0.6.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6659dd330685d5405ba137bafc44ff8827dabc6044c9f536bd2672e5c9ab12bb
MD5 75c1415804de52a7dde7e62a41dadc28
BLAKE2b-256 6a9f0b1f880b4ac206216d389b3e79cdf9625da22d32c2037d1f0733ebf68d85

See more details on using hashes here.

Supported by

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