-
Notifications
You must be signed in to change notification settings - Fork 12
/
js_interaction.py
77 lines (60 loc) · 2.17 KB
/
js_interaction.py
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
import asyncio
import json
import sys
from pathlib import Path
from pywry import PyWry
class Backend(PyWry):
"""Custom backend for PyWry."""
async def get_results(self) -> dict:
"""Wait for completion of interactive task and return the data.
Returns
-------
dict
The data returned from pywry backend.
"""
while True:
try:
data: dict = self.recv.get(block=False) or {}
if data.get("result", False):
return json.loads(data["result"])
except Exception: # pylint: disable=W0703
pass
await asyncio.sleep(1)
def send_interaction(self, title, params: dict, filepath: Path) -> dict:
self.check_backend()
outgoing = dict(
html=filepath.resolve(),
json_data=params,
title=title,
width=400,
height=400,
)
self.send_outgoing(outgoing)
try:
return self.loop.run_until_complete(self.get_results())
except KeyboardInterrupt:
print("\nKeyboard interrupt detected. Exiting...")
return {}
async def main_loop():
while True:
await asyncio.sleep(1)
if __name__ == "__main__":
try:
handler = Backend()
handler.start()
# We send a list of parameters to the backend. The backend will create
# an HTML form with inputs for each parameter and a submit button.
# When the user submits the form, the data will be sent back
# to the Python process.
args = handler.send_interaction(
title="PyWry Example",
params={"required": ["first_name"], "optional": ["last_name"]},
filepath=Path(__file__).parent / "js_interaction.html",
)
# We can now access the data returned from the form.
result = f"Hello {args.get('first_name', 'World')} {args.get('last_name', '')}"
handler.send_html(html=result, title="PyWry Example")
handler.loop.run_until_complete(main_loop())
except KeyboardInterrupt:
print("Keyboard interrupt detected. Exiting...")
sys.exit(0)