Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PyPI/Package/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Python WebUI v2.5.1
# Python WebUI v2.5.2

> Use any web browser as GUI, with Python in the backend and HTML5 in the frontend, all in a lightweight Python package.

Expand Down
2 changes: 1 addition & 1 deletion PyPI/Package/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "webui2"
version = "2.5.1"
version = "2.5.2"
authors = [
{ name="Hassan Draga" },
]
Expand Down
74 changes: 54 additions & 20 deletions PyPI/Package/src/webui/webui.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Python WebUI v2.5.1
# Python WebUI v2.5.2
#
# http://webui.me
# https://github.com/webui-dev/python-webui
Expand All @@ -11,6 +11,7 @@
# webui.py
from __future__ import annotations

import array
import warnings
from typing import Any, Callable, Optional, TypeAlias
from ctypes import *
Expand Down Expand Up @@ -209,29 +210,45 @@ def close_client(self) -> None:
_raw.webui_close_client(byref(self._c_event()))

# -- send_raw_client ----------------------------
def send_raw_client(self, function: str, raw: Optional[int], size: int) -> None:
def send_raw_client(self, function: str, data: Union[bytes, bytearray, memoryview, array.array]) -> None:
"""Safely send raw data to the UI for a single client.

This function sends raw data to a JavaScript function in the UI. The JavaScript function must
be defined to accept the raw data, such as: `function myFunc(myData) {}`.

Args:
function (str): The name of the JavaScript function to receive the raw data, encoded in UTF-8.
raw (Optional[int]): The pointer to the raw data buffer. Must not be `None`.
size (int): The size of the raw data buffer in bytes.
function (str): The name of the JavaScript function to receive the raw data.
data (Union[bytes, bytearray, memoryview, array.array]): The raw data buffer.

Raises:
ValueError: If `raw` is `None`.
ValueError: If `data` is `None` or empty.

Example:
e.send_raw_client("myJavaScriptFunc", my_buffer, 64)
e.send_raw_client("myJavaScriptFunc", bytearray([0x01, 0x0A, 0xFF]))
# Sends 3 bytes of raw data to the JavaScript function `myJavaScriptFunc`.
"""
if raw is None:
raise ValueError("Invalid Pointer: Cannot send a null pointer.")
if data is None or len(data) == 0:
raise ValueError("Data must not be None or empty.")

# Ensure data is a memoryview for uniformity
if not isinstance(data, memoryview):
data = memoryview(data)

# Ensure that data is a writable copy to obtain void pointer,
# from_buffer will throw error if the buffer passed in is not writable.
if data.readonly:
data = memoryview(bytearray(data))

# Obtain a c_void_p pointer to the data buffer
ptr = c_void_p(addressof(c_char.from_buffer(data)))

# Determine the size of the data
size = len(data)

_raw.webui_send_raw_client(
byref(self._c_event()),
c_char_p(function.encode("utf-8")),
c_void_p(raw),
ptr,
c_size_t(size)
)

Expand Down Expand Up @@ -1024,33 +1041,50 @@ def set_icon(self, icon: str, icon_type: str) -> None:
_raw.webui_set_icon(c_size_t(self._window), icon.encode("utf-8"), icon_type.encode("utf-8"))

# -- send_raw -----------------------------------
def send_raw(self, function: str, raw: Optional[c_void_p], size: int) -> None:
"""Safely send raw data to the UI for all clients.
def send_raw(self, function: str, data: Union[bytes, bytearray, memoryview, array.array]) -> None:
"""
Safely send raw data to the UI for all clients.

This function sends a raw data buffer to a JavaScript function in the UI.
The JavaScript function should be capable of handling raw binary data.

Args:
function (str): The JavaScript function that will receive the raw data.
raw (Optional[c_void_p]): A pointer to the raw data buffer. Must not be `None`.
size (int): The size of the raw data in bytes.
data (Union[bytes, bytearray, memoryview, array.array]): The raw data buffer.

Raises:
ValueError: If `raw` is `None`.
ValueError: If `data` is `None` or empty.

Returns:
None

Example:
my_window.send_raw("myJavaScriptFunc", my_buffer, 64)
# Sends 64 bytes of raw data to the JavaScript function `myJavaScriptFunc`.
my_window.send_raw("myJavaScriptFunc", bytearray([0x01, 0x0A, 0xFF]))
# Sends 3 bytes of raw data to the JavaScript function `myJavaScriptFunc`.
"""
if raw is None:
raise ValueError("Invalid pointer: Cannot send a null pointer.")
if data is None or len(data) == 0:
raise ValueError("Data must not be None or empty.")

# Ensure data is a memoryview for uniformity
if not isinstance(data, memoryview):
data = memoryview(data)

# Ensure that data is a writable copy to obtain void pointer,
# from_buffer will throw error if the buffer passed in is not writable.
if data.readonly:
data = memoryview(bytearray(data))

# Obtain a c_void_p pointer to the data buffer
ptr = c_void_p(addressof(c_char.from_buffer(data)))

# Determine the size of the data
size = len(data)

# Call the underlying function to send the data
_raw.webui_send_raw(
c_size_t(self._window),
c_char_p(function.encode("utf-8")),
c_void_p(raw), # type: ignore
ptr,
c_size_t(size)
)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![Logo](https://raw.githubusercontent.com/webui-dev/webui-logo/main/webui_python.png)

# Python-WebUI v2.5.1
# Python-WebUI v2.5.2

[last-commit]: https://img.shields.io/github/last-commit/webui-dev/webui?style=for-the-badge&logo=github&logoColor=C0CAF5&labelColor=414868
[release-version]: https://img.shields.io/github/v/tag/webui-dev/webui?style=for-the-badge&logo=webtrees&logoColor=C0CAF5&labelColor=414868&color=7664C6
Expand Down
32 changes: 32 additions & 0 deletions examples/sending-raw/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Hello there!</h1>

<script src="/webui.js"></script>
<script type="application/javascript">
function myJavaScriptFunc(rawData) {
// `rawData` should be a Uint8Array variable
console.log("rawData: ", rawData)

// Create a Blob from the raw data, specifying the MIME type as 'image/png'
const blob = new Blob([rawData], { type: 'image/png' });

// Generate a URL for the Blob
const imageUrl = URL.createObjectURL(blob);

// Create an <img> element and set its src attribute to the Blob URL
const img = document.createElement('img');
img.src = imageUrl;

// Add the img element to the body of the html
document.body.appendChild(img);
console.log("Image has been added to the page.");
}
</script>
</body>
</html>
24 changes: 24 additions & 0 deletions examples/sending-raw/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from webui import webui

def main():
# Create an instance of a Window object
my_window = webui.Window()

# Open an image file and read it in as byte data.
with open("./webui_python.png", 'rb') as file:
raw_bytes: bytes = file.read()

# Open the window using the html file in the project while getting the appropriate browser for the user.
my_window.show_browser("index.html", my_window.get_best_browser())

#print(raw_bytes)
# Send over the byte data from the picture to the javascript function we have in the html.
my_window.send_raw("myJavaScriptFunc", raw_bytes)

# waits for all windows to close before terminating the program.
webui.wait()

if __name__ == "__main__":
main()


Binary file added examples/sending-raw/webui_python.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.