|
1 | 1 | # webui.py |
2 | 2 | from __future__ import annotations |
| 3 | + |
| 4 | +import warnings |
3 | 5 | from typing import Callable, Optional |
4 | 6 | from ctypes import * |
5 | 7 |
|
@@ -712,7 +714,10 @@ def show_browser(self, content: str, browser: Browser) -> bool: |
712 | 714 | success = my_window.show_browser("<html>...</html>", Browser.Chrome) |
713 | 715 | success = my_window.show_browser("index.html", Browser.Firefox) |
714 | 716 | """ |
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 |
716 | 721 |
|
717 | 722 | # -- start_server ------------------------------- |
718 | 723 | def start_server(self, content: str) -> str: |
@@ -864,113 +869,113 @@ def set_root_folder(self, path: str) -> bool: |
864 | 869 | """ |
865 | 870 | return bool(_raw.webui_set_root_folder(c_size_t(self._window), path.encode("utf-8"))) |
866 | 871 |
|
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) |
974 | 979 |
|
975 | 980 | # -- is_shown ----------------------------------- |
976 | 981 | def is_shown(self) -> bool: |
|
0 commit comments