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

Draft: Implement command widgets to textual CLI #8

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions .github/workflows/black_format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ on:
paths:
- '.github/workflows/black_format.yml'
- '**.py'
push:
paths:
- '.github/workflows/black_format.yml'
- '**.py'

jobs:
black-format-check:
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
- 'Makefile'
push:
branches:
- 'main'
- '*'

jobs:
build:
Expand Down Expand Up @@ -44,10 +44,10 @@ jobs:
- name: Install dependencies
run: poetry install
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
- name: Run mypy
run: |
source $VENV
mypy .
# - name: Run mypy
# run: |
# source $VENV
# mypy .
- name: Test with pytest
run: |
source $VENV
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ venv.bak/
.dmypy.json
dmypy.json

# Pycharm
.idea/

# Pyre type checker
.pyre/

Expand Down
2 changes: 0 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ repos:
rev: 23.3.0
hooks:
- id: black
language_version: python3.7

8 changes: 8 additions & 0 deletions src/textual_dev/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ def colors() -> None:
ColorsApp().run()


@run.command("widgets")
def widgets() -> None:
"""Explore possible example_widgets."""
rodrigogiraoserrao marked this conversation as resolved.
Show resolved Hide resolved
from textual_dev.previews import WidgetsApp

WidgetsApp().run()


@run.command("keys")
def keys() -> None:
"""Show key events."""
Expand Down
3 changes: 2 additions & 1 deletion src/textual_dev/previews/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
from .colors import ColorsApp
from .easing import EasingApp
from .keys import KeysApp
from .widgets import WidgetsApp

__all__ = ["BorderApp", "ColorsApp", "EasingApp", "KeysApp"]
__all__ = ["BorderApp", "ColorsApp", "EasingApp", "KeysApp", "WidgetsApp"]
158 changes: 158 additions & 0 deletions src/textual_dev/previews/example_widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import random
from statistics import mean

from textual.app import ComposeResult
from textual.containers import Container, Center, Middle
from textual.widgets import (
DirectoryTree,
Footer,
Header,
Input,
Label,
ListView,
ListItem,
LoadingIndicator,
Sparkline,
Static,
Tree,
ProgressBar,
TabbedContent,
TabPane,
Markdown,
Tabs,
)

from .button import button_example
from .checkbox import checkbox_example
from .content_switcher import content_switcher_example
from .data_table import data_table_example
from .markdown import markdown_viewer_example, markdown_example
from .option_list import option_list_example
from .placeholder import placeholder_example
from .pretty import pretty_example
from .radio import radio_button_example, radio_set_example
from .select import select_example, selection_list_example
from .switch import switch_example

# from .text_log import text_log_example


def directory_tree_example(id: str) -> ComposeResult:
yield Container(DirectoryTree("./"), id=id)


def footer_example(id: str) -> ComposeResult:
yield Container(Footer(), id=id)


def header_example(id: str) -> ComposeResult:
yield Container(Header(), id=id)


def input_example(id: str) -> ComposeResult:
yield Container(
Input(placeholder="First Name"), Input(placeholder="Last Name"), id=id
)


def label_example(id: str) -> ComposeResult:
yield Container(Label("Hello, world!"), id=id)


def list_item_example(id: str) -> ComposeResult:
yield Container(
ListView(
ListItem(Label("One")),
ListItem(Label("Two")),
ListItem(Label("Three")),
),
id=id,
)

yield Footer()
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need this footer here? Is it showing any relevant bindings?



def loading_example(id: str) -> ComposeResult:
yield Container(LoadingIndicator(), id=id)


def sparkline_example(id: str) -> ComposeResult:
data = [random.expovariate(1 / 3) for _ in range(1000)]

yield Container(
Sparkline(data, summary_function=max),
Sparkline(data, summary_function=mean),
Sparkline(data, summary_function=min),
id=id,
)


def static_example(id: str) -> ComposeResult:
yield Container(Static("Hello, world!"), id=id)


def tree_example(id: str) -> ComposeResult:
tree: Tree[dict] = Tree("Dune")
tree.root.expand()
characters = tree.root.add("Characters", expand=True)
characters.add_leaf("Paul")
characters.add_leaf("Jessica")
characters.add_leaf("Chani")
yield Container(tree, id=id)


def progress_bar_example(id: str) -> ComposeResult:
bar = ProgressBar(total=100, show_eta=False)
bar.advance(50)
yield Container(Center(Middle(bar)), id=id)
Comment on lines +105 to +107
Copy link
Contributor

Choose a reason for hiding this comment

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

Given that the progress bar is static, which makes sense, maybe create it as ProgressBar() and don't advance it. That way, the user can see the indeterminate animation.

Copy link
Author

Choose a reason for hiding this comment

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

Do I understand it correctly that you want to remove bar.advance(50)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove bar.advance(50) and also total=100.



def tabbed_content_example(id: str):
content = TabbedContent()
content._tab_content = [
TabPane("Leto", Markdown("# Leto"), id="leto"),
TabPane("Jessica", Markdown("# Jessica"), id="jessica"),
TabPane("Paul", Markdown("# Paul"), id="paulo"),
]
# This does not work, reason why I used _tab_content above
# content.add_pane(TabPane("Leto", Markdown("#Leto"), id="letoo"))
# content.add_pane(TabPane("Jessica", Markdown(""), id="jessicoa"))
# content.add_pane(TabPane("Paul", Markdown(""), id="paulo"))

yield Container(content, id=id)


def tabs_example(id: str):
yield Container(Tabs("First tab", "Second tab", "Third tab"), id=id)


__all__ = [
"button_example",
"checkbox_example",
"content_switcher_example",
"data_table_example",
"directory_tree_example",
"footer_example",
"header_example",
"input_example",
"label_example",
"list_item_example",
"loading_example",
"markdown_viewer_example",
"markdown_example",
"option_list_example",
"placeholder_example",
"pretty_example",
"progress_bar_example",
"radio_button_example",
"radio_set_example",
"select_example",
"selection_list_example",
"sparkline_example",
"static_example",
"switch_example",
"tabbed_content_example",
"tabs_example",
"tree_example",
# "text_log_example",
]
25 changes: 25 additions & 0 deletions src/textual_dev/previews/example_widgets/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from textual.app import ComposeResult
from textual.containers import Horizontal, VerticalScroll
from textual.widgets import Static, Button


def button_example(id: str) -> ComposeResult:
yield Horizontal(
VerticalScroll(
Static("Standard Buttons", classes="header"),
Button("Default"),
Button("Primary!", variant="primary"),
Button.success("Success!"),
Button.warning("Warning!"),
Button.error("Error!"),
),
VerticalScroll(
Static("Disabled Buttons", classes="header"),
Button("Default", disabled=True),
Button("Primary!", variant="primary", disabled=True),
Button.success("Success!", disabled=True),
Button.warning("Warning!", disabled=True),
Button.error("Error!", disabled=True),
),
id=id,
)
19 changes: 19 additions & 0 deletions src/textual_dev/previews/example_widgets/checkbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from textual.app import ComposeResult
from textual.containers import VerticalScroll, Container
from textual.widgets import Checkbox


def checkbox_example(id: str) -> ComposeResult:
yield Container(
VerticalScroll(
Checkbox("Arrakis :sweat:"),
Checkbox("Caladan"),
Checkbox("Chusuk"),
Checkbox("[b]Giedi Prime[/b]"),
Checkbox("[magenta]Ginaz[/]"),
Checkbox("Grumman", True),
Checkbox("Kaitain", id="initial_focus"),
Checkbox("Novebruns", True),
),
id=id,
)
60 changes: 60 additions & 0 deletions src/textual_dev/previews/example_widgets/content_switcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from textual.app import ComposeResult
from textual.containers import VerticalScroll, Container, Horizontal
from textual.widgets import Button, ContentSwitcher, DataTable, Markdown

MARKDOWN_EXAMPLE = """# Three Flavours Cornetto

The Three Flavours Cornetto trilogy is an anthology series of British
comedic genre films directed by Edgar Wright.

## Shaun of the Dead

| Flavour | UK Release Date | Director |
| -- | -- | -- |
| Strawberry | 2004-04-09 | Edgar Wright |

## Hot Fuzz

| Flavour | UK Release Date | Director |
| -- | -- | -- |
| Classico | 2007-02-17 | Edgar Wright |

## The World's End

| Flavour | UK Release Date | Director |
| -- | -- | -- |
| Mint | 2013-07-19 | Edgar Wright |
"""


def content_switcher_example(id: str) -> ComposeResult:
table: DataTable = DataTable(id="data-table")
table.add_columns("Book", "Year")
table.add_rows(
[
(title.ljust(35), year)
for title, year in (
("Dune", 1965),
("Dune Messiah", 1969),
("Children of Dune", 1976),
("God Emperor of Dune", 1981),
("Heretics of Dune", 1984),
("Chapterhouse: Dune", 1985),
)
]
)

yield Container(
Horizontal(
Button("DataTable", id="data-table"),
Button("Markdown", id="markdown"),
id="buttons",
),
ContentSwitcher(
table,
VerticalScroll(Markdown(MARKDOWN_EXAMPLE), id="markdown"),
initial="data-table",
id="content-switcher-example",
),
id=id,
)
24 changes: 24 additions & 0 deletions src/textual_dev/previews/example_widgets/data_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from textual.containers import Container
from textual.app import ComposeResult
from textual.widgets import DataTable

ROWS = [
("lane", "swimmer", "country", "time"),
(4, "Joseph Schooling", "Singapore", 50.39),
(2, "Michael Phelps", "United States", 51.14),
(5, "Chad le Clos", "South Africa", 51.14),
(6, "László Cseh", "Hungary", 51.14),
(3, "Li Zhuhao", "China", 51.26),
(8, "Mehdy Metella", "France", 51.58),
(7, "Tom Shields", "United States", 51.73),
(1, "Aleksandr Sadovnikov", "Russia", 51.84),
(10, "Darren Burns", "Scotland", 51.84),
]


def data_table_example(id: str) -> ComposeResult:
table: DataTable = DataTable()
table.add_columns(*ROWS[0])
table.add_rows(ROWS[1:])

yield Container(table, id=id)
Loading