Skip to content

Commit 10e2c8f

Browse files
committed
New APIs
webui_new_window_id webui_get_new_window_id webui_get_best_browser webui_start_server webui_show_wv webui_set_custom_parameters webui_set_high_contrast webui_is_high_contrast webui_browser_exist webui_set_minimum_size webui_set_proxy webui_open_url webui_navigate webui_get_free_port webui_get_mime_type webui_encode webui_decode
1 parent 150d113 commit 10e2c8f

File tree

1 file changed

+196
-1
lines changed

1 file changed

+196
-1
lines changed

PyPI/Package/src/webui/webui.py

Lines changed: 196 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Python WebUI v2.5.0
32
#
43
# http://webui.me
@@ -407,6 +406,117 @@ def get_child_process_id(self) -> int:
407406
return int(lib.webui_get_child_process_id(self.window))
408407

409408

409+
"""Create a new webui window object using a specified window number.
410+
@param window_number The window number (should be > 0, and < WEBUI_MAX_IDS)
411+
@return Returns the same window number if success."""
412+
def new_window_id(self, window_number: int) -> int:
413+
global lib
414+
if lib is None:
415+
_err_library_not_found('new_window_id')
416+
return 0
417+
return int(lib.webui_new_window_id(ctypes.c_size_t(window_number)))
418+
419+
420+
"""Get a free window number that can be used with `webui_new_window_id()`.
421+
@return Returns the first available free window number. Starting from 1."""
422+
def get_new_window_id(self) -> int:
423+
global lib
424+
if lib is None:
425+
_err_library_not_found('get_new_window_id')
426+
return 0
427+
return int(lib.webui_get_new_window_id())
428+
429+
430+
"""Get the recommended web browser ID to use. If you are already using one,
431+
this function will return the same ID.
432+
@return Returns a web browser ID."""
433+
def get_best_browser(self) -> int:
434+
global lib
435+
if lib is None:
436+
_err_library_not_found('get_best_browser')
437+
return 0
438+
return int(lib.webui_get_best_browser(self.window))
439+
440+
441+
"""Same as `webui_show()`. But start only the web server and return the URL.
442+
No window will be shown.
443+
@param content The HTML, Or a local file
444+
@return Returns the url of this window server."""
445+
def start_server(self, content: str) -> str:
446+
global lib
447+
if lib is None:
448+
_err_library_not_found('start_server')
449+
return ""
450+
c_res = lib.webui_start_server
451+
c_res.restype = ctypes.c_char_p
452+
url = c_res(self.window, content.encode('utf-8'))
453+
return url.decode('utf-8') if url else ""
454+
455+
456+
"""Show a WebView window using embedded HTML, or a file. If the window is already
457+
open, it will be refreshed. Note: Win32 need `WebView2Loader.dll`.
458+
@param content The HTML, URL, Or a local file
459+
@return Returns True if showing the WebView window is successed."""
460+
def show_wv(self, content: str) -> bool:
461+
global lib
462+
if lib is None:
463+
_err_library_not_found('show_wv')
464+
return False
465+
return bool(lib.webui_show_wv(self.window, content.encode('utf-8')))
466+
467+
468+
"""Add a user-defined web browser's CLI parameters.
469+
@param params Command line parameters"""
470+
def set_custom_parameters(self, params: str):
471+
global lib
472+
if lib is None:
473+
_err_library_not_found('set_custom_parameters')
474+
return
475+
lib.webui_set_custom_parameters(self.window, params.encode('utf-8'))
476+
477+
478+
"""Set the window with high-contrast support. Useful when you want to
479+
build a better high-contrast theme with CSS.
480+
@param status True or False"""
481+
def set_high_contrast(self, status: bool):
482+
global lib
483+
if lib is None:
484+
_err_library_not_found('set_high_contrast')
485+
return
486+
lib.webui_set_high_contrast(self.window, ctypes.c_bool(status))
487+
488+
489+
"""Set the window minimum size.
490+
@param width The window width
491+
@param height The window height"""
492+
def set_minimum_size(self, width: int, height: int):
493+
global lib
494+
if lib is None:
495+
_err_library_not_found('set_minimum_size')
496+
return
497+
lib.webui_set_minimum_size(self.window, ctypes.c_uint(width), ctypes.c_uint(height))
498+
499+
500+
"""Set the web browser proxy server to use. Need to be called before `webui_show()`.
501+
@param proxy_server The web browser proxy_server"""
502+
def set_proxy(self, proxy_server: str):
503+
global lib
504+
if lib is None:
505+
_err_library_not_found('set_proxy')
506+
return
507+
lib.webui_set_proxy(self.window, proxy_server.encode('utf-8'))
508+
509+
510+
"""Navigate to a specific URL. All clients.
511+
@param url Full HTTP URL"""
512+
def navigate(self, url: str):
513+
global lib
514+
if lib is None:
515+
_err_library_not_found('navigate')
516+
return
517+
lib.webui_navigate(self.window, url.encode('utf-8'))
518+
519+
410520
def _get_current_folder() -> str:
411521
return os.path.dirname(os.path.abspath(__file__))
412522

@@ -588,3 +698,88 @@ def _err_library_not_found(f):
588698
#
589699
def _err_window_is_none(f):
590700
print('WebUI ' + f + '(): window is None.')
701+
702+
703+
"""Get OS high contrast preference.
704+
@return Returns True if OS is using high contrast theme"""
705+
def is_high_contrast() -> bool:
706+
global lib
707+
if lib is None:
708+
_err_library_not_found('is_high_contrast')
709+
return False
710+
return bool(lib.webui_is_high_contrast())
711+
712+
713+
"""Check if a web browser is installed.
714+
@return Returns True if the specified browser is available"""
715+
def browser_exist(browser: int) -> bool:
716+
global lib
717+
if lib is None:
718+
_err_library_not_found('browser_exist')
719+
return False
720+
return bool(lib.webui_browser_exist(ctypes.c_size_t(browser)))
721+
722+
723+
"""Open an URL in the native default web browser.
724+
@param url The URL to open"""
725+
def open_url(url: str):
726+
global lib
727+
if lib is None:
728+
_err_library_not_found('open_url')
729+
return
730+
lib.webui_open_url(url.encode('utf-8'))
731+
732+
733+
"""Get an available usable free network port.
734+
@return Returns a free port"""
735+
def get_free_port() -> int:
736+
global lib
737+
if lib is None:
738+
_err_library_not_found('get_free_port')
739+
return 0
740+
return int(lib.webui_get_free_port())
741+
742+
743+
"""Get the HTTP mime type of a file.
744+
@return Returns the HTTP mime string"""
745+
def get_mime_type(file: str) -> str:
746+
global lib
747+
if lib is None:
748+
_err_library_not_found('get_mime_type')
749+
return ""
750+
c_res = lib.webui_get_mime_type
751+
c_res.restype = ctypes.c_char_p
752+
mime = c_res(file.encode('utf-8'))
753+
return mime.decode('utf-8') if mime else ""
754+
755+
756+
"""Encode text to Base64. The returned buffer need to be freed.
757+
@param str The string to encode (Should be null terminated)
758+
@return Returns the base64 encoded string"""
759+
def encode(text: str) -> str:
760+
global lib
761+
if lib is None:
762+
_err_library_not_found('encode')
763+
return ""
764+
c_res = lib.webui_encode
765+
c_res.restype = ctypes.c_char_p
766+
encoded = c_res(text.encode('utf-8'))
767+
result = encoded.decode('utf-8') if encoded else ""
768+
free(encoded)
769+
return result
770+
771+
772+
"""Decode a Base64 encoded text. The returned buffer need to be freed.
773+
@param str The string to decode (Should be null terminated)
774+
@return Returns the base64 decoded string"""
775+
def decode(text: str) -> str:
776+
global lib
777+
if lib is None:
778+
_err_library_not_found('decode')
779+
return ""
780+
c_res = lib.webui_decode
781+
c_res.restype = ctypes.c_char_p
782+
decoded = c_res(text.encode('utf-8'))
783+
result = decoded.decode('utf-8') if decoded else ""
784+
free(decoded)
785+
return result

0 commit comments

Comments
 (0)