Skip to main content

A little Python library for making simple HTML/JS GUI apps

Project description

Webevents

A little, zero-dependency Python library for making simple HTML/JS GUI apps

Installation

Note that only Python >= 3.6 is supported

pip install webevents

Example: ping-pong

Python sends a number to Javascript, which outputs it out to console, increments it and sends the number back to Python for displaying in terminal. And so on.

We will have the directory structure presented below. Generally speaking, In the "web" folder you can put your HTML/CSS/JS application. "Ping-pong" application will only consist of one "index.html" file.

├── example.py
└── web
    └── index.html

index.html:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Webevents "Ping-pong"</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="webevents.js"></script>
</head>
<body>
  Demonstration in console
  <script>
  webevents.addEventListener("ping", function(number){
    console.log(number);
    webevents.fireEvent("pong", number+1);
  });
  </script>
</body>
</html>

example.py:

import webbrowser
import webevents

def pong_callback(number):
    print(number)
    snakes_events.fire_event("ping", int(number) + 1)

address = ("localhost", 8080)
snakes_events = webevents.run(address, "web")
snakes_events.add_termination_callback(lambda: print("The end!"))
snakes_events.add_event_listener("pong", pong_callback)

webbrowser.open_new_tab("http://{}:{}".format(*address))

# initial ping
snakes_events.fire_event("ping", 0)

try:
    while True:
        pass
except KeyboardInterrupt:
    snakes_events.terminate()

Multiple callbacks are supported. You can remove events with:

  • remove_event_listener(event_type, callback) in Python
  • removeEventListener(event_type, callback) in JS

In Python you can add termination callbacks with add_termination_callback(callback). It may be useful in multi process applications. Note that there is no need to add termination callbacks if you don't need them.

Usually termination is occurred when user closes the browser page. But you can disable this by passing timeout=None in webevents.run(). You always can terminate the "webevents" with webevents.terminate().

Javascript output:

0 2 4 6 8 10 12 ...

Python output:

1 3 5 7 9 11 13 ... The end!

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

webevents-0.1.4.tar.gz (4.5 kB view hashes)

Uploaded Source

Built Distribution

webevents-0.1.4-py3-none-any.whl (5.0 kB view hashes)

Uploaded Python 3

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