-
-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathapp.py
70 lines (45 loc) · 1.41 KB
/
app.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
import os
from logging import getLogger
from pathlib import Path
from sanic import Sanic, response
from idom import component
from idom.backend.sanic import Options, configure, use_request
from idom.core.types import ComponentConstructor
from .examples import get_normalized_example_name, load_examples
HERE = Path(__file__).parent
IDOM_MODEL_SERVER_URL_PREFIX = "/_idom"
logger = getLogger(__name__)
IDOM_MODEL_SERVER_URL_PREFIX = "/_idom"
def run():
app = make_app()
configure(
app,
Example(),
Options(url_prefix=IDOM_MODEL_SERVER_URL_PREFIX),
)
app.run(
host="0.0.0.0",
port=int(os.environ.get("PORT", 5000)),
workers=int(os.environ.get("WEB_CONCURRENCY", 1)),
debug=bool(int(os.environ.get("DEBUG", "0"))),
)
@component
def Example():
raw_view_id = use_request().get_args().get("view_id")
view_id = get_normalized_example_name(raw_view_id)
return _get_examples()[view_id]()
def _get_examples():
if not _EXAMPLES:
_EXAMPLES.update(load_examples())
return _EXAMPLES
def reload_examples():
_EXAMPLES.clear()
_EXAMPLES.update(load_examples())
_EXAMPLES: dict[str, ComponentConstructor] = {}
def make_app():
app = Sanic("docs_app")
app.static("/docs", str(HERE / "build"))
@app.route("/")
async def forward_to_index(request):
return response.redirect("/docs/index.html")
return app