Skip to content

Commit 54cb624

Browse files
committed
Updated install scripts (base_url was still set to target urls), started documenting window object functions
1 parent 4db78a1 commit 54cb624

File tree

5 files changed

+219
-60
lines changed

5 files changed

+219
-60
lines changed

PyPI/Package/src/webui/bootstrap.bat

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ GOTO :EOF
7373
:: Download WebUI library for only the current OS.
7474
:MINIMAL
7575

76-
SET "BASE_URL=https://github.com/webui-dev/webui/releases/download/2.5.0-beta.2/"
76+
:: Nightly base url
77+
SET "BASE_URL=https://github.com/webui-dev/webui/releases/download/nightly/"
78+
79+
:: Targeted base url
80+
:: SET "BASE_URL=https://github.com/webui-dev/webui/releases/download/2.5.0-beta.2/"
7781

7882
:: Check the CPU architecture
7983
IF "%PROCESSOR_ARCHITECTURE%"=="x86" (

PyPI/Package/src/webui/bootstrap.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ if [[ "$1" == "minimal" ]]; then
6666
# Download WebUI library for only the current OS.
6767

6868
# Nightly Build
69-
# BASE_URL="https://github.com/webui-dev/webui/releases/download/nightly/"
69+
BASE_URL="https://github.com/webui-dev/webui/releases/download/nightly/"
7070

7171
# Release
72-
BASE_URL="https://github.com/webui-dev/webui/releases/download/2.5.0-beta.2/"
72+
# BASE_URL="https://github.com/webui-dev/webui/releases/download/2.5.0-beta.2/"
7373

7474
# Detect OS (macOS / Linux)
7575
OS="linux"

PyPI/Package/src/webui/webui.py

Lines changed: 196 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,11 @@ def return_bool(self, b: bool) -> None:
584584

585585

586586
# -- Window Object ------------------------------
587-
class Window: # TODO: document
587+
class Window:
588588
"""
589-
Pythonic wrapper around a WebUI window.
589+
WebUI Window Object
590+
591+
Has all related functions that need a window reference to execute.
590592
"""
591593
def __init__(self, window_id: Optional[int] = None):
592594
"""
@@ -603,13 +605,17 @@ def __init__(self, window_id: Optional[int] = None):
603605
if not self._window:
604606
raise RuntimeError("Failed to create a new WebUI window.")
605607

608+
# window id but in string format if needed. (currently not in use, legacy from previous wrapper)
606609
self._window_id: str = str(self._window)
607610

611+
# Dispatch function conversion to C bind for binding functions
608612
callback_function_type = _raw.CFUNCTYPE(None, _raw.POINTER(_raw.WebuiEventT))
609613
self._dispatcher_cfunc = callback_function_type(self._make_dispatcher())
610-
# Dict to keep track of binded functions: {bind_id: python_function}
614+
615+
# Dict to keep track of bound functions: {bind_id: python_function}
611616
self._cb_func_list: dict = {}
612617

618+
#
613619
self._file_handler_cfunc = None
614620

615621
# -- dispatcher for function bindings -----------
@@ -625,83 +631,223 @@ def dispatcher(c_event_ptr):
625631

626632
@property
627633
# -- get_window_id --------------------------
628-
def get_window_id(self) -> int: # TODO: document
634+
def get_window_id(self) -> int:
629635
"""Returns the window id."""
630636
return self._window
631637

632638
# -- bind ---------------------------------------
633-
def bind(self, element: str, func: Callable[[Event], None]) -> int: # TODO: document
634-
"""
635-
Bind an HTML element and a JavaScript object with
636-
a backend function. Empty element name means all events.
639+
def bind(self, element: str, func: Callable[[Event], None]) -> int:
640+
"""Bind an HTML element or JavaScript object to a backend function.
641+
642+
This function binds a frontend HTML element or JavaScript object to a Python
643+
callback function, allowing interaction between the frontend and backend.
644+
If an empty string is passed as the element, the function will be bound to all events.
645+
646+
Args:
647+
element (str): The name of the HTML element or JavaScript object to bind.
648+
An empty string binds to all events.
649+
func (Callable[[Event], None]): The Python callback function to execute when the event occurs.
650+
651+
Returns:
652+
int: A unique bind ID that can be used to manage the binding.
653+
654+
Example:
655+
def my_function(event: Event):
656+
print("Event received:", event)
657+
658+
bind_id = my_window.bind("myFunction", my_function)
659+
print(f"Function bound with ID: {bind_id}")
637660
"""
638661
element_c = element.encode('utf-8') if element else None
639-
bind_id = _raw.webui_bind(self._window, element_c, self._dispatcher_cfunc)
662+
bind_id = _raw.webui_bind(c_size_t(self._window), element_c, self._dispatcher_cfunc)
640663
self._cb_func_list[bind_id] = func
641664
return bind_id
642665

643666
# -- get_best_browser ---------------------------
644-
def get_best_browser(self) -> Browser: # TODO: document
645-
"""
646-
Get the recommended web browser to use. If you
647-
are already using one, this function will return the same browser.
667+
def get_best_browser(self) -> Browser:
668+
"""Get the recommended web browser ID to use.
669+
670+
This function retrieves the recommended web browser ID for the current window.
671+
If a browser is already in use, it returns the same browser ID.
672+
673+
Returns:
674+
Browser: The ID of the recommended web browser.
675+
676+
Example:
677+
browser_id = my_window.get_best_browser()
678+
print(f"Recommended browser ID: {browser_id}")
648679
"""
649-
return Browser(int(_raw.webui_get_best_browser(self._window)))
680+
return Browser(int(_raw.webui_get_best_browser(c_size_t(self._window))))
650681

651682
# -- show ---------------------------------------
652-
def show(self, content: str) -> bool: # TODO: document
653-
"""
654-
Show or refresh the window with the specified content
655-
(HTML, URL, or local file).
683+
def show(self, content: str) -> bool:
684+
"""Show a window using embedded HTML, a file, or a URL.
685+
686+
This function displays a window with the provided content. The content can be
687+
raw HTML, a local file path, or a URL. If the window is already open, it will be
688+
refreshed. In multi-client mode, all windows will be refreshed.
689+
690+
Args:
691+
content (str): The HTML content, a file path, or a URL to load in the window.
692+
693+
Returns:
694+
bool: True if the window was successfully shown, False otherwise.
695+
696+
Example:
697+
success = my_window.show("<html>...</html>")
698+
success = my_window.show("index.html")
699+
success = my_window.show("http://example.com")
656700
"""
657-
# We pass UTF-8 strings to the C function
658-
return bool(_raw.webui_show(self._window, content.encode("utf-8")))
701+
return bool(_raw.webui_show(c_size_t(self._window), c_char_p(content.encode("utf-8"))))
659702

660703
# -- show_browser -------------------------------
661-
def show_browser(self, content: str, browser: Browser) -> bool: # TODO: document
662-
"""
663-
Show or refresh the window using a specific browser (by enum).
704+
def show_browser(self, content: str, browser: Browser) -> bool:
705+
"""Show a window using embedded HTML or a file in a specific web browser.
706+
707+
This function displays a window with the provided content using a specified web browser.
708+
The content can be raw HTML, a local file path, or a URL. If the window is already open,
709+
it will be refreshed. In multi-client mode, all windows will be refreshed.
710+
711+
Args:
712+
content (str): The HTML content, a file path, or a URL to load in the window.
713+
browser (Browser): The web browser to be used.
714+
715+
Returns:
716+
bool: True if the window was successfully shown, False otherwise.
717+
718+
Example:
719+
success = my_window.show_browser("<html>...</html>", Browser.Chrome)
720+
success = my_window.show_browser("index.html", Browser.Firefox)
664721
"""
665-
return bool(_raw.webui_show_browser(self._window, content.encode("utf-8"), c_size_t(browser.value)))
722+
return bool(_raw.webui_show_browser(c_size_t(self._window), c_char_p(content.encode("utf-8")), c_size_t(browser.value)))
666723

667724
# -- start_server -------------------------------
668-
def start_server(self, content: str) -> str: # TODO: document
669-
return _raw.webui_start_server(self._window, content.encode("utf-8")).decode("utf-8")
725+
def start_server(self, content: str) -> str:
726+
"""Start a web server and return the server URL.
727+
728+
This function starts a web server using the provided content but does not show
729+
a window. The content can be raw HTML, a local file path, or a URL.
730+
731+
Args:
732+
content (str): The HTML content, a file path, or a URL to serve.
733+
734+
Returns:
735+
str: The URL of the web server.
736+
737+
Example:
738+
url = my_window.start_server("/full/root/path")
739+
print(f"Server started at: {url}")
740+
"""
741+
return str(_raw.webui_start_server(c_size_t(self._window), c_char_p(content.encode("utf-8"))).decode("utf-8"))
670742

671743
# -- show_wv ------------------------------------
672-
def show_wv(self, content: str) -> bool: # TODO: document
673-
return bool(_raw.webui_show_wv(self._window, content.encode("utf-8")))
744+
def show_wv(self, content: str) -> bool:
745+
"""Show a WebView window using embedded HTML, a file, or a URL.
746+
747+
This function displays a WebView window with the provided content. The content
748+
can be raw HTML, a local file path, or a URL. If the window is already open, it
749+
will be refreshed.
750+
751+
Note:
752+
On Win32 systems, `WebView2Loader.dll` is required for this function to work.
753+
754+
Args:
755+
content (str): The HTML content, a file path, or a URL to load in the WebView window.
756+
757+
Returns:
758+
bool: True if the WebView window was successfully shown, False otherwise.
759+
760+
Example:
761+
success = my_window.show_wv("<html>...</html>")
762+
success = my_window.show_wv("index.html")
763+
success = my_window.show_wv("http://example.com")
764+
"""
765+
return bool(_raw.webui_show_wv(c_size_t(self._window), content.encode("utf-8")))
674766

675767
# -- set_kiosk ----------------------------------
676-
def set_kiosk(self, status: bool) -> None: # TODO: document
768+
def set_kiosk(self, status: bool) -> None:
769+
"""Set the window in Kiosk mode (full screen).
770+
771+
This function enables or disables Kiosk mode for the specified window.
772+
Kiosk mode forces the window into full-screen mode without window controls.
773+
774+
Args:
775+
status (bool): True to enable Kiosk mode, False to disable it.
776+
777+
Returns:
778+
None
779+
780+
Example:
781+
my_window.set_kiosk(True) # Enable Kiosk mode
782+
my_window.set_kiosk(False) # Disable Kiosk mode
677783
"""
678-
Set or unset kiosk (fullscreen) mode.
784+
_raw.webui_set_kiosk(c_size_t(self._window), c_bool(status))
785+
786+
# -- set_custom_parameters ----------------------
787+
def set_custom_parameters(self, params: str) -> None:
788+
"""Add user-defined command-line parameters for the web browser.
789+
790+
This function sets custom command-line parameters for the web browser
791+
used to display the window.
792+
793+
Args:
794+
params (str): The command-line parameters to pass to the web browser.
795+
796+
Returns:
797+
None
798+
799+
Example:
800+
my_window.set_custom_parameters("--remote-debugging-port=9222")
679801
"""
680-
_raw.webui_set_kiosk(self._window, c_bool(status))
802+
_raw.webui_set_custom_parameters(c_size_t(self._window), c_char_p(params.encode("utf-8")))
681803

682804
# -- set_high_contrast --------------------------
683-
def set_high_contrast(self, status: bool) -> None: # TODO: document
684-
_raw.webui_set_high_contrast(self._window, c_bool(status))
805+
def set_high_contrast(self, status: bool) -> None:
806+
"""Enable or disable high-contrast mode for the window.
807+
808+
This function enables or disables high-contrast support for the window.
809+
It is useful when building a high-contrast theme using CSS to improve
810+
accessibility.
811+
812+
Args:
813+
status (bool): True to enable high-contrast mode, False to disable it.
814+
815+
Returns:
816+
None
817+
818+
Example:
819+
my_window.set_high_contrast(True) # Enable high-contrast mode
820+
my_window.set_high_contrast(False) # Disable high-contrast mode
821+
"""
822+
_raw.webui_set_high_contrast(c_size_t(self._window), c_bool(status))
685823

686824
# -- close --------------------------------------
687825
def close(self) -> None: # TODO: document
826+
"""Close a specific window.
827+
828+
This function closes the specified window, but the window object still exists
829+
and can be reopened if needed. It does not affect other windows or clients.
830+
831+
Returns:
832+
None
833+
834+
Example:
835+
my_window.close() # Close the current window.
688836
"""
689-
Close this window (all clients).
690-
"""
691-
_raw.webui_close(self._window)
837+
_raw.webui_close(c_size_t(self._window))
692838

693839
# -- destroy ------------------------------------
694840
def destroy(self) -> None: # TODO: document
695841
"""
696842
Close this window and free all memory resources used by it.
697843
"""
698-
_raw.webui_destroy(self._window)
844+
_raw.webui_destroy(c_size_t(self._window))
699845

700846
# -- set_root_folder ----------------------------
701847
def set_root_folder(self, path: str) -> bool: # TODO: document
702-
return bool(_raw.webui_set_root_folder(self._window, path.encode("utf-8")))
848+
return bool(_raw.webui_set_root_folder(c_size_t(self._window), path.encode("utf-8")))
703849

704-
# -- set_file_handler----------------------------
850+
# -- set_file_handler ---------------------------
705851
def set_file_handler(self, handler: Callable[[str], bytes]) -> None: # TODO: document
706852
"""
707853
Set a custom file handler to serve files for this window.
@@ -740,19 +886,21 @@ def _internal_file_handler(filename_ptr: c_char_p, length_ptr: POINTER(c_int)) -
740886
# Keep a reference so it doesnt get garbage collected
741887
self._file_handler_cfunc = filehandler_callback(_internal_file_handler)
742888

743-
_raw.webui_set_file_handler(self._window, self._file_handler_cfunc)
889+
_raw.webui_set_file_handler(c_size_t(self._window), self._file_handler_cfunc)
744890

745891
# -- set_file_handler_window --------------------
746892
# TODO: set_file_handler_window
893+
def set_file_handler_window(self):
894+
pass
747895

748896
# -- is_shown -----------------------------------
749897
def is_shown(self) -> bool: # TODO: document
750898
"""Return True if the window is currently shown."""
751-
return bool(_raw.webui_is_shown(self._window))
899+
return bool(_raw.webui_is_shown(c_size_t(self._window)))
752900

753901
# -- set_icon -----------------------------------
754902
def set_icon(self, icon: str, icon_type: str) -> None: # TODO: document
755-
_raw.webui_set_icon(self._window, icon.encode("utf-8"), icon_type.encode("utf-8"))
903+
_raw.webui_set_icon(c_size_t(self._window), icon.encode("utf-8"), icon_type.encode("utf-8"))
756904

757905
# -- send_raw -----------------------------------
758906
def send_raw(self, function: str, raw: Optional[c_void_p], size: int) -> None: # TODO: document
@@ -773,6 +921,10 @@ def set_hide(self, status: bool) -> bool: # TODO: document
773921
def set_size(self, width: int, height: int) -> None: # TODO: document
774922
_raw.webui_set_size(c_size_t(self._window), c_uint(width), c_uint(height))
775923

924+
# -- set_minimum_size ---------------------------
925+
def set_minimum_size(self, width: int, height: int) -> None: # TODO: document
926+
_raw.webui_set_minimum_size(self._window, c_uint(width), c_uint(height))
927+
776928
# -- set_position -------------------------------
777929
def set_position(self, x: int, y: int) -> None: # TODO: document
778930
_raw.webui_set_position(c_size_t(self._window), c_uint(x), c_uint(y))
@@ -829,7 +981,7 @@ def run(self, script: str) -> None: # TODO: document
829981
"""
830982
Run JavaScript in the window without waiting for a return.
831983
"""
832-
_raw.webui_run(self._window, script.encode("utf-8"))
984+
_raw.webui_run(c_size_t(self._window), script.encode("utf-8"))
833985

834986
# -- script -------------------------------------
835987
def script(self, script: str, timeout: int = 0, buffer_size: int = 4096) -> JavaScript: # TODO: document
@@ -848,7 +1000,7 @@ def script(self, script: str, timeout: int = 0, buffer_size: int = 4096) -> Java
8481000

8491001
# Call the raw C function
8501002
success = _raw.webui_script(
851-
self._window,
1003+
c_size_t(self._window),
8521004
script.encode('utf-8'), # Convert Python str -> bytes
8531005
timeout,
8541006
buffer,

0 commit comments

Comments
 (0)