Skip to content

Commit ab34be0

Browse files
committed
updated examples to reflect pypackage install, updated serve_folder example to exclude the set_file_handler portion, commented out set_file_handler and set_file_handler_window
1 parent 0c18eb5 commit ab34be0

File tree

9 files changed

+173
-188
lines changed

9 files changed

+173
-188
lines changed

PyPI/Package/src/webui/webui.py

Lines changed: 113 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# webui.py
22
from __future__ import annotations
3+
4+
import warnings
35
from typing import Callable, Optional
46
from ctypes import *
57

@@ -712,7 +714,10 @@ def show_browser(self, content: str, browser: Browser) -> bool:
712714
success = my_window.show_browser("<html>...</html>", Browser.Chrome)
713715
success = my_window.show_browser("index.html", Browser.Firefox)
714716
"""
715-
return _raw.webui_show_browser(c_size_t(self._window), c_char_p(content.encode("utf-8")), c_size_t(browser.value))
717+
success = bool(_raw.webui_show_browser(c_size_t(self._window), c_char_p(content.encode("utf-8")), c_size_t(browser.value)))
718+
if not success: warnings.warn("The browser you selected might not be installed on your system.")
719+
720+
return success
716721

717722
# -- start_server -------------------------------
718723
def start_server(self, content: str) -> str:
@@ -864,113 +869,113 @@ def set_root_folder(self, path: str) -> bool:
864869
"""
865870
return bool(_raw.webui_set_root_folder(c_size_t(self._window), path.encode("utf-8")))
866871

867-
# -- set_file_handler ---------------------------
868-
def set_file_handler(self, handler: Callable[[str], Optional[str]]) -> None:
869-
"""Set a custom file handler for serving files.
870-
871-
This function registers a custom file handler that processes file requests
872-
and serves HTTP responses. The handler must return a full HTTP response
873-
(headers and body) as a UTF-8 encoded string. Setting a new handler overrides
874-
any previously registered file handler.
875-
876-
Args:
877-
handler (Callable[[str], str]): A function that takes a filename as input
878-
and returns a complete HTTP response as a string.
879-
880-
Returns:
881-
None
882-
883-
Example:
884-
def my_handler(filename: str) -> str:
885-
response_body = "Hello, World!"
886-
response_headers = (
887-
"HTTP/1.1 200 OK\r\n"
888-
"Content-Type: text/plain\r\n"
889-
f"Content-Length: {len(response_body)}\r\n"
890-
"\r\n"
891-
)
892-
return response_headers + response_body
893-
894-
my_window.set_file_handler(my_handler)
895-
"""
896-
def _internal_file_handler(filename_ptr: c_char_p, length_ptr: POINTER(c_int)) -> c_void_p:
897-
"""
898-
Internal C callback that matches the signature required by webui_set_file_handler_window.
899-
"""
900-
# Decode the incoming filename from C
901-
filename = filename_ptr.decode('utf-8') if filename_ptr else ""
902-
903-
# Call the Python-level handler to get the HTTP response
904-
response_bytes = handler(filename).encode("utf-8")
905-
906-
# Create a ctypes buffer from the Python bytes; this buffer must remain alive
907-
# at least until WebUI is done with it.
908-
buf = create_string_buffer(response_bytes)
909-
910-
# Set the length (the int* that C expects)
911-
length_ptr[0] = len(response_bytes)
912-
913-
# Return a pointer (void*) to the buffer
914-
return cast(buf, c_void_p)
915-
916-
# Keep a reference so it doesn't get garbage collected
917-
self._file_handler_cb = filehandler_window_callback(_internal_file_handler)
918-
_raw.webui_set_file_handler_window(c_size_t(self._window), _raw.FILE_HANDLER_CB(self._file_handler_cb))
919-
920-
921-
# -- set_file_handler_window --------------------
922-
def set_file_handler_window(self, handler: Callable[[int, str], Optional[str]]) -> None:
923-
"""Set a custom file handler for a specific window.
924-
925-
This function registers a custom file handler that processes file requests
926-
for a specific window and serves HTTP responses. The handler must return
927-
a full HTTP response (headers and body) as a UTF-8 encoded string.
928-
Setting a new handler overrides any previously registered file handler.
929-
930-
Args:
931-
handler (Callable[[int, str], str]): A function that takes a window ID
932-
and a filename as input and returns a complete HTTP response as a string.
933-
934-
Returns:
935-
None
936-
937-
Example:
938-
def my_handler(window_id: int, filename: str) -> str:
939-
response_body = "Hello, World!"
940-
response_headers = (
941-
"HTTP/1.1 200 OK\r\n"
942-
"Content-Type: text/plain\r\n"
943-
f"Content-Length: {len(response_body)}\r\n"
944-
"\r\n"
945-
)
946-
return response_headers + response_body
947-
948-
my_window.set_file_handler_window(my_handler)
949-
"""
950-
def _internal_file_handler(window_id: c_size_t, filename_ptr: c_char_p, length_ptr: POINTER(c_int)) -> c_void_p:
951-
"""
952-
Internal C callback that matches the signature required by webui_set_file_handler_window.
953-
"""
954-
# Decode the incoming filename from C
955-
filename = filename_ptr.decode('utf-8') if filename_ptr else ""
956-
957-
# Call the Python-level handler to get the HTTP response
958-
response_bytes = handler(int(window_id), filename).encode("utf-8")
959-
960-
# Create a ctypes buffer from the Python bytes; this buffer must remain alive
961-
# at least until WebUI is done with it.
962-
buf = create_string_buffer(response_bytes)
963-
964-
# Set the length (the int* that C expects)
965-
length_ptr[0] = len(response_bytes)
966-
967-
# Return a pointer (void*) to the buffer
968-
return cast(buf, c_void_p)
969-
970-
# Keep a reference so it doesn't get garbage collected
971-
self._file_handler_cb = filehandler_window_callback(_internal_file_handler)
972-
973-
_raw.webui_set_file_handler_window(c_size_t(self._window), self._file_handler_cb)
872+
# -- set_file_handler --------------------------- # TODO: still errors on call to c bind
873+
# def set_file_handler(self, handler: Callable[[str], Optional[str]]) -> None:
874+
# """Set a custom file handler for serving files.
875+
#
876+
# This function registers a custom file handler that processes file requests
877+
# and serves HTTP responses. The handler must return a full HTTP response
878+
# (headers and body) as a UTF-8 encoded string. Setting a new handler overrides
879+
# any previously registered file handler.
880+
#
881+
# Args:
882+
# handler (Callable[[str], str]): A function that takes a filename as input
883+
# and returns a complete HTTP response as a string.
884+
#
885+
# Returns:
886+
# None
887+
#
888+
# Example:
889+
# def my_handler(filename: str) -> str:
890+
# response_body = "Hello, World!"
891+
# response_headers = (
892+
# "HTTP/1.1 200 OK\r\n"
893+
# "Content-Type: text/plain\r\n"
894+
# f"Content-Length: {len(response_body)}\r\n"
895+
# "\r\n"
896+
# )
897+
# return response_headers + response_body
898+
#
899+
# my_window.set_file_handler(my_handler)
900+
# """
901+
# def _internal_file_handler(filename_ptr: c_char_p, length_ptr: POINTER(c_int)) -> c_void_p:
902+
# """
903+
# Internal C callback that matches the signature required by webui_set_file_handler_window.
904+
# """
905+
# # Decode the incoming filename from C
906+
# filename = filename_ptr.decode('utf-8') if filename_ptr else ""
907+
#
908+
# # Call the Python-level handler to get the HTTP response
909+
# response_bytes = handler(filename).encode("utf-8")
910+
#
911+
# # Create a ctypes buffer from the Python bytes; this buffer must remain alive
912+
# # at least until WebUI is done with it.
913+
# buf = create_string_buffer(response_bytes)
914+
#
915+
# # Set the length (the int* that C expects)
916+
# length_ptr[0] = len(response_bytes)
917+
#
918+
# # Return a pointer (void*) to the buffer
919+
# return cast(buf, c_void_p)
920+
#
921+
# # Keep a reference so it doesn't get garbage collected
922+
# self._file_handler_cb = filehandler_window_callback(_internal_file_handler)
923+
# _raw.webui_set_file_handler_window(c_size_t(self._window), _raw.FILE_HANDLER_CB(self._file_handler_cb))
924+
925+
926+
# -- set_file_handler_window -------------------- # TODO: still errors on call to c bind
927+
# def set_file_handler_window(self, handler: Callable[[int, str], Optional[str]]) -> None:
928+
# """Set a custom file handler for a specific window.
929+
#
930+
# This function registers a custom file handler that processes file requests
931+
# for a specific window and serves HTTP responses. The handler must return
932+
# a full HTTP response (headers and body) as a UTF-8 encoded string.
933+
# Setting a new handler overrides any previously registered file handler.
934+
#
935+
# Args:
936+
# handler (Callable[[int, str], str]): A function that takes a window ID
937+
# and a filename as input and returns a complete HTTP response as a string.
938+
#
939+
# Returns:
940+
# None
941+
#
942+
# Example:
943+
# def my_handler(window_id: int, filename: str) -> str:
944+
# response_body = "Hello, World!"
945+
# response_headers = (
946+
# "HTTP/1.1 200 OK\r\n"
947+
# "Content-Type: text/plain\r\n"
948+
# f"Content-Length: {len(response_body)}\r\n"
949+
# "\r\n"
950+
# )
951+
# return response_headers + response_body
952+
#
953+
# my_window.set_file_handler_window(my_handler)
954+
# """
955+
# def _internal_file_handler(window_id: c_size_t, filename_ptr: c_char_p, length_ptr: POINTER(c_int)) -> c_void_p:
956+
# """
957+
# Internal C callback that matches the signature required by webui_set_file_handler_window.
958+
# """
959+
# # Decode the incoming filename from C
960+
# filename = filename_ptr.decode('utf-8') if filename_ptr else ""
961+
#
962+
# # Call the Python-level handler to get the HTTP response
963+
# response_bytes = handler(int(window_id), filename).encode("utf-8")
964+
#
965+
# # Create a ctypes buffer from the Python bytes; this buffer must remain alive
966+
# # at least until WebUI is done with it.
967+
# buf = create_string_buffer(response_bytes)
968+
#
969+
# # Set the length (the int* that C expects)
970+
# length_ptr[0] = len(response_bytes)
971+
#
972+
# # Return a pointer (void*) to the buffer
973+
# return cast(buf, c_void_p)
974+
#
975+
# # Keep a reference so it doesn't get garbage collected
976+
# self._file_handler_cb = filehandler_window_callback(_internal_file_handler)
977+
#
978+
# _raw.webui_set_file_handler_window(c_size_t(self._window), self._file_handler_cb)
974979

975980
# -- is_shown -----------------------------------
976981
def is_shown(self) -> bool:

examples/andershell3000/andershell3000.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import sys
2-
sys.path.append('../../PyPI/Package/src/webui')
3-
import webui
4-
51
# Install WebUI
62
# pip install --upgrade webui2
7-
# from webui import webui
3+
from webui import webui
84
import subprocess
95
import time
106

@@ -53,6 +49,6 @@ def run_command(e : webui.Event):
5349
MyWindow = webui.Window()
5450
MyWindow.bind("Run", run_command)
5551
MyWindow.set_root_folder("ui/")
56-
MyWindow.show_browser('index.html', webui.Browser.Chrome)
52+
MyWindow.show_browser('index.html', webui.Browser.AnyBrowser)
5753
webui.wait()
5854
executor.close()

examples/hello_world.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import sys
2-
sys.path.append('../PyPI/Package/src/webui')
3-
import webui
4-
51
# Install WebUI
62
# pip install --upgrade webui2
7-
# from webui import webui
3+
from webui import webui
84

95
# CSS
106
css = """

examples/minimal.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import sys
2-
sys.path.append('../PyPI/Package/src/webui')
3-
import webui
4-
51
# Install WebUI
62
# pip install --upgrade webui2
73

8-
# from webui import webui
4+
from webui import webui
95

106
MyWindow = webui.Window()
117
MyWindow.show('<html><head><script src=\"webui.js\"></script></head> Hello World ! </html>')

examples/public_ui.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import sys
2-
sys.path.append('../PyPI/Package/src/webui')
3-
import webui
4-
5-
# from webui import webui # GUI
1+
from webui import webui # GUI
62
import socket # To get local IP
73

84

examples/serve_folder/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ <h4><a href="second.html">Second Page As A Simple Link (Local file)</a></h4>
8181
<br />
8282
<button id="OpenNewWindow">Open The Second Window (Local file)</button>
8383
<br />
84-
<h4><a href="test.txt">Static file example (Embedded)</a></h4>
85-
<h4><a href="dynamic.html">Dynamic file example (Embedded)</a></h4>
84+
<!-- <h4><a href="test.txt">Static file example (Embedded)</a></h4>-->
85+
<!-- <h4><a href="dynamic.html">Dynamic file example (Embedded)</a></h4>-->
8686
<p>
8787
Unicode Test:<br /><br />
8888
مرحبًا<br /> <!-- Arabic -->

0 commit comments

Comments
 (0)