nuked the sandbox

This commit is contained in:
Will McGugan
2022-10-21 16:21:30 +01:00
parent 727a4f1292
commit 81f561657f
66 changed files with 0 additions and 3000 deletions

View File

@@ -1,252 +0,0 @@
/* CSS file for basic.py */
* {
transition: color 300ms linear, background 300ms linear;
}
*:hover {
/* tint: 30% red;
/* outline: heavy red; */
}
App > Screen {
background: $surface;
color: $text;
layers: base sidebar;
color: $text;
background: $background;
layout: vertical;
overflow: hidden;
}
#tree-container {
overflow-y: auto;
height: 20;
margin: 1 2;
background: $panel;
padding: 1 2;
}
DirectoryTree {
padding: 0 1;
height: auto;
}
DataTable {
/*border:heavy red;*/
/* tint: 10% green; */
/* opacity: 50%; */
padding: 1;
margin: 1 2;
height: 24;
}
#sidebar {
color: $text;
background: $panel;
dock: left;
width: 30;
margin-bottom: 1;
offset-x: -100%;
transition: offset 500ms in_out_cubic 2s;
layer: sidebar;
}
#sidebar.-active {
offset-x: 0;
}
#sidebar .title {
height: 1;
background: $primary-background-darken-1;
color: $text-muted;
border-right: wide $background;
content-align: center middle;
}
#sidebar .user {
height: 8;
background: $panel-darken-1;
color: $text-muted;
border-right: wide $background;
content-align: center middle;
}
#sidebar .content {
background: $panel-darken-2;
color: $text;
border-right: wide $background;
content-align: center middle;
}
Tweet {
height:12;
width: 100%;
background: $panel;
color: $text;
layout: vertical;
/* border: outer $primary; */
padding: 1;
border: wide $panel;
overflow: auto;
/* scrollbar-gutter: stable; */
align-horizontal: center;
box-sizing: border-box;
}
.scrollable {
overflow-x: auto;
overflow-y: scroll;
margin: 1 2;
height: 24;
align-horizontal: center;
layout: vertical;
}
.code {
height: auto;
}
TweetHeader {
height:1;
background: $accent;
color: $text
}
TweetBody {
width: 100%;
background: $panel;
color: $text;
height: auto;
padding: 0 1 0 0;
}
Tweet.scroll-horizontal TweetBody {
width: 350;
}
.button {
background: $accent;
color: $text;
width:20;
height: 3;
/* border-top: hidden $accent-darken-3; */
border: tall $accent-darken-2;
/* border-left: tall $accent-darken-1; */
/* padding: 1 0 0 0 ; */
transition: background 400ms in_out_cubic, color 400ms in_out_cubic;
}
.button:hover {
background: $accent-lighten-1;
color: $text-disabled;
width: 20;
height: 3;
border: tall $accent-darken-1;
/* border-left: tall $accent-darken-3; */
}
#footer {
color: $text;
background: $accent;
height: 1;
content-align: center middle;
dock:bottom;
}
#sidebar .content {
layout: vertical
}
OptionItem {
height: 3;
background: $panel;
border-right: wide $background;
border-left: blank;
content-align: center middle;
}
OptionItem:hover {
height: 3;
color: $text;
background: $primary-darken-1;
/* border-top: hkey $accent2-darken-3;
border-bottom: hkey $accent2-darken-3; */
text-style: bold;
border-left: outer $secondary-darken-2;
}
Error {
width: 100%;
height:3;
background: $error;
color: $text;
border-top: tall $error-darken-2;
border-bottom: tall $error-darken-2;
padding: 0;
text-style: bold;
align-horizontal: center;
}
Warning {
width: 100%;
height:3;
background: $warning;
color: $text-muted;
border-top: tall $warning-darken-2;
border-bottom: tall $warning-darken-2;
text-style: bold;
align-horizontal: center;
}
Success {
width: 100%;
height:auto;
box-sizing: border-box;
background: $success;
color: $text-muted;
border-top: hkey $success-darken-2;
border-bottom: hkey $success-darken-2;
text-style: bold ;
align-horizontal: center;
}
.horizontal {
layout: horizontal
}

View File

@@ -1,235 +0,0 @@
from rich.console import RenderableType
from rich.syntax import Syntax
from rich.text import Text
from textual.app import App, ComposeResult
from textual.reactive import Reactive
from textual.widget import Widget
from textual.widgets import Static, DataTable, DirectoryTree, Header, Footer
from textual.containers import Container
CODE = '''
from __future__ import annotations
from typing import Iterable, TypeVar
T = TypeVar("T")
def loop_first(values: Iterable[T]) -> Iterable[tuple[bool, T]]:
"""Iterate and generate a tuple with a flag for first value."""
iter_values = iter(values)
try:
value = next(iter_values)
except StopIteration:
return
yield True, value
for value in iter_values:
yield False, value
def loop_last(values: Iterable[T]) -> Iterable[tuple[bool, T]]:
"""Iterate and generate a tuple with a flag for last value."""
iter_values = iter(values)
try:
previous_value = next(iter_values)
except StopIteration:
return
for value in iter_values:
yield False, previous_value
previous_value = value
yield True, previous_value
def loop_first_last(values: Iterable[T]) -> Iterable[tuple[bool, bool, T]]:
"""Iterate and generate a tuple with a flag for first and last value."""
iter_values = iter(values)
try:
previous_value = next(iter_values)
except StopIteration:
return
first = True
for value in iter_values:
yield first, False, previous_value
first = False
previous_value = value
yield first, True, previous_value
'''
lorem_short = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. In velit liber a a a, volutpat nec hendrerit at, faucibus in odio. Aliquam hendrerit nibh sed quam volutpat maximus. Nullam suscipit convallis lorem quis sodales. In tristique lobortis ante et dictum. Ut at finibus ipsum."""
lorem = (
lorem_short
+ """ In urna dolor, placerat et mi facilisis, congue sollicitudin massa. Phasellus felis turpis, cursus eu lectus et, porttitor malesuada augue. Sed feugiat volutpat velit, sollicitudin fringilla velit bibendum faucibus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In velit libero, volutpat nec hendrerit at, faucibus in odio. Aliquam hendrerit nibh sed quam volutpat maximus. Nullam suscipit convallis lorem quis sodales. In tristique lobortis ante et dictum. Ut at finibus ipsum. In urna dolor, placerat et mi facilisis, congue sollicitudin massa. Phasellus felis turpis, cursus eu lectus et, porttitor malesuada augue. Sed feugiat volutpat velit, sollicitudin fringilla velit bibendum faucibus. """
)
lorem_short_text = Text.from_markup(lorem_short)
lorem_long_text = Text.from_markup(lorem * 2)
class TweetHeader(Widget):
def render(self) -> RenderableType:
return Text("Lorem Impsum", justify="center")
class TweetBody(Widget):
short_lorem = Reactive(False)
def render(self) -> Text:
return lorem_short_text if self.short_lorem else lorem_long_text
class Tweet(Widget):
pass
class OptionItem(Widget):
def render(self) -> Text:
return Text("Option")
class Error(Widget):
def render(self) -> Text:
return Text("This is an error message", justify="center")
class Warning(Widget):
def render(self) -> Text:
return Text("This is a warning message", justify="center")
class Success(Widget):
def render(self) -> Text:
return Text("This is a success message", justify="center")
class BasicApp(App, css_path="basic.css"):
"""A basic app demonstrating CSS"""
def on_load(self):
"""Bind keys here."""
self.bind("s", "toggle_class('#sidebar', '-active')", description="Sidebar")
self.bind("d", "toggle_dark", description="Dark mode")
self.bind("q", "quit", description="Quit")
self.bind("f", "query_test", description="Query test")
def compose(self):
yield Header()
table = DataTable()
self.scroll_to_target = Tweet(TweetBody())
yield Container(
Tweet(TweetBody()),
Widget(
Static(
Syntax(CODE, "python", line_numbers=True, indent_guides=True),
classes="code",
),
classes="scrollable",
),
table,
Widget(DirectoryTree("~/code/textual"), id="tree-container"),
Error(),
Tweet(TweetBody(), classes="scrollbar-size-custom"),
Warning(),
Tweet(TweetBody(), classes="scroll-horizontal"),
Success(),
Tweet(TweetBody(), classes="scroll-horizontal"),
Tweet(TweetBody(), classes="scroll-horizontal"),
Tweet(TweetBody(), classes="scroll-horizontal"),
Tweet(TweetBody(), classes="scroll-horizontal"),
Tweet(TweetBody(), classes="scroll-horizontal"),
)
yield Widget(
Widget(classes="title"),
Widget(classes="user"),
OptionItem(),
OptionItem(),
OptionItem(),
Widget(classes="content"),
id="sidebar",
)
yield Footer()
table.add_column("Foo", width=20)
table.add_column("Bar", width=20)
table.add_column("Baz", width=20)
table.add_column("Foo", width=20)
table.add_column("Bar", width=20)
table.add_column("Baz", width=20)
table.zebra_stripes = True
for n in range(100):
table.add_row(*[f"Cell ([b]{n}[/b], {col})" for col in range(6)])
def on_mount(self):
self.sub_title = "Widget demo"
async def on_key(self, event) -> None:
await self.dispatch_key(event)
def action_toggle_dark(self):
self.dark = not self.dark
def action_query_test(self):
query = self.query("Tweet")
self.log(query)
self.log(query.nodes)
self.log(query)
self.log(query.nodes)
query.set_styles("outline: outer red;")
query = query.exclude(".scroll-horizontal")
self.log(query)
self.log(query.nodes)
# query = query.filter(".rubbish")
# self.log(query)
# self.log(query.first())
async def key_q(self):
await self.shutdown()
def key_x(self):
self.panic(self.tree)
def key_escape(self):
self.app.bell()
def key_t(self):
# Pressing "t" toggles the content of the TweetBody widget, from a long "Lorem ipsum..." to a shorter one.
tweet_body = self.query("TweetBody").first()
tweet_body.short_lorem = not tweet_body.short_lorem
def key_v(self):
self.get_child(id="content").scroll_to_widget(self.scroll_to_target)
def key_space(self):
self.bell()
app = BasicApp()
if __name__ == "__main__":
app.run()
# from textual.geometry import Region
# from textual.color import Color
# print(Region.intersection.cache_info())
# print(Region.overlaps.cache_info())
# print(Region.union.cache_info())
# print(Region.split_vertical.cache_info())
# print(Region.__contains__.cache_info())
# from textual.css.scalar import Scalar
# print(Scalar.resolve_dimension.cache_info())
# from rich.style import Style
# from rich.cells import cached_cell_len
# print(Style._add.cache_info())
# print(cached_cell_len.cache_info())

View File

@@ -1,6 +0,0 @@
Button {
padding-left: 1;
padding-right: 1;
margin: 3;
text-opacity: 30%;
}

View File

@@ -1,34 +0,0 @@
from textual import events
from textual.app import App, ComposeResult
from textual.containers import Vertical
from textual.widgets import Button
class ButtonsApp(App[str]):
def compose(self) -> ComposeResult:
yield Vertical(
Button("default", id="foo"),
Button.success("success", id="bar"),
Button.warning("warning", id="baz"),
Button.error("error", id="baz"),
)
def on_button_pressed(self, event: Button.Pressed) -> None:
self.app.bell()
async def on_key(self, event: events.Key) -> None:
await self.dispatch_key(event)
def key_d(self):
self.dark = not self.dark
app = ButtonsApp(
log_path="textual.log",
css_path="buttons.css",
watch_css=True,
)
if __name__ == "__main__":
result = app.run()
print(repr(result))

View File

@@ -1,28 +0,0 @@
Screen {
align: center middle;
}
Container {
width: 50;
height: 15;
background: $boost;
align: center middle;
}
Checkbox {
}
#check {
background: red;
border: none;
padding: 0;
}
#check > .checkbox--switch {
color: red;
background: blue;
}
#check:focus {
tint: magenta 60%;
}

View File

@@ -1,24 +0,0 @@
from textual.app import App, ComposeResult
from textual.containers import Container
from textual.widgets import Checkbox, Footer
class CheckboxApp(App):
BINDINGS = [("s", "switch", "Press switch"), ("d", "toggle_dark", "Dark mode")]
def compose(self) -> ComposeResult:
yield Footer()
yield Container(Checkbox(id="check", animate=True))
def action_switch(self) -> None:
checkbox = self.query_one(Checkbox)
checkbox.toggle()
def key_f(self):
print(self.app.focused)
app = CheckboxApp(css_path="check.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,8 +0,0 @@
* {
transition: color 300ms linear, background 300ms linear;
}
#another-box {
background: $boost;
padding: 1 2;
}

View File

@@ -1,28 +0,0 @@
from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.color import Color
from textual.widgets import Static
START_COLOR = Color.parse("#FF0000EE")
END_COLOR = Color.parse("#0000FF0F")
class ColorAnimate(App):
BINDINGS = [Binding("d", action="toggle_dark", description="Dark mode")]
def compose(self) -> ComposeResult:
self.box = Static("Hello, world", id="box")
self.box.styles.background = START_COLOR
self.another_box = Static("Another box with $boost", id="another-box")
yield self.box
yield self.another_box
def key_a(self):
self.animator.animate(self.box.styles, "background", END_COLOR, duration=2.0)
app = ColorAnimate(css_path="color_animate.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,68 +0,0 @@
from __future__ import annotations
from pathlib import Path
from typing import Iterable
from rich.console import RenderableType
from rich.table import Table
from rich.text import Text
from textual.app import App
from textual.geometry import Size
from textual.reactive import Reactive
from textual.widget import Widget
from textual.widgets._input import Input
def get_files() -> list[Path]:
files = list(Path.cwd().iterdir())
return files
class FileTable(Widget):
filter = Reactive("", layout=True)
def __init__(self, *args, files: Iterable[Path] | None = None, **kwargs):
super().__init__(*args, **kwargs)
self.files = files if files is not None else []
@property
def filtered_files(self) -> list[Path]:
return [
file
for file in self.files
if self.filter == "" or (self.filter and self.filter in file.name)
]
def get_content_height(self, container: Size, viewport: Size, width: int) -> int:
return len(self.filtered_files)
def render(self) -> RenderableType:
grid = Table.grid()
grid.add_column()
for file in self.filtered_files:
file_text = Text(f" {file.name}")
if self.filter:
file_text.highlight_regex(self.filter, "black on yellow")
grid.add_row(file_text)
return grid
class FileSearchApp(App):
dark = True
def on_mount(self) -> None:
self.file_table = FileTable(id="file_table", files=list(Path.cwd().iterdir()))
self.search_bar = Input(placeholder="Search for files...")
# self.search_bar.focus()
self.mount(search_bar=self.search_bar)
self.mount(file_table_wrapper=Widget(self.file_table))
def on_input_changed(self, event: Input.Changed) -> None:
self.file_table.filter = event.value
app = FileSearchApp(css_path="file_search.scss", watch_css=True)
if __name__ == "__main__":
result = app.run()

View File

@@ -1,15 +0,0 @@
Screen {
}
#file_table_wrapper {
scrollbar-color: $accent-darken-1;
}
#file_table {
height: auto;
}
#search_bar {
height: 1;
}

View File

@@ -1,52 +0,0 @@
*:focus {
tint: red 20%;
}
#info {
background: $primary;
dock: top;
height: 3;
padding: 1;
}
#body {
dock: top;
}
#left_list {
width: 50%;
}
#right_list {
width: 50%;
}
#footer {
height: 1;
background: $secondary;
padding: 0 1;
dock: bottom;
}
.list:focus-within {
background: $panel-lighten-1;
outline-top: $accent-lighten-1;
outline-bottom: $accent-lighten-1;
}
.list {
background: $surface;
border-top: hkey $surface-darken-1;
}
.list-item {
background: $surface;
height: auto;
border: $surface-darken-1 tall;
padding: 0 1;
}
.list-item:focus {
background: $surface-darken-1;
outline: $accent tall;
}

View File

@@ -1,66 +0,0 @@
from textual import containers as layout
from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.widgets import Static, Input
class Label(Static, can_focus=True):
pass
class FocusKeybindsApp(App):
dark = True
BINDINGS = [Binding("a", "private_handler", "Private Handler")]
def on_load(self) -> None:
self.bind("1", "focus('widget1')")
self.bind("2", "focus('widget2')")
self.bind("3", "focus('widget3')")
self.bind("4", "focus('widget4')")
self.bind("q", "focus('widgetq')")
self.bind("w", "focus('widgetw')")
self.bind("e", "focus('widgete')")
self.bind("r", "focus('widgetr')")
def compose(self) -> ComposeResult:
yield Static(
"Use keybinds to shift focus between the widgets in the lists below",
id="info",
)
yield layout.Horizontal(
layout.Vertical(
Label("Press 1 to focus", id="widget1", classes="list-item"),
Label("Press 2 to focus", id="widget2", classes="list-item"),
Input(placeholder="Enter some text..."),
Label("Press 3 to focus", id="widget3", classes="list-item"),
Label("Press 4 to focus", id="widget4", classes="list-item"),
classes="list",
id="left_list",
),
layout.Vertical(
Label("Press Q to focus", id="widgetq", classes="list-item"),
Label("Press W to focus", id="widgetw", classes="list-item"),
Label("Press E to focus", id="widgete", classes="list-item"),
Label("Press R to focus", id="widgetr", classes="list-item"),
classes="list",
id="right_list",
),
)
yield Static("No widget focused", id="footer")
def on_descendant_focus(self):
self.get_child("footer").update(
f"Focused: {self.focused.id}" or "No widget focused"
)
def key_p(self):
print(self.app.focused.parent)
print(self.app.focused)
def _action_private_handler(self):
print("inside private handler!")
app = FocusKeybindsApp(css_path="focus_keybinds.css", watch_css=True)
app.run()

View File

@@ -1,12 +0,0 @@
Screen {
align: center middle;
background: darkslategrey;
overflow: auto auto;
}
#box1 {
background: darkmagenta;
width: auto;
opacity: 0.5;
padding: 4 8;
}

View File

@@ -1,52 +0,0 @@
from __future__ import annotations
from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.screen import Screen
from textual.widgets import Static, Footer, Header
class MainScreen(Screen):
BINDINGS = [
Binding(
key="ctrl+t", action="text_fade_out", description="text-opacity fade out"
),
(
"o,f,w",
"widget_fade_out",
"opacity fade out",
# key_display="o or f or w",
),
]
def compose(self) -> ComposeResult:
yield Header()
yield Static("Hello, world!", id="box1")
yield Footer()
def action_text_fade_out(self) -> None:
box = self.query_one("#box1")
self.app.animator.animate(box.styles, "text_opacity", value=0.0, duration=1)
def action_widget_fade_out(self) -> None:
box = self.query_one("#box1")
self.app.animator.animate(box.styles, "opacity", value=0.0, duration=1)
class JustABox(App):
def on_mount(self):
self.push_screen(MainScreen())
def key_d(self):
print(self.screen.styles.get_rules())
print(self.screen.styles.css)
def key_plus(self):
print("plus!")
app = JustABox(watch_css=True, css_path="../darren/just_a_box.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,9 +0,0 @@
Focusable {
padding: 1 2;
background: $panel;
margin-bottom: 1;
}
Focusable:focus {
outline: solid dodgerblue;
}

View File

@@ -1,73 +0,0 @@
from textual.app import App, ComposeResult, ScreenStackError
from textual.binding import Binding
from textual.screen import Screen
from textual.widgets import Static, Footer, Input
from some_text import TEXT
class Focusable(Static, can_focus=True):
pass
class CustomScreen(Screen):
def compose(self) -> ComposeResult:
yield Focusable(f"Screen {id(self)} - two {TEXT}")
yield Focusable(f"Screen {id(self)} - three")
yield Focusable(f"Screen {id(self)} - four")
yield Input(placeholder="Text input")
yield Footer()
class MyInstalledScreen(Screen):
def __init__(self, string: str):
super().__init__()
self.string = string
def compose(self) -> ComposeResult:
yield Static(f"Hello, world! {self.string}")
class ScreensFocusApp(App):
BINDINGS = [
Binding("plus", "push_new_screen", "Push"),
Binding("minus", "pop_top_screen", "Pop"),
Binding("d", "toggle_dark", "Toggle Dark"),
Binding("q", "push_screen('q')", "Screen Q"),
Binding("w", "push_screen('w')", "Screen W"),
Binding("e", "push_screen('e')", "Screen E"),
Binding("r", "push_screen('r')", "Screen R"),
]
SCREENS = {
"q": MyInstalledScreen("q"),
"w": MyInstalledScreen("w"),
"e": MyInstalledScreen("e"),
"r": MyInstalledScreen("r"),
}
def compose(self) -> ComposeResult:
yield Focusable("App - one")
yield Input(placeholder="Text input")
yield Input(placeholder="Text input")
yield Focusable("App - two")
yield Focusable("App - three")
yield Focusable("App - four")
yield Footer()
def action_push_new_screen(self):
self.push_screen(CustomScreen())
def action_pop_top_screen(self):
try:
self.pop_screen()
except ScreenStackError:
pass
def _action_toggle_dark(self):
self.dark = not self.dark
app = ScreensFocusApp(css_path="screens_focus.css")
if __name__ == "__main__":
app.run()

View File

@@ -1 +0,0 @@
TEXT = "ABCDEFG"

View File

@@ -1,147 +0,0 @@
from dataclasses import dataclass
from rich.console import RenderableType
from rich.padding import Padding
from rich.rule import Rule
from rich.style import Style
from textual import events
from textual.app import App
from textual.widget import Widget
from textual.widgets.tabs import Tabs, Tab
class Hr(Widget):
def render(self) -> RenderableType:
return Rule()
class Info(Widget):
def __init__(self, text: str) -> None:
super().__init__()
self.text = text
def render(self) -> RenderableType:
return Padding(f"{self.text}", pad=(0, 1))
@dataclass
class WidgetDescription:
description: str
widget: Widget
class BasicApp(App):
"""Sandbox application used for testing/development by Textual developers"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.keys_to_tabs = {
"1": Tab("January", name="one"),
"2": Tab("に月", name="two"),
"3": Tab("March", name="three"),
"4": Tab("April", name="four"),
"5": Tab("May", name="five"),
"6": Tab("And a really long tab!", name="six"),
}
tabs = list(self.keys_to_tabs.values())
self.examples = [
WidgetDescription(
"Customise the spacing between tabs, e.g. tab_padding=1",
Tabs(
tabs,
tab_padding=1,
),
),
WidgetDescription(
"Change the opacity of inactive tab text, e.g. inactive_text_opacity=.2",
Tabs(
tabs,
active_tab="two",
active_bar_style="#1493FF",
inactive_text_opacity=0.2,
tab_padding=2,
),
),
WidgetDescription(
"Change the color of the inactive portions of the underline, e.g. inactive_bar_style='blue'",
Tabs(
tabs,
active_tab="four",
inactive_bar_style="blue",
),
),
WidgetDescription(
"Change the color of the active portion of the underline, e.g. active_bar_style='red'",
Tabs(
tabs,
active_tab="five",
active_bar_style="red",
inactive_text_opacity=1,
),
),
WidgetDescription(
"Change the styling of active and inactive labels (active_tab_style, inactive_tab_style)",
Tabs(
tabs,
active_tab="one",
active_bar_style="#DA812D",
active_tab_style="bold #FFCB4D on #021720",
inactive_tab_style="italic #887AEF on #021720",
inactive_bar_style="#695CC8",
inactive_text_opacity=0.6,
),
),
WidgetDescription(
"Change the animation duration and function (animation_duration=1, animation_function='out_quad')",
Tabs(
tabs,
active_tab="one",
active_bar_style="#887AEF",
inactive_text_opacity=0.2,
animation_duration=1,
animation_function="out_quad",
),
),
WidgetDescription(
"Choose which tab to start on by name, e.g. active_tab='three'",
Tabs(
tabs,
active_tab="three",
active_bar_style="#FFCB4D",
tab_padding=3,
),
),
]
def on_load(self):
"""Bind keys here."""
self.bind("tab", "toggle_class('#sidebar', '-active')")
self.bind("a", "toggle_class('#header', '-visible')")
self.bind("c", "toggle_class('#content', '-content-visible')")
self.bind("d", "toggle_class('#footer', 'dim')")
def on_key(self, event: events.Key) -> None:
for example in self.examples:
tab = self.keys_to_tabs.get(event.key)
if tab:
example.widget._active_tab_name = tab.name
def on_mount(self):
"""Build layout here."""
self.mount(
info=Info(
"\n"
"• The examples below show customisation options for the [bold #1493FF]Tabs[/] widget.\n"
"• Press keys 1-6 on your keyboard to switch tabs, or click on a tab.",
)
)
for example in self.examples:
info = Info(example.description)
self.mount(Hr())
self.mount(info)
self.mount(example.widget)
app = BasicApp(css_path="tabs.scss", watch_css=True, log_path="textual.log")
app.run()

View File

@@ -1,9 +0,0 @@
$background: #021720;
App > View {
background: $background;
}
#info {
height: 4;
}

View File

@@ -1,33 +0,0 @@
from __future__ import annotations
from textual.app import App, ComposeResult
from textual.widgets import Static
TEXT = (
"I must not fear. Fear is the mind-killer. Fear is the little-death that "
"brings total obliteration. I will face my fear. I will permit it to pass over "
"me and through me. And when it has gone past, I will turn the inner eye to "
"see its path. Where the fear has gone there will be nothing. Only I will "
"remain. "
)
class TextAlign(App):
def compose(self) -> ComposeResult:
left = Static("[b]Left aligned[/]\n" + TEXT, id="one")
yield left
right = Static("[b]Center aligned[/]\n" + TEXT, id="two")
yield right
center = Static("[b]Right aligned[/]\n" + TEXT, id="three")
yield center
full = Static("[b]Fully justified[/]\n" + TEXT, id="four")
yield full
app = TextAlign(css_path="text_align.scss", watch_css=True)
if __name__ == "__main__":
app.run()

View File

@@ -1,24 +0,0 @@
#one {
text-align: left;
background: lightblue;
}
#two {
text-align: center;
background: indianred;
}
#three {
text-align: right;
background: palegreen;
}
#four {
text-align: justify;
background: palevioletred;
}
Static {
padding: 1;
}

View File

@@ -1,2 +0,0 @@
all:
poetry run textual run --dev focus_removal_tester.py

View File

@@ -1,45 +0,0 @@
"""Focus removal tester.
https://github.com/Textualize/textual/issues/939
"""
from textual.app import App
from textual.containers import Container
from textual.widgets import Static, Header, Footer, Button
class LeftButton(Button):
pass
class RightButton(Button):
pass
class NonFocusParent(Static):
def compose(self):
yield LeftButton("Do Not Press")
yield Static("Test")
yield RightButton("Really Do Not Press")
class FocusRemovalTester(App[None]):
BINDINGS = [("a", "add_widget", "Add Widget"), ("d", "del_widget", "Delete Widget")]
def compose(self):
yield Header()
yield Container()
yield Footer()
def action_add_widget(self):
self.query_one(Container).mount(NonFocusParent())
def action_del_widget(self):
candidates = self.query(NonFocusParent)
if candidates:
candidates.last().remove()
if __name__ == "__main__":
FocusRemovalTester().run()

View File

@@ -1,71 +0,0 @@
import random
from textual.containers import Horizontal, Vertical
from textual.app import App, ComposeResult
from textual.widgets import Button, Static
class Thing(Static):
def on_show(self) -> None:
self.scroll_visible()
class AddRemoveApp(App):
DEFAULT_CSS = """
#buttons {
dock: top;
height: auto;
}
#buttons Button {
width: 1fr;
}
#items {
height: 100%;
overflow-y: scroll;
}
Thing {
height: 5;
background: $panel;
border: tall $primary;
margin: 1 1;
content-align: center middle;
}
"""
def on_mount(self) -> None:
self.count = 0
def compose(self) -> ComposeResult:
yield Vertical(
Horizontal(
Button("Add", variant="success", id="add"),
Button("Remove", variant="error", id="remove"),
Button("Remove random", variant="warning", id="remove_random"),
id="buttons",
),
Vertical(id="items"),
)
def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "add":
self.count += 1
self.query("#items").first().mount(
Thing(f"Thing {self.count}", id=f"thing{self.count}")
)
elif event.button.id == "remove":
things = self.query("#items Thing")
if things:
things.last().remove()
elif event.button.id == "remove_random":
things = self.query("#items Thing")
if things:
random.choice(things).remove()
self.app.bell()
app = AddRemoveApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,14 +0,0 @@
Screen {
align: center middle;
}
Label {
width: 20;
height: 5;
background: blue;
color: white;
border: tall white;
margin: 1;
content-align: center middle;
}

View File

@@ -1,19 +0,0 @@
from textual.app import App, ComposeResult
from textual.widgets import Static
class Label(Static):
pass
class AlignApp(App):
CSS_PATH = "align.css"
def compose(self) -> ComposeResult:
yield Label("Hello")
yield Label("World!")
if __name__ == "__main__":
app = AlignApp()
app.run()

View File

@@ -1,265 +0,0 @@
/* CSS file for basic.py */
* {
transition: color 300ms linear, background 300ms linear;
}
Tweet.tall {
height: 24;
}
*:hover {
/* tint: 30% red;
/* outline: heavy red; */
}
App > Screen {
color: $text;
layers: base sidebar;
layout: vertical;
overflow: hidden;
}
#tree-container {
background: $panel;
overflow-y: auto;
height: 20;
margin: 1 2;
padding: 1 2;
}
DirectoryTree {
padding: 0 1;
height: auto;
}
#table-container {
background: $panel;
height: auto;
margin: 1 2;
}
DataTable {
/*border:heavy red;*/
/* tint: 10% green; */
/* text-opacity: 50%; */
margin: 1 2;
height: 24;
}
#sidebar {
background: $panel;
color: $text;
dock: left;
width: 30;
margin-bottom: 1;
offset-x: -100%;
transition: offset 500ms in_out_cubic;
layer: sidebar;
}
#sidebar.-active {
offset-x: 0;
}
#sidebar .title {
height: 1;
background: $primary-background-darken-1;
color: $text;
border-right: wide $background;
content-align: center middle;
}
#sidebar .user {
height: 8;
background: $panel-darken-1;
color: $text;
border-right: wide $background;
content-align: center middle;
}
#sidebar .content {
background: $panel-darken-2;
color: $text;
border-right: wide $background;
content-align: center middle;
}
Tweet {
height:12;
width: 100%;
margin: 0 2;
margin:0 2;
background: $panel;
color: $text;
layout: vertical;
/* border: outer $primary; */
padding: 1;
border: wide $panel;
/* scrollbar-gutter: stable; */
box-sizing: border-box;
}
.scrollable {
overflow-x: auto;
overflow-y: scroll;
padding: 0 2;
margin: 1 2;
height: 24;
layout: vertical;
}
.code {
height: auto;
}
TweetHeader {
height:1;
background: $accent;
color: $text;
}
TweetBody {
width: 100%;
background: $panel;
color: $text;
height: auto;
padding: 0 1 0 0;
}
Tweet.scroll-horizontal {
overflow-x: auto;
}
Tweet.scroll-horizontal TweetBody {
width: 350;
}
.button {
background: $accent;
color: $text;
width:20;
height: 3;
/* border-top: hidden $accent-darken-3; */
border: tall $accent-darken-2;
/* border-left: tall $accent-darken-1; */
/* padding: 1 0 0 0 ; */
transition: background 400ms in_out_cubic, color 400ms in_out_cubic;
}
.button:hover {
background: $accent-lighten-1;
color: $text;
width: 20;
height: 3;
border: tall $accent-darken-1;
/* border-left: tall $accent-darken-3; */
}
#footer {
color: $text;
background: $accent;
height: 1;
content-align: center middle;
dock:bottom;
}
#sidebar .content {
layout: vertical;
}
OptionItem {
height: 3;
background: $panel;
border-right: wide $background;
border-left: blank;
content-align: center middle;
}
OptionItem:hover {
height: 3;
color: $text;
background: $primary-darken-1;
/* border-top: hkey $accent2-darken-3;
border-bottom: hkey $accent2-darken-3; */
text-style: bold;
border-left: outer $secondary-darken-2;
}
Error {
width: 100%;
height:3;
background: $error;
color: $text;
border-top: tall $error-darken-2;
border-bottom: tall $error-darken-2;
padding: 0;
text-style: bold;
content-align: center middle;
}
Warning {
width: 100%;
height:3;
background: $warning;
color: $text;
border-top: tall $warning-darken-2;
border-bottom: tall $warning-darken-2;
text-style: bold;
content-align: center middle;
}
Success {
width: 100%;
height:auto;
box-sizing: border-box;
background: $success;
color: $text;
border-top: hkey $success-darken-2;
border-bottom: hkey $success-darken-2;
text-style: bold ;
content-align: center middle;
}
.horizontal {
layout: horizontal
}

View File

@@ -1,243 +0,0 @@
from rich.console import RenderableType
from rich.syntax import Syntax
from rich.text import Text
from textual.app import App, ComposeResult
from textual.reactive import Reactive
from textual.widget import Widget
from textual.widgets import Static, DataTable, DirectoryTree, Header, Footer
from textual.containers import Container, Vertical
CODE = '''
from __future__ import annotations
from typing import Iterable, TypeVar
T = TypeVar("T")
def loop_first(values: Iterable[T]) -> Iterable[tuple[bool, T]]:
"""Iterate and generate a tuple with a flag for first value."""
iter_values = iter(values)
try:
value = next(iter_values)
except StopIteration:
return
yield True, value
for value in iter_values:
yield False, value
def loop_last(values: Iterable[T]) -> Iterable[tuple[bool, T]]:
"""Iterate and generate a tuple with a flag for last value."""
iter_values = iter(values)
try:
previous_value = next(iter_values)
except StopIteration:
return
for value in iter_values:
yield False, previous_value
previous_value = value
yield True, previous_value
def loop_first_last(values: Iterable[T]) -> Iterable[tuple[bool, bool, T]]:
"""Iterate and generate a tuple with a flag for first and last value."""
iter_values = iter(values)
try:
previous_value = next(iter_values)
except StopIteration:
return
first = True
for value in iter_values:
yield first, False, previous_value
first = False
previous_value = value
yield first, True, previous_value
'''
lorem_short = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. In velit liber a a a, volutpat nec hendrerit at, faucibus in odio. Aliquam hendrerit nibh sed quam volutpat maximus. Nullam suscipit convallis lorem quis sodales. In tristique lobortis ante et dictum. Ut at finibus ipsum."""
lorem = (
lorem_short
+ """ In urna dolor, placerat et mi facilisis, congue sollicitudin massa. Phasellus felis turpis, cursus eu lectus et, porttitor malesuada augue. Sed feugiat volutpat velit, sollicitudin fringilla velit bibendum faucibus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In velit libero, volutpat nec hendrerit at, faucibus in odio. Aliquam hendrerit nibh sed quam volutpat maximus. Nullam suscipit convallis lorem quis sodales. In tristique lobortis ante et dictum. Ut at finibus ipsum. In urna dolor, placerat et mi facilisis, congue sollicitudin massa. Phasellus felis turpis, cursus eu lectus et, porttitor malesuada augue. Sed feugiat volutpat velit, sollicitudin fringilla velit bibendum faucibus. """
)
lorem_short_text = Text.from_markup(lorem_short)
lorem_long_text = Text.from_markup(lorem * 2)
class TweetHeader(Static):
def render(self) -> RenderableType:
return Text("Lorem Impsum", justify="center")
class TweetBody(Static):
short_lorem = Reactive(False)
def render(self) -> Text:
return lorem_short_text if self.short_lorem else lorem_long_text
class Tweet(Vertical):
pass
class OptionItem(Static):
def render(self) -> Text:
return Text("Option")
class Error(Static):
def render(self) -> Text:
return Text("This is an error message", justify="center")
class Warning(Static):
def render(self) -> Text:
return Text("This is a warning message", justify="center")
class Success(Static):
def render(self) -> Text:
return Text("This is a success message", justify="center")
class BasicApp(App):
"""A basic app demonstrating CSS"""
CSS_PATH = "basic.css"
def on_load(self):
"""Bind keys here."""
self.bind("s", "toggle_class('#sidebar', '-active')", description="Sidebar")
self.bind("d", "toggle_dark", description="Dark mode")
self.bind("q", "quit", description="Quit")
self.bind("f", "query_test", description="Query test")
def compose(self):
yield Header()
table = DataTable()
self.scroll_to_target = Tweet(TweetBody())
yield Vertical(
Tweet(TweetBody()),
Tweet(
Static(
Syntax(
CODE,
"python",
theme="ansi_dark",
line_numbers=True,
indent_guides=True,
),
classes="code",
),
classes="tall",
),
Container(table, id="table-container"),
Container(DirectoryTree("~/"), id="tree-container"),
Error(),
Tweet(TweetBody(), classes="scrollbar-size-custom"),
Warning(),
Tweet(TweetBody(), classes="scroll-horizontal"),
Success(),
Tweet(TweetBody(), classes="scroll-horizontal"),
Tweet(TweetBody(), classes="scroll-horizontal"),
Tweet(TweetBody(), classes="scroll-horizontal"),
Tweet(TweetBody(), classes="scroll-horizontal"),
Tweet(TweetBody(), classes="scroll-horizontal"),
)
yield Widget(
Static("Title", classes="title"),
Static("Content", classes="user"),
OptionItem(),
OptionItem(),
OptionItem(),
Static(classes="content"),
id="sidebar",
)
yield Footer()
table.add_column("Foo", width=20)
table.add_column("Bar", width=20)
table.add_column("Baz", width=20)
table.add_column("Foo", width=20)
table.add_column("Bar", width=20)
table.add_column("Baz", width=20)
table.zebra_stripes = True
for n in range(100):
table.add_row(*[f"Cell ([b]{n}[/b], {col})" for col in range(6)])
def on_mount(self):
self.sub_title = "Widget demo"
async def on_key(self, event) -> None:
await self.dispatch_key(event)
def action_toggle_dark(self):
self.dark = not self.dark
def action_query_test(self):
query = self.query("Tweet")
self.log(query)
self.log(query.nodes)
self.log(query)
self.log(query.nodes)
query.set_styles("outline: outer red;")
query = query.exclude(".scroll-horizontal")
self.log(query)
self.log(query.nodes)
# query = query.filter(".rubbish")
# self.log(query)
# self.log(query.first())
async def key_q(self):
await self.shutdown()
def key_x(self):
self.panic(self.tree)
def key_escape(self):
self.app.bell()
def key_t(self):
# Pressing "t" toggles the content of the TweetBody widget, from a long "Lorem ipsum..." to a shorter one.
tweet_body = self.query("TweetBody").first()
tweet_body.short_lorem = not tweet_body.short_lorem
def key_v(self):
self.get_child(id="content").scroll_to_widget(self.scroll_to_target)
def key_space(self):
self.bell()
app = BasicApp()
if __name__ == "__main__":
app.run()
# from textual.geometry import Region
# from textual.color import Color
# print(Region.intersection.cache_info())
# print(Region.overlaps.cache_info())
# print(Region.union.cache_info())
# print(Region.split_vertical.cache_info())
# print(Region.__contains__.cache_info())
# from textual.css.scalar import Scalar
# print(Scalar.resolve_dimension.cache_info())
# from rich.style import Style
# from rich.cells import cached_cell_len
# print(Style._add.cache_info())
# print(cached_cell_len.cache_info())

View File

@@ -1,54 +0,0 @@
from textual.app import App, ComposeResult
from textual.widgets import Footer, Static
class Focusable(Static, can_focus=True):
DEFAULT_CSS = """
Focusable {
background: blue 20%;
height: 1fr;
padding: 1;
}
Focusable:hover {
outline: solid white;
}
Focusable:focus {
background: red 20%;
}
"""
class Focusable1(Focusable):
BINDINGS = [
("a", "app.bell", "Ding"),
]
def render(self) -> str:
return repr(self)
class Focusable2(Focusable):
CSS = ""
BINDINGS = [
("b", "app.bell", "Beep"),
("f1", "app.quit", "QUIT"),
]
def render(self) -> str:
return repr(self)
class BindingApp(App):
BINDINGS = [("f1", "app.bell", "Bell")]
def compose(self) -> ComposeResult:
yield Focusable1()
yield Focusable2()
yield Footer()
app = BindingApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,28 +0,0 @@
Screen {
background: white;
color:black;
}
#box1 {
width: 10;
height: 5;
background: red 40%;
box-sizing: content-box;
}
#box2 {
width: 10;
height: 5;
padding: 1;
background:blue 40%;
box-sizing: content-box;
}
#box3 {
width: 10;
height: 5;
background:green 40%;
border: heavy;
box-sizing: content-box;
}

View File

@@ -1,13 +0,0 @@
from textual.app import App
from textual.widgets import Static
class BoxApp(App):
def compose(self):
yield Static("0123456789", id="box1")
yield Static("0123456789", id="box2")
yield Static("0123456789", id="box3")
app = BoxApp(css_path="box.css")
app.run()

View File

@@ -1,24 +0,0 @@
Button {
margin: 1;
width: 100%;
}
Vertical {
height: auto;
}
Horizontal {
height: auto;
}
Horizontal Button {
width: 20;
margin: 1 2 ;
}
#scroll {
height: 10;
}

View File

@@ -1,32 +0,0 @@
Screen {
overflow: auto;
}
#calculator {
layout: grid;
grid-size: 4;
grid-gutter: 1 2;
grid-columns: 1fr;
grid-rows: 2fr 1fr 1fr 1fr 1fr 1fr;
margin: 1 2;
min-height:25;
min-width: 26;
}
Button {
width: 100%;
height: 100%;
}
#numbers {
column-span: 4;
content-align: right middle;
padding: 0 1;
height: 100%;
background: $primary-lighten-2;
color: $text;
}
#number-0 {
column-span: 2;
}

View File

@@ -1,144 +0,0 @@
from decimal import Decimal
from textual.app import App, ComposeResult
from textual import events
from textual.containers import Container
from textual.reactive import Reactive
from textual.widgets import Button, Static
class CalculatorApp(App):
"""A working 'desktop' calculator."""
numbers = Reactive.var("0")
show_ac = Reactive.var(True)
left = Reactive.var(Decimal("0"))
right = Reactive.var(Decimal("0"))
value = Reactive.var("")
operator = Reactive.var("plus")
KEY_MAP = {
"+": "plus",
"-": "minus",
".": "point",
"*": "multiply",
"/": "divide",
"_": "plus-minus",
"%": "percent",
"=": "equals",
}
def watch_numbers(self, value: str) -> None:
"""Called when numbers is updated."""
# Update the Numbers widget
self.query_one("#numbers", Static).update(value)
def compute_show_ac(self) -> bool:
"""Compute switch to show AC or C button"""
return self.value in ("", "0") and self.numbers == "0"
def watch_show_ac(self, show_ac: bool) -> None:
"""Called when show_ac changes."""
self.query_one("#c").display = not show_ac
self.query_one("#ac").display = show_ac
def compose(self) -> ComposeResult:
"""Add our buttons."""
yield Container(
Static(id="numbers"),
Button("AC", id="ac", variant="primary"),
Button("C", id="c", variant="primary"),
Button("+/-", id="plus-minus", variant="primary"),
Button("%", id="percent", variant="primary"),
Button("÷", id="divide", variant="warning"),
Button("7", id="number-7"),
Button("8", id="number-8"),
Button("9", id="number-9"),
Button("×", id="multiply", variant="warning"),
Button("4", id="number-4"),
Button("5", id="number-5"),
Button("6", id="number-6"),
Button("-", id="minus", variant="warning"),
Button("1", id="number-1"),
Button("2", id="number-2"),
Button("3", id="number-3"),
Button("+", id="plus", variant="warning"),
Button("0", id="number-0"),
Button(".", id="point"),
Button("=", id="equals", variant="warning"),
id="calculator",
)
def on_key(self, event: events.Key) -> None:
"""Called when the user presses a key."""
print(f"KEY {event} was pressed!")
def press(button_id: str) -> None:
self.query_one(f"#{button_id}", Button).press()
self.set_focus(None)
key = event.key
if key.isdecimal():
press(f"number-{key}")
elif key == "c":
press("c")
press("ac")
elif key in self.KEY_MAP:
press(self.KEY_MAP[key])
def on_button_pressed(self, event: Button.Pressed) -> None:
"""Called when a button is pressed."""
button_id = event.button.id
assert button_id is not None
self.bell() # Terminal bell
def do_math() -> None:
"""Does the math: LEFT OPERATOR RIGHT"""
try:
if self.operator == "plus":
self.left += self.right
elif self.operator == "minus":
self.left -= self.right
elif self.operator == "divide":
self.left /= self.right
elif self.operator == "multiply":
self.left *= self.right
self.numbers = str(self.left)
self.value = ""
except Exception:
self.numbers = "Error"
if button_id.startswith("number-"):
number = button_id.partition("-")[-1]
self.numbers = self.value = self.value.lstrip("0") + number
elif button_id == "plus-minus":
self.numbers = self.value = str(Decimal(self.value or "0") * -1)
elif button_id == "percent":
self.numbers = self.value = str(Decimal(self.value or "0") / Decimal(100))
elif button_id == "point":
if "." not in self.value:
self.numbers = self.value = (self.value or "0") + "."
elif button_id == "ac":
self.value = ""
self.left = self.right = Decimal(0)
self.operator = "plus"
self.numbers = "0"
elif button_id == "c":
self.value = ""
self.numbers = "0"
elif button_id in ("plus", "minus", "divide", "multiply"):
self.right = Decimal(self.value or "0")
do_math()
self.operator = button_id
elif button_id == "equals":
if self.value:
self.right = Decimal(self.value)
do_math()
app = CalculatorApp(css_path="calculator.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,28 +0,0 @@
from textual.app import App
from textual.widgets import Static
class CenterApp(App):
DEFAULT_CSS = """
CenterApp Screen {
layout: center;
overflow: auto auto;
}
CenterApp Static {
border: wide $primary;
background: $panel;
width: 50;
height: 20;
margin: 1 2;
content-align: center middle;
}
"""
def compose(self):
yield Static("Hello World!")
app = CenterApp()

View File

@@ -1,55 +0,0 @@
from textual.app import App
from textual.containers import Vertical, Center
from textual.widgets import Static
class CenterApp(App):
DEFAULT_CSS = """
#sidebar {
dock: left;
width: 32;
height: 100%;
border-right: vkey $primary;
}
#bottombar {
dock: bottom;
height: 12;
width: 100%;
border-top: hkey $primary;
}
#hello {
border: wide $primary;
width: 40;
height: 16;
margin: 2 4;
}
#sidebar.hidden {
width: 0;
}
Static {
background: $panel;
color: $text;
content-align: center middle;
}
"""
def on_mount(self) -> None:
self.bind("t", "toggle_class('#sidebar', 'hidden')")
def compose(self):
yield Static("Sidebar", id="sidebar")
yield Vertical(
Static("Bottom bar", id="bottombar"),
Center(
Static("Hello World!", id="hello"),
),
)
app = CenterApp()

View File

@@ -1,10 +0,0 @@
Screen {
align: center middle;
}
Container {
width: 50;
height: 15;
background: $boost;
align: center middle;
}

View File

@@ -1,21 +0,0 @@
from textual.app import App, ComposeResult
from textual.containers import Container
from textual.widgets import Checkbox, Footer
class CheckboxApp(App):
BINDINGS = [("s", "switch", "Press switch"), ("d", "toggle_dark", "Dark mode")]
def compose(self) -> ComposeResult:
yield Footer()
yield Container(Checkbox())
def action_switch(self) -> None:
checkbox = self.query_one(Checkbox)
checkbox.value = not checkbox.value
app = CheckboxApp(css_path="check.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,23 +0,0 @@
Screen {
background: $surface;
}
Container {
height: auto;
background: $boost;
}
Panel {
height: auto;
background: $boost;
margin: 1 2;
}
Content {
background: $boost;
padding: 1 2;
margin: 1 2;
color: auto 95%;
}

View File

@@ -1,35 +0,0 @@
from textual.app import App
from textual.containers import Container
from textual.widgets import Header, Footer, Static
class Content(Static):
pass
class Panel(Container):
pass
class Panel2(Container):
pass
class DesignApp(App):
BINDINGS = [("d", "toggle_dark", "Toggle dark mode")]
def compose(self):
yield Header()
yield Footer()
yield Container(
Content("content"),
Panel(
Content("more content"),
Content("more content"),
),
)
app = DesignApp(css_path="design.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,52 +0,0 @@
from textual.app import App, ComposeResult
from textual.widgets import Static
class DockApp(App):
def compose(self) -> ComposeResult:
self.screen.styles.layers = "base sidebar"
header = Static("Header", id="header")
header.styles.dock = "top"
header.styles.height = "3"
header.styles.background = "blue"
header.styles.color = "white"
header.styles.margin = 0
header.styles.align_horizontal = "center"
# header.styles.layer = "base"
header.styles.box_sizing = "border-box"
yield header
footer = Static("Footer")
footer.styles.dock = "bottom"
footer.styles.height = 1
footer.styles.background = "green"
footer.styles.color = "white"
yield footer
sidebar = Static("Sidebar", id="sidebar")
sidebar.styles.dock = "right"
sidebar.styles.width = 20
sidebar.styles.height = "100%"
sidebar.styles.background = "magenta"
# sidebar.styles.layer = "sidebar"
yield sidebar
for n, color in zip(range(5), ["red", "green", "blue", "yellow", "magenta"]):
thing = Static(f"Thing {n}", id=f"#thing{n}")
thing.styles.border = ("heavy", "rgba(0,0,0,0.2)")
thing.styles.background = f"{color} 20%"
thing.styles.height = 15
yield thing
app = DockApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,36 +0,0 @@
from textual.app import App, ComposeResult
from textual.widgets import Static
class OrderApp(App):
CSS = """
Screen {
layout: center;
}
Static {
border: heavy white;
}
#one {
background: red;
width:20;
height: 30;
dock:left;
}
#two {
background: blue;
width:30;
height: 20;
dock:left;
}
"""
def compose(self) -> ComposeResult:
yield Static("One", id="one")
yield Static("Two", id="two")
app = OrderApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,8 +0,0 @@
App Static {
border: heavy white;
background: blue;
color: white;
height: 100%;
box-sizing: border-box;
}

View File

@@ -1,10 +0,0 @@
from textual.app import App, ComposeResult
from textual.widgets import Static
class FillApp(App):
def compose(self) -> ComposeResult:
yield Static("Hello")
app = FillApp(css_path="fill.css")

View File

@@ -1,17 +0,0 @@
from textual.app import App
from textual.widgets import Header, Footer
class FooterApp(App):
def on_mount(self):
self.sub_title = "Header and footer example"
self.bind("b", "app.bell", description="Play the Bell")
self.bind("d", "dark", description="Toggle dark")
self.bind("f1", "app.bell", description="Hello World")
def action_dark(self):
self.dark = not self.dark
def compose(self):
yield Header()
yield Footer()

View File

@@ -1,19 +0,0 @@
from textual.app import App
from textual.widgets import Input
class InputApp(App):
CSS = """
Input {
width: 20;
}
"""
def compose(self):
yield Input("你123456789界", placeholder="Type something")
if __name__ == "__main__":
app = InputApp()
app.run()

View File

@@ -1,24 +0,0 @@
Screen {
background: lightcoral;
}
#left_pane {
background: red;
width: 30;
height: auto;
}
#middle_pane {
background: green;
width: 140;
}
#right_pane {
background: blue;
width: 30;
}
.box {
height: 5;
width: 15;
}

View File

@@ -1,43 +0,0 @@
from __future__ import annotations
from rich.console import RenderableType
from rich.panel import Panel
from textual import events
from textual.app import App, ComposeResult
from textual.containers import Container, Horizontal, Vertical
from textual.widget import Widget
class Box(Widget, can_focus=True):
DEFAULT_CSS = "#box {background: blue;}"
def render(self) -> RenderableType:
return Panel("Box")
class JustABox(App):
def compose(self) -> ComposeResult:
# yield Container(Box(classes="box"))
yield Horizontal(
Vertical(
Box(id="box1", classes="box"),
Box(id="box2", classes="box"),
id="left_pane",
),
id="horizontal",
)
def key_p(self):
for k, v in self.app.stylesheet.source.items():
print(k)
print(self.query_one("#horizontal").styles.layout)
async def on_key(self, event: events.Key) -> None:
await self.dispatch_key(event)
app = JustABox(css_path="just_a_box.css", watch_css=True)
if __name__ == "__main__":
app.run()

View File

@@ -1,39 +0,0 @@
from textual.app import App, ComposeResult
from textual.widgets import Static
class OrderApp(App):
CSS = """
Screen {
layout: center;
}
Static {
border: heavy white;
}
#one {
background: red;
width:20;
height: 30;
}
#two {
background: blue;
width:30;
height: 20;
}
#three {
background: green;
width:40;
height:10
}
"""
def compose(self) -> ComposeResult:
yield Static("One", id="one")
yield Static("Two", id="two")
yield Static("Three", id="three")
app = OrderApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,35 +0,0 @@
from textual.app import App, ComposeResult
from textual.containers import Container
from textual.widget import Widget
from textual.widgets import Static
class MountWidget(Widget):
def on_mount(self) -> None:
print("Widget mounted")
class MountContainer(Container):
def compose(self) -> ComposeResult:
yield Container(MountWidget(id="bar"))
def on_mount(self) -> None:
bar = self.query_one("#bar")
print("MountContainer got", bar)
class MountApp(App):
def compose(self) -> ComposeResult:
yield MountContainer(id="foo")
def on_mount(self) -> None:
foo = self.query_one("#foo")
print("foo is", foo)
static = self.query_one("#bar")
print("App got", static)
if __name__ == "__main__":
app = MountApp()
app.run()

View File

@@ -1,21 +0,0 @@
Screen {
align: center middle;
}
#parent {
width: 32;
height: 8;
background: $panel;
}
#tag {
color: $text;
background: $success;
padding: 2 4;
width: auto;
offset: -8 -4;
}
#child {
background: red;
}

View File

@@ -1,14 +0,0 @@
from textual.app import App, ComposeResult
from textual.containers import Vertical
from textual.widgets import Static
class OffsetExample(App):
def compose(self) -> ComposeResult:
yield Vertical(Static("Child", id="child"), id="parent")
yield Static("Tag", id="tag")
app = OffsetExample(css_path="offset.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,27 +0,0 @@
import asyncio
from textual.app import App
from textual import events
from textual.widget import Widget
class OrderWidget(Widget, can_focus=True):
def on_key(self, event) -> None:
self.log("PRESS", event.key)
class OrderApp(App):
def compose(self):
yield OrderWidget()
async def on_mount(self):
async def send_keys():
self.query_one(OrderWidget).focus()
chars = ["tab", "enter", "h", "e", "l", "l", "o"]
for char in chars:
self.log("SENDING", char)
await self.post_message(events.Key(self, key=char))
self.set_timer(1, lambda: asyncio.create_task(send_keys()))
app = OrderApp()

View File

@@ -1,24 +0,0 @@
from textual.app import App, ComposeResult
from textual.screen import Screen
from textual.widgets import Footer
class DefaultScreen(Screen):
BINDINGS = [("f", "foo", "FOO")]
def compose(self) -> ComposeResult:
yield Footer()
def action_foo(self) -> None:
self.app.bell()
class ScreenApp(App):
def on_mount(self) -> None:
self.push_screen(DefaultScreen())
app = ScreenApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,69 +0,0 @@
from textual.app import App, Screen, ComposeResult
from textual.widgets import Static, Footer, Pretty
class ModalScreen(Screen):
def compose(self) -> ComposeResult:
yield Pretty(self.app.screen_stack)
yield Footer()
def on_mount(self) -> None:
pretty = self.query_one("Pretty")
def on_screen_resume(self):
self.query_one(Pretty).update(self.app.screen_stack)
class NewScreen(Screen):
def compose(self):
yield Pretty(self.app.screen_stack)
yield Footer()
def on_screen_resume(self):
self.query_one(Pretty).update(self.app.screen_stack)
class ScreenApp(App):
DEFAULT_CSS = """
ScreenApp Screen {
background: #111144;
color: white;
}
ScreenApp ModalScreen {
background: #114411;
color: white;
}
ScreenApp Pretty {
height: auto;
content-align: center middle;
background: white 20%;
}
"""
def compose(self) -> ComposeResult:
yield Static("On Screen 1")
yield Footer()
def on_mount(self) -> None:
self.install_screen(NewScreen("Screen1"), name="1")
self.install_screen(NewScreen("Screen2"), name="2")
self.install_screen(NewScreen("Screen3"), name="3")
self.bind("1", "switch_screen('1')", description="Screen 1")
self.bind("2", "switch_screen('2')", description="Screen 2")
self.bind("3", "switch_screen('3')", description="Screen 3")
self.bind("s", "modal_screen", description="add screen")
self.bind("escape", "back", description="Go back")
def action_modal_screen(self) -> None:
self.push_screen(ModalScreen())
app = ScreenApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,9 +0,0 @@
Focusable {
padding: 3 6;
background: blue 20%;
}
Focusable :focus {
border: solid red;
}

View File

@@ -1,20 +0,0 @@
from textual.app import App, ComposeResult
from textual.widgets import Static, Footer
class Focusable(Static, can_focus=True):
pass
class ScreensFocusApp(App):
def compose(self) -> ComposeResult:
yield Focusable("App - one")
yield Focusable("App - two")
yield Focusable("App - three")
yield Focusable("App - four")
yield Footer()
app = ScreensFocusApp(css_path="screens_focus.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,9 +0,0 @@
Screen {
align: center middle;
}
#test {
border: solid white;
background: blue;
}

View File

@@ -1,19 +0,0 @@
from textual.app import App, ComposeResult
from textual.containers import Container
from textual.widgets import Header, Footer
class ScrollApp(App):
BINDINGS = [("q", "quit", "QUIT")]
CSS_PATH = "scrollbug.css"
def compose(self) -> ComposeResult:
yield Header()
yield Container(id="test")
yield Footer()
app = ScrollApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,32 +0,0 @@
from rich.text import Text
from textual.app import App, ComposeResult
from textual.widgets import Static
text = "\n".join("FOO BAR bazz etc sdfsdf " * 20 for n in range(1000))
class Content(Static):
DEFAULT_CSS = """
Content {
width: auto;
}
"""
def render(self):
return Text(text, no_wrap=False)
class ScrollApp(App):
CSS = """
Screen {
overflow: auto;
}
"""
def compose(self) -> ComposeResult:
yield Content()
app = ScrollApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,13 +0,0 @@
Screen {
overflow: auto;
}
Static {
background: blue 20%;
height: 100%;
margin: 2 4;
min-width: 80;
min-height: 40;
}

View File

@@ -1,15 +0,0 @@
from textual.app import App
from textual.widgets import Static
class Clickable(Static):
def on_click(self):
self.app.bell()
class SpacingApp(App):
def compose(self):
yield Static(id="2332")
app = SpacingApp(css_path="spacing.css")

View File

@@ -1,85 +0,0 @@
from textual.app import App, ComposeResult
from textual.widgets import DataTable
from rich.syntax import Syntax
from rich.table import Table
CODE = '''\
def loop_first_last(values: Iterable[T]) -> Iterable[tuple[bool, bool, T]]:
"""Iterate and generate a tuple with a flag for first and last value."""
iter_values = iter(values)
try:
previous_value = next(iter_values)
except StopIteration:
return
first = True
for value in iter_values:
yield first, False, previous_value
first = False
previous_value = value
yield first, True, previous_value'''
test_table = Table(title="Star Wars Movies")
test_table.add_column("Released", style="cyan", no_wrap=True)
test_table.add_column("Title", style="magenta")
test_table.add_column("Box Office", justify="right", style="green")
test_table.add_row("Dec 20, 2019", "Star Wars: The Rise of Skywalker", "$952,110,690")
test_table.add_row("May 25, 2018", "Solo: A Star Wars Story", "$393,151,347")
test_table.add_row(
"Dec 15, 2017", "Star Wars Ep. V111: The Last Jedi", "$1,332,539,889"
)
test_table.add_row("Dec 16, 2016", "Rogue One: A Star Wars Story", "$1,332,439,889")
class TableApp(App):
def compose(self) -> ComposeResult:
table = self.table = DataTable(id="data")
yield table
table.add_column("Foo")
table.add_column("Bar")
table.add_column("Baz")
table.add_column("Foo")
table.add_column("Bar")
table.add_column("Baz")
for n in range(200):
height = 1
row = [f"row [b]{n}[/b] col [i]{c}[/i]" for c in range(6)]
if n == 10:
row[1] = Syntax(
CODE,
"python",
theme="ansi_dark",
line_numbers=True,
indent_guides=True,
)
height = 13
if n == 30:
row[1] = test_table
height = 13
table.add_row(*row, height=height)
table.focus()
def on_mount(self):
self.bind("d", "toggle_dark")
self.bind("z", "toggle_zebra")
self.bind("x", "exit")
def action_toggle_dark(self) -> None:
self.app.dark = not self.app.dark
def action_toggle_zebra(self) -> None:
self.table.zebra_stripes = not self.table.zebra_stripes
def action_exit(self) -> None:
pass
app = TableApp()
if __name__ == "__main__":
print(app.run())

View File

@@ -1,23 +0,0 @@
Screen {
layout: grid;
grid-columns: 2fr 1fr 1fr;
grid-rows: 1fr 1fr;
grid-gutter: 1 2;
}
Static {
border: solid white;
background: blue 20%;
height: 100%;
width: 100%;
}
#foo {
row-span: 2;
}
#last {
column-span: 3;
margin: 1;
}

View File

@@ -1,19 +0,0 @@
from textual.app import App
from textual.widgets import Static
class TableLayoutApp(App):
def compose(self):
yield Static("foo", id="foo")
yield Static("bar")
yield Static("baz")
yield Static("foo")
yield Static("bar")
yield Static("baz", id="last")
app = TableLayoutApp(css_path="table_layout.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,16 +0,0 @@
from textual.app import App
from textual.containers import Container
from textual.widgets import DirectoryTree
class TreeApp(App):
def compose(self):
tree = DirectoryTree("~/projects")
yield Container(tree)
tree.focus()
app = TreeApp()
if __name__ == "__main__":
app.run()