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 PythonremoveEventListener(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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file webevents-0.1.4.tar.gz
.
File metadata
- Download URL: webevents-0.1.4.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1920ec15b3086ff4b2128d0ec724b8939e04c40fb2881fdfa7f6a79e54f531df |
|
MD5 | a2b0cbf98b55668fceebec2e8baa571a |
|
BLAKE2b-256 | c23a8f2cf3bc4f8239320bef5726c5c4c20a5ad4c689b4f559d3571038f84037 |
File details
Details for the file webevents-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: webevents-0.1.4-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | de9a1139b9ffb81bef5c96e8fc0d757804b8c00d176c3d054d71df014c9ed224 |
|
MD5 | 0fa664f376e0759af60c7c2c453efeae |
|
BLAKE2b-256 | d0d7812a86eab444bcb13b90066564026946ad0d94c17ad9f001c86927e6c64a |