Skip to content

Commit 07258a4

Browse files
authored
Merge pull request #24 from Textualize/add-web-interface
web interface
2 parents 5c167d1 + 93750b6 commit 07258a4

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

src/textual_web/cli.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def print_disclaimer() -> None:
9292
default=0,
9393
help="Exit textual-web when no apps have been launched in WAIT seconds",
9494
)
95+
@click.option("-w", "--web-interface", is_flag=True, help="Enable web interface")
9596
@click.option("-s", "--signup", is_flag=True, help="Create a textual-web account.")
9697
@click.option("--welcome", is_flag=True, help="Launch an example app.")
9798
@click.option("--merlin", is_flag=True, help="Launch Merlin game.")
@@ -102,6 +103,7 @@ def app(
102103
dev: bool,
103104
terminal: bool,
104105
exit_on_idle: int,
106+
web_interface: bool,
105107
api_key: str,
106108
signup: bool,
107109
welcome: bool,
@@ -185,6 +187,7 @@ def app(
185187
api_key=api_key or None,
186188
devtools=dev,
187189
exit_on_idle=exit_on_idle,
190+
web_interface=web_interface,
188191
)
189192

190193
for app_command in run:

src/textual_web/exit_poller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from time import monotonic
66
from typing import TYPE_CHECKING
77

8-
EXIT_POLL_RATE = 15
8+
EXIT_POLL_RATE = 5
99

1010
log = logging.getLogger("textual-web")
1111

src/textual_web/ganglion_client.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
from .session import SessionConnector
3232
from .session_manager import SessionManager
3333
from .types import Meta, RouteKey, SessionID
34+
from .web import run_web_interface
35+
3436

3537
if TYPE_CHECKING:
3638
from .config import Config
@@ -78,10 +80,12 @@ def __init__(
7880
api_key: str | None,
7981
devtools: bool = False,
8082
exit_on_idle: int = 0,
83+
web_interface: bool = False,
8184
) -> None:
8285
self.environment = environment
8386
self.websocket_url = environment.url
8487
self.exit_on_idle = exit_on_idle
88+
self.web_interface = web_interface
8589

8690
abs_path = Path(config_path).absolute()
8791
path = abs_path if abs_path.is_dir() else abs_path.parent
@@ -165,6 +169,7 @@ def decode_envelope(
165169

166170
async def run(self) -> None:
167171
"""Run the connection loop."""
172+
168173
try:
169174
self._exit_poller.start()
170175
await self._run()
@@ -201,7 +206,15 @@ def exit_handler(signal_handler, stack_frame) -> None:
201206
self._poller.set_loop(loop)
202207
self._poller.start()
203208

204-
self._task = asyncio.create_task(self.connect())
209+
if self.web_interface:
210+
app = await run_web_interface()
211+
try:
212+
self._task = asyncio.create_task(self.connect())
213+
finally:
214+
await app.shutdown()
215+
else:
216+
self._task = asyncio.create_task(self.connect())
217+
205218
await self._task
206219

207220
def force_exit(self) -> None:

src/textual_web/web.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
An optional web interface to control textual-web
3+
4+
Note: Currently just a stub.
5+
6+
"""
7+
8+
import logging
9+
10+
from aiohttp import web
11+
12+
13+
log = logging.getLogger("textual-web")
14+
15+
16+
async def run_web_interface() -> web.Application:
17+
"""Run the web interface."""
18+
19+
async def health_check(request) -> web.Response:
20+
return web.Response(text="Hello, world")
21+
22+
app = web.Application()
23+
app.add_routes([web.get("/health-check/", health_check)])
24+
25+
runner = web.AppRunner(app)
26+
await runner.setup()
27+
site = web.TCPSite(runner, "0.0.0.0", 8080)
28+
await site.start()
29+
log.info("Web interface started on port 8080")
30+
return app

0 commit comments

Comments
 (0)