Skip to content

Commit 2e59dd2

Browse files
committed
rename Server proto to ServerType + test_sample
1 parent 0edc111 commit 2e59dd2

File tree

6 files changed

+53
-30
lines changed

6 files changed

+53
-30
lines changed

src/idom/sample.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
from __future__ import annotations
2+
13
import webbrowser
4+
from typing import Any
5+
6+
from idom.server.proto import ServerType
27

38
from . import html
49
from .core.component import component
@@ -22,17 +27,34 @@ def App() -> VdomDict:
2227
)
2328

2429

25-
def run_sample_app(open_browser: bool = False) -> None:
26-
"""Run a sample application."""
27-
host = "127.0.0.1"
28-
port = find_available_port(host)
30+
def run_sample_app(
31+
host: str = "127.0.0.1",
32+
port: int | None = None,
33+
open_browser: bool = False,
34+
run_in_thread: bool | None = None,
35+
) -> ServerType[Any]:
36+
"""Run a sample application.
37+
38+
Args:
39+
host: host where the server should run
40+
port: the port on the host to serve from
41+
open_browser: whether to open a browser window after starting the server
42+
"""
43+
port = port or find_available_port(host)
2944
server_type = find_builtin_server_type("PerClientStateServer")
3045
server = server_type(App)
3146

32-
if not open_browser:
47+
run_in_thread = open_browser or run_in_thread
48+
49+
if not run_in_thread: # pragma: no cover
3350
server.run(host=host, port=port)
34-
else:
35-
thread = server.run_in_thread(host=host, port=port)
36-
server.wait_until_started(5)
51+
return server
52+
53+
thread = server.run_in_thread(host=host, port=port)
54+
server.wait_until_started(5)
55+
56+
if open_browser: # pragma: no cover
3757
webbrowser.open(f"http://{host}:{port}")
3858
thread.join()
59+
60+
return server

src/idom/server/prefab.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from idom.core.proto import ComponentConstructor
1010
from idom.widgets import MountFunc, MultiViewMount, hotswap, multiview
1111

12-
from .proto import Server, ServerFactory
12+
from .proto import ServerFactory, ServerType
1313
from .utils import find_available_port, find_builtin_server_type
1414

1515

@@ -28,7 +28,7 @@ def run(
2828
run_kwargs: Optional[Dict[str, Any]] = None,
2929
app: Optional[Any] = None,
3030
daemon: bool = False,
31-
) -> Server[_App]:
31+
) -> ServerType[_App]:
3232
"""A utility for quickly running a render server with minimal boilerplate
3333
3434
Parameters:
@@ -77,7 +77,7 @@ def multiview_server(
7777
server_config: Optional[_Config] = None,
7878
run_kwargs: Optional[Dict[str, Any]] = None,
7979
app: Optional[Any] = None,
80-
) -> Tuple[MultiViewMount, Server[_App]]:
80+
) -> Tuple[MultiViewMount, ServerType[_App]]:
8181
"""Set up a server where views can be dynamically added.
8282
8383
In other words this allows the user to work with IDOM in an imperative manner. Under
@@ -120,7 +120,7 @@ def hotswap_server(
120120
run_kwargs: Optional[Dict[str, Any]] = None,
121121
app: Optional[Any] = None,
122122
sync_views: bool = False,
123-
) -> Tuple[MountFunc, Server[_App]]:
123+
) -> Tuple[MountFunc, ServerType[_App]]:
124124
"""Set up a server where views can be dynamically swapped out.
125125
126126
In other words this allows the user to work with IDOM in an imperative manner. Under

src/idom/server/proto.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ def __call__(
2020
constructor: ComponentConstructor,
2121
config: Optional[_Config] = None,
2222
app: Optional[_App] = None,
23-
) -> Server[_App]:
23+
) -> ServerType[_App]:
2424
...
2525

2626

27-
class Server(Protocol[_App]):
27+
class ServerType(Protocol[_App]):
2828
"""A thin wrapper around a web server that provides a common operational interface"""
2929

3030
app: _App

src/idom/server/utils.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,6 @@ def target() -> None:
4343
return wrapper
4444

4545

46-
def threaded_no_event_loop(function: Callable[_FuncParams, None]) -> Callable[_FuncParams, Thread]: # type: ignore
47-
@wraps(function)
48-
def wrapper(*args: Any, **kwargs: Any) -> Thread:
49-
def target() -> None:
50-
function(*args, **kwargs)
51-
52-
thread = Thread(target=target, daemon=True)
53-
thread.start()
54-
55-
return thread
56-
57-
return wrapper
58-
59-
6046
def wait_on_event(description: str, event: Event, timeout: Optional[float]) -> None:
6147
if not event.wait(timeout):
6248
raise TimeoutError(f"Did not {description} within {timeout} seconds")

src/idom/testing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from idom.core.events import EventHandler, to_event_handler_function
3232
from idom.core.hooks import LifeCycleHook, current_hook
3333
from idom.server.prefab import hotswap_server
34-
from idom.server.proto import Server, ServerFactory
34+
from idom.server.proto import ServerFactory, ServerType
3535
from idom.server.utils import find_available_port
3636

3737

@@ -60,7 +60,7 @@ def create_simple_selenium_web_driver(
6060

6161
_Self = TypeVar("_Self", bound="ServerMountPoint[Any, Any]")
6262
_Mount = TypeVar("_Mount")
63-
_Server = TypeVar("_Server", bound=Server[Any])
63+
_Server = TypeVar("_Server", bound=ServerType[Any])
6464
_App = TypeVar("_App")
6565
_Config = TypeVar("_Config")
6666

tests/test_sample.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from idom.sample import run_sample_app
2+
from idom.server.utils import find_available_port
3+
4+
5+
def test_sample_app(driver):
6+
host = "127.0.0.1"
7+
port = find_available_port(host, allow_reuse_waiting_ports=False)
8+
9+
run_sample_app(host=host, port=port, run_in_thread=True)
10+
11+
driver.get(f"http://{host}:{port}")
12+
13+
h1 = driver.find_element("tag name", "h1")
14+
15+
assert h1.get_attribute("innerHTML") == "Sample Application"

0 commit comments

Comments
 (0)