-
-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathsample.py
60 lines (47 loc) · 1.58 KB
/
sample.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
from __future__ import annotations
import webbrowser
from typing import Any
from idom.server.types import ServerType
from . import html
from .core.component import component
from .core.types import VdomDict
from .server.utils import find_available_port, find_builtin_server_type
@component
def App() -> VdomDict:
return html.div(
{"style": {"padding": "15px"}},
html.h1("Sample Application"),
html.p(
"This is a basic application made with IDOM. Click ",
html.a(
{"href": "https://pypi.org/project/idom/", "target": "_blank"},
"here",
),
" to learn more.",
),
)
def run_sample_app(
host: str = "127.0.0.1",
port: int | None = None,
open_browser: bool = False,
run_in_thread: bool | None = None,
) -> ServerType[Any]:
"""Run a sample application.
Args:
host: host where the server should run
port: the port on the host to serve from
open_browser: whether to open a browser window after starting the server
"""
port = port or find_available_port(host)
server_type = find_builtin_server_type("PerClientStateServer")
server = server_type(App)
run_in_thread = open_browser or run_in_thread
if not run_in_thread: # pragma: no cover
server.run(host=host, port=port)
return server
thread = server.run_in_thread(host=host, port=port)
server.wait_until_started(5)
if open_browser: # pragma: no cover
webbrowser.open(f"http://{host}:{port}")
thread.join()
return server