forked from webui-dev/python-webui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_package.py
More file actions
129 lines (114 loc) · 3.36 KB
/
test_package.py
File metadata and controls
129 lines (114 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# --[ Debugging and Development Test ]-----------
# > pip uninstall webui2
import sys
sys.path.append('./Package/src/webui')
import webui
# --[ Production Test ]--------------------------
# > pip install --upgrade webui2
# from webui import webui
# HTML
html = """<!DOCTYPE html>
<html>
<head>
<title>WebUI 2 - Python Wrapper Test</title>
<script src="webui.js"></script>
<style>
body {
font-family: 'Arial', sans-serif;
color: white;
background: linear-gradient(to right, #507d91, #1c596f, #022737);
text-align: center;
font-size: 18px;
}
button, input {
padding: 10px;
border-radius: 3px;
border: 1px solid #ccc;
box-shadow: 0 3px 5px rgba(0,0,0,0.1);
transition: 0.2s;
}
button {
background: #3498db;
color: #fff;
cursor: pointer;
font-size: 16px;
}
h1 { text-shadow: -7px 10px 7px rgb(67 57 57 / 76%); }
button:hover { background: #c9913d; }
input:focus { outline: none; border-color: #3498db; }
</style>
</head>
<body>
<h2>Python Wrapper Test</h2>
<br>
<input type="text" id="MyInput" OnKeyUp="document.getElementById('err').innerHTML=' ';" autocomplete="off" value=\"2\">
<br>
<h3 id="err" style="color: #dbdd52"> </h3>
<br>
<button id="P2JS">Test Python-To-JS</button>
<button OnClick="MyJS();">Test JS-To-Python</button>
<button id="Exit">Exit</button>
<script>
function MyJS() {
const number = document.getElementById('MyInput').value;
JS2P(number).then((response) => {
document.getElementById('MyInput').value = response;
});
}
</script>
</body></html>
"""
def all_events(e : webui.Event):
print('Function: all_events()')
print('Element: ' + e.element)
print('Type: ' + str(e.event_type))
print(' ')
def python_to_js(e : webui.Event):
print('Function: python_to_js()')
print('Element: ' + e.element)
print('Type: ' + str(e.event_type))
print('Data: ' + e.get_string())
print(' ')
# Run JavaScript to get the value
res = e.window.script("return document.getElementById('MyInput').value;")
# Check for any error
if res.error is True:
print("JavaScript Error: [" + res.data + "]")
else:
print("JavaScript OK: [" + res.data + "]")
# Quick JavaScript (no response waiting)
# e.window.run("alert('Fast!')")
print(' ')
def js_to_python(e : webui.Event):
print('Function: js_to_python()')
print('Element: ' + e.element)
print('Type: ' + str(e.event_type))
print('Data: ' + e.get_string_at(0))
print(' ')
v = e.get_int()
v = v * 2
e.return_int(v)
def exit(e : webui.Event):
print('Function: exit()')
print('Element: ' + e.element)
print('Type: ' + str(e.event_type))
print('Data: ' + e.get_string_at(0))
print(' ')
webui.exit()
def main():
# Create a window object
MyWindow = webui.Window()
# Bind am HTML element ID with a python function
MyWindow.bind('', all_events)
MyWindow.bind('P2JS', python_to_js)
MyWindow.bind('JS2P', js_to_python)
MyWindow.bind('Exit', exit)
browser = MyWindow.get_best_browser()
print(browser)
# Show the window
MyWindow.show(html)
# Wait until all windows are closed
webui.wait()
print('Test Done.')
if __name__ == "__main__":
main()