Skip to content

Commit 11ee93d

Browse files
committed
feat: Add support for Typer
1 parent b9ddd90 commit 11ee93d

File tree

4 files changed

+67
-41
lines changed

4 files changed

+67
-41
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ pip install trogon
9090

9191
## Quickstart
9292

93+
### Click
9394
1. Import `from trogon import tui`
9495
2. Add the `@tui` decorator above your click app. e.g.
9596
```python
@@ -100,6 +101,15 @@ pip install trogon
100101
```
101102
3. Your click app will have a new `tui` command available.
102103

104+
### Typer
105+
1. Import `from trogon.typer import init_tui`
106+
2. Add the `tui` decorator above your typer app. e.g.
107+
```python
108+
cli = typer.Typer(...)
109+
init_tui(cli)
110+
```
111+
3. Your click app will have a new `tui` command available.
112+
103113
See also the `examples` folder for two example apps.
104114

105115
## Follow this project

poetry.lock

Lines changed: 29 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ textual = {version = ">=0.26.0"}
3131
#textual = {extras = ["dev"], path = "../textual", develop = true}
3232
click = ">=8.0.0"
3333

34+
typer = {version = ">=0.9.0", optional = true}
35+
36+
[tool.poetry.extras]
37+
typer = ["typer"]
38+
3439

3540
[tool.poetry.group.dev.dependencies]
3641
mypy = "^1.2.0"

trogon/typer.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import click
2+
3+
try:
4+
import typer
5+
except ImportError:
6+
raise ImportError(
7+
"The extra `trogon[typer]` is required to enable tui generation from Typer apps."
8+
)
9+
10+
from trogon.trogon import Trogon
11+
12+
13+
def init_tui(app: typer.Typer, name: str | None = None):
14+
def wrapped_tui():
15+
Trogon(
16+
typer.main.get_group(app),
17+
app_name=name,
18+
click_context=click.get_current_context(),
19+
).run()
20+
21+
app.command("tui", help="Open Textual TUI.")(wrapped_tui)
22+
23+
return app

0 commit comments

Comments
 (0)