-
-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathprefab.py
151 lines (125 loc) · 4.99 KB
/
prefab.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import logging
from typing import Any, Dict, Optional, Tuple, TypeVar
from idom.core.types import ComponentConstructor
from idom.widgets import MountFunc, MultiViewMount, hotswap, multiview
from .types import ServerFactory, ServerType
from .utils import find_available_port, find_builtin_server_type
logger = logging.getLogger(__name__)
_App = TypeVar("_App")
_Config = TypeVar("_Config")
def run(
component: ComponentConstructor,
server_type: Optional[ServerFactory[_App, _Config]] = None,
host: str = "127.0.0.1",
port: Optional[int] = None,
server_config: Optional[Any] = None,
run_kwargs: Optional[Dict[str, Any]] = None,
app: Optional[Any] = None,
daemon: bool = False,
) -> ServerType[_App]:
"""A utility for quickly running a render server with minimal boilerplate
Parameters:
component:
The root of the view.
server_type:
What server to run. Defaults to a builtin implementation if available.
host:
The host string.
port:
The port number. Defaults to a dynamically discovered available port.
server_config:
Options passed to configure the server.
run_kwargs:
Keyword arguments passed to the :meth:`~idom.server.proto.Server.run`
or :meth:`~idom.server.proto.Server.run_in_thread` methods of the server
depending on whether ``daemon`` is set or not.
app:
Register the server to an existing application and run that.
daemon:
Whether the server should be run in a daemon thread.
Returns:
The server instance. This isn't really useful unless the server is spawned
as a daemon. Otherwise this function blocks until the server has stopped.
"""
if server_type is None:
server_type = find_builtin_server_type("PerClientStateServer")
if port is None: # pragma: no cover
port = find_available_port(host)
server = server_type(component, server_config, app)
logger.info(f"Using {type(server).__name__}")
run_server = server.run if not daemon else server.run_in_thread
run_server(host, port, **(run_kwargs or {}))
server.wait_until_started()
return server
def multiview_server(
server_type: Optional[ServerFactory[_App, _Config]] = None,
host: str = "127.0.0.1",
port: Optional[int] = None,
server_config: Optional[_Config] = None,
run_kwargs: Optional[Dict[str, Any]] = None,
app: Optional[Any] = None,
) -> Tuple[MultiViewMount, ServerType[_App]]:
"""Set up a server where views can be dynamically added.
In other words this allows the user to work with IDOM in an imperative manner. Under
the hood this uses the :func:`idom.widgets.multiview` function to add the views on
the fly.
Parameters:
server: The server type to start up as a daemon
host: The server hostname
port: The server port number
server_config: Value passed to :meth:`~idom.server.proto.ServerFactory`
run_kwargs: Keyword args passed to :meth:`~idom.server.proto.Server.run_in_thread`
app: Optionally provide a prexisting application to register to
Returns:
The server instance and a function for adding views. See
:func:`idom.widgets.multiview` for details.
"""
mount, component = multiview()
server = run(
component,
server_type,
host,
port,
server_config=server_config,
run_kwargs=run_kwargs,
daemon=True,
app=app,
)
return mount, server
def hotswap_server(
server_type: Optional[ServerFactory[_App, _Config]] = None,
host: str = "127.0.0.1",
port: Optional[int] = None,
server_config: Optional[_Config] = None,
run_kwargs: Optional[Dict[str, Any]] = None,
app: Optional[Any] = None,
sync_views: bool = False,
) -> Tuple[MountFunc, ServerType[_App]]:
"""Set up a server where views can be dynamically swapped out.
In other words this allows the user to work with IDOM in an imperative manner. Under
the hood this uses the :func:`idom.widgets.hotswap` function to swap the views on
the fly.
Parameters:
server: The server type to start up as a daemon
host: The server hostname
port: The server port number
server_config: Value passed to :meth:`~idom.server.proto.ServerFactory`
run_kwargs: Keyword args passed to :meth:`~idom.server.proto.Server.run_in_thread`
app: Optionally provide a prexisting application to register to
sync_views: Whether to update all displays with newly mounted components
Returns:
The server instance and a function for swapping views. See
:func:`idom.widgets.hotswap` for details.
"""
mount, component = hotswap(update_on_change=sync_views)
server = run(
component,
server_type,
host,
port,
server_config=server_config,
run_kwargs=run_kwargs,
daemon=True,
app=app,
)
return mount, server