-
-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathmain.py
79 lines (57 loc) · 1.93 KB
/
main.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
78
79
import os
import sys
from pathlib import Path
from pathlib import Path
from sanic import Sanic
from sanic import response
import idom
from idom.widgets.utils import multiview
from idom.client.manage import APP_DIR
from idom.server.sanic import PerClientStateServer
here = Path(__file__).parent
app = Sanic(__name__)
app.static("/docs", str(here / "build"))
app.static("/favicon.ico", str(APP_DIR / "favicon.ico"))
@app.route("/")
async def forward_to_index(request):
return response.redirect("/docs/index.html")
mount, element = multiview()
examples_dir = here / "source" / "examples"
sys.path.insert(0, str(examples_dir))
original_run = idom.run
try:
for file in examples_dir.iterdir():
if not file.is_file() or not file.suffix == ".py" or file.stem.startswith("_"):
continue
# Modify the run function so when we exec the file
# instead of running a server we mount the view.
idom.run = mount[file.stem]
with file.open() as f:
try:
exec(
f.read(),
{
"__file__": str(file),
"__name__": f"widgets.{file.stem}",
},
)
except Exception as error:
raise RuntimeError(f"Failed to execute {file}") from error
finally:
idom.run = original_run
server = PerClientStateServer(element, {"redirect_root_to_index": False}).register(app)
def prod():
app.run(
host="0.0.0.0",
port=int(os.environ.get("PORT", 5000)),
workers=int(os.environ.get("WEB_CONCURRENCY", 1)),
debug={"true": True, "false": False}[os.environ.get("DEBUG", "False").lower()],
)
def local(path=""):
import webbrowser
thread = server.daemon("127.0.0.1", 5000)
path = f"docs/{path}" if path else ""
webbrowser.open(f"http://127.0.0.1:5000/{path}")
thread.join()
if __name__ == "__main__":
prod()