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

feat: Add support for Typer #14

Merged
merged 5 commits into from
Sep 20, 2024
Merged
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
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ pip install trogon

## Quickstart

### Click
1. Import `from trogon import tui`
2. Add the `@tui` decorator above your click app. e.g.
2. Add the `@tui` decorator above your click app, e.g.
```python
from trogon import tui

Expand All @@ -102,6 +103,15 @@ pip install trogon
```
3. Your click app will have a new `tui` command available.

### Typer
1. Import `from trogon.typer import init_tui`
2. Pass your Typer CLI app into the `init_tui` function, e.g.
```python
cli = typer.Typer(...)
init_tui(cli)
```
3. Your Typer app will have a new `tui` command available.

See also the `examples` folder for two example apps.

## Custom command name and custom help
Expand Down
151 changes: 91 additions & 60 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ python = "^3.8"
textual = ">=0.61.0"
click = ">=8.0.0"

typer = {version = ">=0.9.0", optional = true}

[tool.poetry.extras]
typer = ["typer"]


[tool.poetry.group.dev.dependencies]
mypy = "^1.2.0"
Expand Down
23 changes: 23 additions & 0 deletions trogon/typer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import click

try:
import typer
except ImportError:
raise ImportError(
"The extra `trogon[typer]` is required to enable tui generation from Typer apps."
)

from trogon.trogon import Trogon


def init_tui(app: typer.Typer, name: str | None = None):
def wrapped_tui():
Trogon(
typer.main.get_group(app),
app_name=name,
click_context=click.get_current_context(),
).run()

app.command("tui", help="Open Textual TUI.")(wrapped_tui)

return app