Skip to content

Commit

Permalink
feat: Add support for Typer
Browse files Browse the repository at this point in the history
  • Loading branch information
evanmags committed May 22, 2023
1 parent b9ddd90 commit 49e8807
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 41 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pip install trogon

## Quickstart

### Click
1. Import `from trogon import tui`
2. Add the `@tui` decorator above your click app. e.g.
```python
Expand All @@ -100,6 +101,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. Add the `tui` decorator above your typer app. e.g.
```python
cli = typer.Typer(...)
init_tui(cli)
```
3. Your click app will have a new `tui` command available.

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

## Follow this project
Expand Down
67 changes: 26 additions & 41 deletions poetry.lock

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

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ textual = {version = ">=0.26.0"}
#textual = {extras = ["dev"], path = "../textual", develop = true}
click = ">=8.0.0"

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


[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

0 comments on commit 49e8807

Please sign in to comment.