Skip to content

Commit 694266f

Browse files
committed
misc minor improvements
1 parent 7c8b962 commit 694266f

File tree

6 files changed

+35
-48
lines changed

6 files changed

+35
-48
lines changed

docs/source/guides/getting-started/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ To check that everything is working you can run the sample application:
5656

5757
.. code-block:: bash
5858
59-
python -c "import idom; idom.run_sample_app(open_browser=True)"
59+
python -c "import idom; idom.run(idom.sample.App, open_browser=True)"
6060
6161
This should automatically open up a browser window to a page that looks like this:
6262

src/idom/__init__.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from . import config, html, log, types, web
1+
from . import config, html, log, sample, types, web
22
from .core import hooks
33
from .core.component import component
44
from .core.events import event
@@ -15,7 +15,6 @@
1515
from .core.layout import Layout
1616
from .core.serve import Stop
1717
from .core.vdom import vdom
18-
from .sample import run_sample_app
1918
from .server.utils import run
2019
from .utils import Ref, html_to_vdom
2120
from .widgets import hotswap, multiview
@@ -37,9 +36,9 @@
3736
"log",
3837
"multiview",
3938
"Ref",
40-
"run_sample_app",
4139
"run",
4240
"Stop",
41+
"sample",
4342
"types",
4443
"use_callback",
4544
"use_context",

src/idom/sample.py

-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from . import html
44
from .core.component import component
55
from .core.types import VdomDict
6-
from .server.utils import run
76

87

98
@component
@@ -20,18 +19,3 @@ def App() -> VdomDict:
2019
" to learn more.",
2120
),
2221
)
23-
24-
25-
def run_sample_app(
26-
host: str = "127.0.0.1",
27-
port: int | None = None,
28-
open_browser: bool = False,
29-
) -> None:
30-
"""Run a sample application.
31-
32-
Args:
33-
host: host where the server should run
34-
port: the port on the host to serve from
35-
open_browser: whether to open a browser window after starting the server
36-
"""
37-
run(App, host, port, open_browser=open_browser)

src/idom/server/default.py

+24-4
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,43 @@
55

66
from idom.types import RootComponentConstructor
77

8-
from .utils import default_implementation
8+
from .types import ServerImplementation
9+
from .utils import all_implementations
910

1011

1112
def configure(app: Any, component: RootComponentConstructor) -> None:
1213
"""Configure the given app instance to display the given component"""
13-
return default_implementation().configure(app, component)
14+
return _default_implementation().configure(app, component)
1415

1516

1617
def create_development_app() -> Any:
1718
"""Create an application instance for development purposes"""
18-
return default_implementation().create_development_app()
19+
return _default_implementation().create_development_app()
1920

2021

2122
async def serve_development_app(
2223
app: Any, host: str, port: int, started: asyncio.Event
2324
) -> None:
2425
"""Run an application using a development server"""
25-
return await default_implementation().serve_development_app(
26+
return await _default_implementation().serve_development_app(
2627
app, host, port, started
2728
)
29+
30+
31+
def _default_implementation() -> ServerImplementation[Any]:
32+
"""Get the first available server implementation"""
33+
global _DEFAULT_IMPLEMENTATION
34+
35+
if _DEFAULT_IMPLEMENTATION is not None:
36+
return _DEFAULT_IMPLEMENTATION
37+
38+
try:
39+
implementation = next(all_implementations())
40+
except StopIteration:
41+
raise RuntimeError("No built-in server implementation installed.")
42+
else:
43+
_DEFAULT_IMPLEMENTATION = implementation
44+
return implementation
45+
46+
47+
_DEFAULT_IMPLEMENTATION: ServerImplementation[Any] | None = None

src/idom/server/utils.py

+4-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import asyncio
44
import socket
5-
import sys
65
import warnings
76
import webbrowser
87
from contextlib import closing
@@ -32,7 +31,7 @@ def run(
3231
host: str = "127.0.0.1",
3332
port: int | None = None,
3433
open_browser: bool = True,
35-
implementation: ServerImplementation[Any] = sys.modules[__name__],
34+
implementation: ServerImplementation[Any] | None = None,
3635
) -> None:
3736
"""Run a component with a development server"""
3837

@@ -42,6 +41,9 @@ def run(
4241
stacklevel=2,
4342
)
4443

44+
if implementation is None:
45+
from . import default as implementation
46+
4547
app = implementation.create_development_app()
4648
implementation.configure(app, component)
4749

@@ -91,25 +93,6 @@ def find_available_port(
9193
)
9294

9395

94-
def default_implementation() -> ServerImplementation[Any]:
95-
"""Get the first available server implementation"""
96-
global _DEFAULT_IMPLEMENTATION
97-
98-
if _DEFAULT_IMPLEMENTATION is not None:
99-
return _DEFAULT_IMPLEMENTATION
100-
101-
try:
102-
implementation = next(all_implementations())
103-
except StopIteration:
104-
raise RuntimeError("No built-in server implementation installed.")
105-
else:
106-
_DEFAULT_IMPLEMENTATION = implementation
107-
return implementation
108-
109-
110-
_DEFAULT_IMPLEMENTATION: ServerImplementation[Any] | None = None
111-
112-
11396
def all_implementations() -> Iterator[ServerImplementation[Any]]:
11497
"""Yield all available server implementations"""
11598
for name in SUPPORTED_PACKAGES:

tests/test_sample.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from idom.sample import App, run_sample_app
2-
from idom.server.utils import find_available_port
1+
from idom.sample import App
32
from idom.testing import DisplayFixture
43

54

65
async def test_sample_app(display: DisplayFixture):
7-
await display.show(App)
6+
page = await display.show(App)
7+
h1 = await page.wait_for_selector("h1")
8+
assert (await h1.text_content()) == "Sample Application"

0 commit comments

Comments
 (0)