Skip to content

Commit 51faad2

Browse files
committed
added new example showing how to use send_raw, updated send_raw and send_raw_client functions, and updated version text to 2.5.2
1 parent 14fa456 commit 51faad2

File tree

6 files changed

+66
-22
lines changed

6 files changed

+66
-22
lines changed

PyPI/Package/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Python WebUI v2.5.1
1+
# Python WebUI v2.5.2
22

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

PyPI/Package/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "webui2"
7-
version = "2.5.1"
7+
version = "2.5.2"
88
authors = [
99
{ name="Hassan Draga" },
1010
]

PyPI/Package/src/webui/webui.py

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Python WebUI v2.5.1
1+
# Python WebUI v2.5.2
22
#
33
# http://webui.me
44
# https://github.com/webui-dev/python-webui
@@ -11,6 +11,7 @@
1111
# webui.py
1212
from __future__ import annotations
1313

14+
import array
1415
import warnings
1516
from typing import Callable, Optional
1617
from ctypes import *
@@ -210,29 +211,45 @@ def close_client(self) -> None:
210211
_raw.webui_close_client(byref(self._c_event()))
211212

212213
# -- send_raw_client ----------------------------
213-
def send_raw_client(self, function: str, raw: Optional[int], size: int) -> None:
214+
def send_raw_client(self, function: str, data: Union[bytes, bytearray, memoryview, array.array]) -> None:
214215
"""Safely send raw data to the UI for a single client.
215216
216217
This function sends raw data to a JavaScript function in the UI. The JavaScript function must
217218
be defined to accept the raw data, such as: `function myFunc(myData) {}`.
218219
219220
Args:
220-
function (str): The name of the JavaScript function to receive the raw data, encoded in UTF-8.
221-
raw (Optional[int]): The pointer to the raw data buffer. Must not be `None`.
222-
size (int): The size of the raw data buffer in bytes.
221+
function (str): The name of the JavaScript function to receive the raw data.
222+
data (Union[bytes, bytearray, memoryview, array.array]): The raw data buffer.
223223
224224
Raises:
225-
ValueError: If `raw` is `None`.
225+
ValueError: If `data` is `None` or empty.
226226
227227
Example:
228-
e.send_raw_client("myJavaScriptFunc", my_buffer, 64)
228+
e.send_raw_client("myJavaScriptFunc", bytearray([0x01, 0x0A, 0xFF]))
229+
# Sends 3 bytes of raw data to the JavaScript function `myJavaScriptFunc`.
229230
"""
230-
if raw is None:
231-
raise ValueError("Invalid Pointer: Cannot send a null pointer.")
231+
if data is None or len(data) == 0:
232+
raise ValueError("Data must not be None or empty.")
233+
234+
# Ensure data is a memoryview for uniformity
235+
if not isinstance(data, memoryview):
236+
data = memoryview(data)
237+
238+
# Ensure that data is a writable copy to obtain void pointer,
239+
# from_buffer will throw error if the buffer passed in is not writable.
240+
if data.readonly:
241+
data = memoryview(bytearray(data))
242+
243+
# Obtain a c_void_p pointer to the data buffer
244+
ptr = c_void_p(addressof(c_char.from_buffer(data)))
245+
246+
# Determine the size of the data
247+
size = len(data)
248+
232249
_raw.webui_send_raw_client(
233250
byref(self._c_event()),
234251
c_char_p(function.encode("utf-8")),
235-
c_void_p(raw),
252+
ptr,
236253
c_size_t(size)
237254
)
238255

@@ -1025,33 +1042,50 @@ def set_icon(self, icon: str, icon_type: str) -> None:
10251042
_raw.webui_set_icon(c_size_t(self._window), icon.encode("utf-8"), icon_type.encode("utf-8"))
10261043

10271044
# -- send_raw -----------------------------------
1028-
def send_raw(self, function: str, raw: Optional[c_void_p], size: int) -> None:
1029-
"""Safely send raw data to the UI for all clients.
1045+
def send_raw(self, function: str, data: Union[bytes, bytearray, memoryview, array.array]) -> None:
1046+
"""
1047+
Safely send raw data to the UI for all clients.
10301048
10311049
This function sends a raw data buffer to a JavaScript function in the UI.
10321050
The JavaScript function should be capable of handling raw binary data.
10331051
10341052
Args:
10351053
function (str): The JavaScript function that will receive the raw data.
1036-
raw (Optional[c_void_p]): A pointer to the raw data buffer. Must not be `None`.
1037-
size (int): The size of the raw data in bytes.
1054+
data (Union[bytes, bytearray, memoryview, array.array]): The raw data buffer.
10381055
10391056
Raises:
1040-
ValueError: If `raw` is `None`.
1057+
ValueError: If `data` is `None` or empty.
10411058
10421059
Returns:
10431060
None
10441061
10451062
Example:
1046-
my_window.send_raw("myJavaScriptFunc", my_buffer, 64)
1047-
# Sends 64 bytes of raw data to the JavaScript function `myJavaScriptFunc`.
1063+
my_window.send_raw("myJavaScriptFunc", bytearray([0x01, 0x0A, 0xFF]))
1064+
# Sends 3 bytes of raw data to the JavaScript function `myJavaScriptFunc`.
10481065
"""
1049-
if raw is None:
1050-
raise ValueError("Invalid pointer: Cannot send a null pointer.")
1066+
if data is None or len(data) == 0:
1067+
raise ValueError("Data must not be None or empty.")
1068+
1069+
# Ensure data is a memoryview for uniformity
1070+
if not isinstance(data, memoryview):
1071+
data = memoryview(data)
1072+
1073+
# Ensure that data is a writable copy to obtain void pointer,
1074+
# from_buffer will throw error if the buffer passed in is not writable.
1075+
if data.readonly:
1076+
data = memoryview(bytearray(data))
1077+
1078+
# Obtain a c_void_p pointer to the data buffer
1079+
ptr = c_void_p(addressof(c_char.from_buffer(data)))
1080+
1081+
# Determine the size of the data
1082+
size = len(data)
1083+
1084+
# Call the underlying function to send the data
10511085
_raw.webui_send_raw(
10521086
c_size_t(self._window),
10531087
c_char_p(function.encode("utf-8")),
1054-
c_void_p(raw),
1088+
ptr,
10551089
c_size_t(size)
10561090
)
10571091

examples/sending-raw/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
</head>
7+
<body>
8+
9+
</body>
10+
</html>

examples/sending-raw/main.py

Whitespace-only changes.
6.57 KB
Loading

0 commit comments

Comments
 (0)