Skip to content

Commit b4ba4b0

Browse files
committed
fixed all examples to work with new new library. fixed handling empty charptr returns as strings. Need to finish double checking any other functions that return str and docstring all the wrapper functions
1 parent 483d9f8 commit b4ba4b0

File tree

10 files changed

+85
-59
lines changed

10 files changed

+85
-59
lines changed

PyPI/Package/src/webui/webui.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def close_client(self):
7676
_raw.webui_close_client(byref(self._c_event()))
7777

7878
# -- send_raw_client ----------------------------
79-
def send_raw_client(self, function: str, raw: Optional[c_void_p], size: int) -> None:
79+
def send_raw_client(self, function: str, raw: Optional[int], size: int) -> None:
8080
if raw is None:
8181
raise ValueError("Invalid Pointer: Cannot send a null pointer.")
8282
_raw.webui_send_raw_client(
@@ -146,11 +146,17 @@ def get_string_at(self, index: int) -> str:
146146
Returns:
147147
str: The UTF-8 decoded string corresponding to the specified index.
148148
"""
149-
return str(_raw.webui_get_string_at(byref(self._c_event()), c_size_t(index)).decode('utf-8'))
149+
char_ptr = _raw.webui_get_string_at(byref(self._c_event()), c_size_t(index))
150+
if char_ptr is None:
151+
return ""
152+
return str(char_ptr.decode("utf-8"))
150153

151154
# -- get_string ---------------------------------
152155
def get_string(self) -> str:
153-
return str(_raw.webui_get_string(byref(self._c_event())).decode('utf-8'))
156+
char_ptr = _raw.webui_get_string(byref(self._c_event()))
157+
if char_ptr is None:
158+
return ""
159+
return str(char_ptr.decode("utf-8"))
154160

155161
# -- get_bool_at --------------------------------
156162
def get_bool_at(self, index: int) -> bool:
@@ -170,7 +176,7 @@ def get_size(self) -> int:
170176

171177
# -- return_int ---------------------------------
172178
def return_int(self, n: int) -> None:
173-
_raw.webui_return_int(byref(self._c_event()), c_size_t(n))
179+
_raw.webui_return_int(byref(self._c_event()), c_longlong(n))
174180

175181
# -- return_float -------------------------------
176182
def return_float(self, f: float) -> None:

PyPI/test_package.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,19 @@
6262
</body></html>
6363
"""
6464

65-
def all_events(e : webui.event):
65+
def all_events(e : webui.Event):
6666
print('Function: all_events()')
6767
print('Element: ' + e.element)
6868
print('Type: ' + str(e.event_type))
6969
print(' ')
7070

71-
def python_to_js(e : webui.event):
71+
def python_to_js(e : webui.Event):
7272
print('Function: python_to_js()')
7373
print('Element: ' + e.element)
7474
print('Type: ' + str(e.event_type))
75-
print('Data: ' + e.window.get_str(e))
75+
print('Data: ' + e.get_string())
7676
print(' ')
77-
# Run JavaScript to get the password
77+
# Run JavaScript to get the value
7878
res = e.window.script("return document.getElementById('MyInput').value;")
7979
# Check for any error
8080
if res.error is True:
@@ -85,28 +85,29 @@ def python_to_js(e : webui.event):
8585
# e.window.run("alert('Fast!')")
8686
print(' ')
8787

88-
def js_to_python(e : webui.event):
88+
def js_to_python(e : webui.Event):
8989
print('Function: js_to_python()')
9090
print('Element: ' + e.element)
9191
print('Type: ' + str(e.event_type))
92-
print('Data: ' + e.window.get_str(e, 0))
92+
print('Data: ' + e.get_string_at(0))
9393
print(' ')
94-
v = int(e.window.get_str(e, 0))
94+
v = e.get_int()
9595
v = v * 2
96-
return v
96+
e.return_int(v)
9797

98-
def exit(e : webui.event):
98+
99+
def exit(e : webui.Event):
99100
print('Function: exit()')
100101
print('Element: ' + e.element)
101102
print('Type: ' + str(e.event_type))
102-
print('Data: ' + e.window.get_str(e, 0))
103+
print('Data: ' + e.get_string_at(0))
103104
print(' ')
104105
webui.exit()
105106

106107
def main():
107108

108109
# Create a window object
109-
MyWindow = webui.window()
110+
MyWindow = webui.Window()
110111

111112
# Bind am HTML element ID with a python function
112113
MyWindow.bind('', all_events)
@@ -115,7 +116,7 @@ def main():
115116
MyWindow.bind('Exit', exit)
116117

117118
# Show the window
118-
MyWindow.show(html, webui.browser.any)
119+
MyWindow.show_browser(html, webui.Browser.AnyBrowser)
119120

120121
# Wait until all windows are closed
121122
webui.wait()

examples/andershell3000/andershell3000.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import sys
2+
sys.path.append('../../PyPI/Package/src/webui')
3+
import webui
4+
15
# Install WebUI
26
# pip install --upgrade webui2
3-
4-
from webui import webui
7+
# from webui import webui
58
import subprocess
69
import time
710

@@ -36,20 +39,20 @@ def close(self):
3639
self.shell.terminate()
3740
self.shell.wait()
3841

39-
def run_command(e : webui.event):
40-
cmd = e.window.get_str(e, 0)
42+
def run_command(e : webui.Event):
43+
cmd = e.get_string_at(0)
4144
if cmd == "exit":
4245
webui.exit()
4346
try:
4447
command_result = executor.execute(cmd)
45-
return command_result
48+
e.return_string(command_result)
4649
except FileNotFoundError:
47-
return "Command not found '{cmd}'"
50+
print(f"Command not found '{cmd}'")
4851

4952
executor = CommandExecutor()
50-
MyWindow = webui.window()
53+
MyWindow = webui.Window()
5154
MyWindow.bind("Run", run_command)
5255
MyWindow.set_root_folder("ui/")
53-
MyWindow.show('index.html')
56+
MyWindow.show_browser('index.html', webui.Browser.Chrome)
5457
webui.wait()
5558
executor.close()

examples/hello_world.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import sys
2+
sys.path.append('../PyPI/Package/src/webui')
3+
import webui
4+
15
# Install WebUI
26
# pip install --upgrade webui2
3-
4-
from webui import webui
7+
# from webui import webui
58

69
# CSS
710
css = """
@@ -72,7 +75,7 @@
7275
"""
7376

7477
# This function get called every time the user click on "MyButton1"
75-
def check_the_password(e : webui.event):
78+
def check_the_password(e : webui.Event):
7679

7780
# Run JavaScript to get the password
7881
res = e.window.script("return document.getElementById(\"MyInput\").value;")
@@ -90,20 +93,20 @@ def check_the_password(e : webui.event):
9093
print("Wrong password: " + res.data)
9194
e.window.script(" document.getElementById('err').innerHTML = '[ ! ] Wrong password'; ")
9295

93-
def close_the_application(e : webui.event):
96+
def close_the_application(e : webui.Event):
9497
webui.exit()
9598

9699
def main():
97100

98101
# Create a window object
99-
MyWindow = webui.window()
102+
my_window = webui.Window()
100103

101104
# Bind am HTML element ID with a python function
102-
MyWindow.bind('CheckPassword', check_the_password)
103-
MyWindow.bind('Exit', close_the_application)
105+
my_window.bind('CheckPassword', check_the_password)
106+
my_window.bind('Exit', close_the_application)
104107

105108
# Show the window
106-
MyWindow.show(login_html)
109+
my_window.show_browser(login_html, webui.Browser.Chrome)
107110

108111
# Wait until all windows are closed
109112
webui.wait()

examples/minimal.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import sys
2+
sys.path.append('../PyPI/Package/src/webui')
3+
import webui
4+
15
# Install WebUI
26
# pip install --upgrade webui2
37

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

6-
MyWindow = webui.window()
10+
MyWindow = webui.Window()
711
MyWindow.show('<html><head><script src=\"webui.js\"></script></head> Hello World ! </html>')
812
webui.wait()
913
print('Thank you.')

examples/public_ui.py

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

48

@@ -16,13 +20,13 @@ def get_local_ip():
1620
s.close()
1721
return local_ip
1822

19-
def all_events(e : webui.event):
20-
if e.event_type == webui.eventType.CONNECTED:
23+
def all_events(e : webui.Event):
24+
if e.event_type == webui.EventType.CONNECTED:
2125
print('Connected.')
22-
if e.event_type == webui.eventType.DISCONNECTED:
26+
if e.event_type == webui.EventType.DISCONNECTED:
2327
print('Disconnected.')
2428

25-
def exit(e : webui.event):
29+
def exit(e : webui.Event):
2630
webui.exit()
2731

2832
def main():
@@ -36,23 +40,23 @@ def main():
3640
"""
3741

3842
# New window
39-
MyWindow = webui.window()
43+
my_window = webui.Window()
4044

4145
# Make the window URL accessible from public networks
42-
MyWindow.set_public(True)
46+
my_window.set_public(True)
4347

4448
# Wait forever (Otherwise WebUI will timeout after 30s)
4549
webui.set_timeout(0)
4650

4751
# Bind
48-
MyWindow.bind('', all_events)
49-
MyWindow.bind('Exit', exit)
52+
my_window.bind('', all_events)
53+
my_window.bind('Exit', exit)
5054

5155
# Start the window without any browser
52-
MyWindow.show(html, webui.browser.NoBrowser)
56+
my_window.show_browser(html, webui.Browser.NoBrowser)
5357

5458
# Get URL of the window
55-
url = MyWindow.get_url()
59+
url = my_window.get_url()
5660

5761
# Get local IP
5862
local_ip = get_local_ip()
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
1-
2-
1+
import sys
2+
sys.path.append('../../PyPI/Package/src/webui')
3+
import webui
34

45
# Install WebUI
56
# pip install --upgrade webui2
7+
# from webui import webui
68

7-
from webui import webui
8-
9-
def switch_to_second_page(e : webui.event):
9+
def switch_to_second_page(e : webui.Event):
1010
# This function get called when the user
1111
# click on "SwitchToSecondPage" button
1212
e.window.show("second.html")
1313

14-
def close_the_application(e : webui.event):
14+
def close_the_application(e : webui.Event):
1515
webui.exit()
1616

1717
def main():
1818

1919
# Create a window object
20-
MyWindow = webui.window()
20+
my_window = webui.Window()
2121

2222
# Bind am HTML element ID with a python function
23-
MyWindow.bind('SwitchToSecondPage', switch_to_second_page)
24-
MyWindow.bind('Exit', close_the_application)
23+
my_window.bind('SwitchToSecondPage', switch_to_second_page)
24+
my_window.bind('Exit', close_the_application)
2525

2626
# Note
2727
# Add this script to all your .html files:
2828
# <script src="webui.js"></script>
2929

3030
# Show a window using the local file
31-
MyWindow.show("serve_folder.html", webui.browser.chrome)
31+
my_window.show_browser("serve_folder.html", webui.Browser.Chrome)
3232

3333
# Wait until all windows are closed
3434
webui.wait()
35+
webui.clean()
3536

3637
print('Thank you.')
3738

examples/text-editor/main.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
from webui import webui
1+
import sys
2+
sys.path.append('../../PyPI/Package/src/webui')
3+
import webui
24

3-
def close(e: webui.event):
5+
# from webui import webui
6+
7+
def close(e: webui.Event):
48
print("Exit.")
59
# Close all opened windows
610
webui.exit()
711

812
def main():
913
# Create new window
10-
window = webui.window()
14+
window = webui.Window()
1115

1216
# Bind HTML element IDs with Python functions
1317
window.bind("__close-btn", close)
@@ -16,7 +20,7 @@ def main():
1620
window.set_root_folder("ui/")
1721

1822
# Show the window
19-
window.show("index.html")
23+
window.show_browser("index.html", webui.Browser.Chrome)
2024

2125
# Wait until all windows get closed
2226
webui.wait()

0 commit comments

Comments
 (0)