-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathweb_repl_example.py
More file actions
executable file
·44 lines (33 loc) · 1.09 KB
/
web_repl_example.py
File metadata and controls
executable file
·44 lines (33 loc) · 1.09 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
import argparse
import anyio
from pura import WebViewServer, WebRepl
HOST = "localhost"
HTTP_PORT = 8080
class Counter:
def __init__(self):
self.count = 0
async def _run(self):
while True:
self.count += 1
await anyio.sleep(0.5)
def increment_by(self, value):
self.count += value
async def main():
async with anyio.create_task_group() as tg:
counter = Counter()
tg.start_soon(counter._run)
webview_server = WebViewServer()
await tg.start(webview_server.serve, "WebRepl test", HOST, HTTP_PORT)
await webview_server.add_repl(WebRepl(dict(
counter=counter,
some_variable='hello',
)))
print(f"REPL is running at http://{HOST}:{HTTP_PORT}/repl")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='web repl example')
parser.add_argument('--async-backend', default='asyncio', choices=['ayncio', 'trio'])
args = parser.parse_args()
try:
anyio.run(main, backend=args.async_backend)
except KeyboardInterrupt:
print()