Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include port in console run hint #9

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/textual_dev/renderables.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,23 @@


class DevConsoleHeader:
def __init__(self, verbose: bool = False) -> None:
"""Renderable representing the header at the top of the console

Args:
port: The port the devtools server is running on.
verbose: Whether verbose logging is enabled
"""

def __init__(self, port: int | None = None, verbose: bool = False) -> None:
self.port = port
self.verbose = verbose

def __rich_console__(
self, console: Console, options: ConsoleOptions
) -> RenderResult:
preamble = Text.from_markup(
f"[bold]Textual Development Console [magenta]v{version('textual')}\n"
"[magenta]Run a Textual app with [reverse]textual run --dev my_app.py[/] to connect.\n"
f"[magenta]Run a Textual app with [reverse]{self._run_command()}[/] to connect.\n"
"[magenta]Press [reverse]Ctrl+C[/] to quit."
)
if self.verbose:
Expand All @@ -45,6 +53,17 @@ def __rich_console__(
yield from line
yield new_line

def _run_command(self) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring please!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! I also added a docstring to the DevConsoleHeader class to match the other renderables in the file

"""Get help text for the user to connect to the console

Returns:
The command a user can run to connect a Textual app to the dev server
"""
if self.port:
return f"textual run --port {self.port} --dev my_app.py"
else:
return "textual run --dev my_app.py"


class DevConsoleLog:
"""Renderable representing a single log message
Expand Down
8 changes: 6 additions & 2 deletions src/textual_dev/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async def _on_startup(app: Application) -> None:
def _run_devtools(
verbose: bool, exclude: list[str] | None = None, port: int | None = None
) -> None:
app = _make_devtools_aiohttp_app(verbose=verbose, exclude=exclude)
app = _make_devtools_aiohttp_app(port=port, verbose=verbose, exclude=exclude)

def noop_print(_: str) -> None:
pass
Expand All @@ -63,6 +63,7 @@ def noop_print(_: str) -> None:

def _make_devtools_aiohttp_app(
size_change_poll_delay_secs: float = DEFAULT_SIZE_CHANGE_POLL_DELAY_SECONDS,
port: int | None = None,
verbose: bool = False,
exclude: list[str] | None = None,
) -> Application:
Expand All @@ -73,7 +74,10 @@ def _make_devtools_aiohttp_app(

app["verbose"] = verbose
app["service"] = DevtoolsService(
update_frequency=size_change_poll_delay_secs, verbose=verbose, exclude=exclude
update_frequency=size_change_poll_delay_secs,
port=port,
verbose=verbose,
exclude=exclude,
)

app.add_routes(
Expand Down
5 changes: 4 additions & 1 deletion src/textual_dev/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@ class DevtoolsService:
def __init__(
self,
update_frequency: float,
port: int | None = None,
verbose: bool = False,
exclude: list[str] | None = None,
) -> None:
"""
Args:
update_frequency: The number of seconds to wait between
sending updates of the console size to connected clients.
port: The port the devtools server is running on.
verbose: Enable verbose logging on client.
exclude: List of log groups to exclude from output.
"""
self.update_frequency = update_frequency
self.port = port
self.verbose = verbose
self.exclude = {name.upper() for name in exclude} if exclude else set()
self.console = Console()
Expand All @@ -49,7 +52,7 @@ def __init__(
async def start(self) -> None:
"""Starts devtools tasks"""
self.size_poll_task = asyncio.create_task(self._console_size_poller())
self.console.print(DevConsoleHeader(verbose=self.verbose))
self.console.print(DevConsoleHeader(port=self.port, verbose=self.verbose))

@property
def clients_connected(self) -> bool:
Expand Down