Module textual.cli.previews.colors
Expand source code
from textual.app import App, ComposeResult
from textual.containers import Horizontal, Vertical
from textual.design import ColorSystem
from textual.widget import Widget
from textual.widgets import Button, Footer, Static
class ColorButtons(Vertical):
def compose(self) -> ComposeResult:
for border in ColorSystem.COLOR_NAMES:
if border:
yield Button(border, id=border)
class ColorBar(Static):
pass
class ColorItem(Horizontal):
pass
class ColorGroup(Vertical):
pass
class Content(Vertical):
pass
class ColorLabel(Static):
pass
class ColorsView(Vertical):
def compose(self) -> ComposeResult:
LEVELS = [
"darken-3",
"darken-2",
"darken-1",
"",
"lighten-1",
"lighten-2",
"lighten-3",
]
for color_name in ColorSystem.COLOR_NAMES:
items: list[Widget] = [ColorLabel(f'"{color_name}"')]
for level in LEVELS:
color = f"{color_name}-{level}" if level else color_name
item = ColorItem(
ColorBar(f"${color}", classes="text label"),
ColorBar(f"$text-muted", classes="muted"),
ColorBar(f"$text-disabled", classes="disabled"),
classes=color,
)
items.append(item)
yield ColorGroup(*items, id=f"group-{color_name}")
class ColorsApp(App):
CSS_PATH = "colors.css"
BINDINGS = [("d", "toggle_dark", "Toggle dark mode")]
def compose(self) -> ComposeResult:
yield Content(ColorButtons())
yield Footer()
def on_mount(self) -> None:
self.call_later(self.update_view)
def update_view(self) -> None:
content = self.query_one("Content", Content)
content.mount(ColorsView())
def on_button_pressed(self, event: Button.Pressed) -> None:
self.bell()
self.query(ColorGroup).remove_class("-active")
group = self.query_one(f"#group-{event.button.id}", ColorGroup)
group.add_class("-active")
group.scroll_visible(top=True, speed=150)
app = ColorsApp()
if __name__ == "__main__":
app.run()
Classes
class ColorBar (renderable: RenderableType = '', *, expand: bool = False, shrink: bool = False, markup: bool = True, name: str | None = None, id: str | None = None, classes: str | None = None)
-
A widget to display simple static content, or use as a base class for more complex widgets.
Args
renderable
:RenderableType
, optional- A Rich renderable, or string containing console markup. Defaults to "".
expand
:bool
, optional- Expand content if required to fill container. Defaults to False.
shrink
:bool
, optional- Shrink content if required to fill container. Defaults to False.
markup
:bool
, optional- True if markup should be parsed and rendered. Defaults to True.
name (str | None, optional): Name of widget. Defaults to None. id (str | None, optional): ID of Widget. Defaults to None. classes (str | None, optional): Space separated list of class names. Defaults to None.
Expand source code
class ColorBar(Static): pass
Ancestors
- textual.widgets._static.Static
- Widget
- DOMNode
- MessagePump
Inherited members
Widget
:action
add_class
allow_horizontal_scroll
allow_vertical_scroll
ancestors
animate
app
auto_height
auto_links
auto_width
background_colors
call_later
can_focus
can_focus_children
capture_mouse
check_idle
classes
colors
compose
container_size
container_viewport
content_offset
content_region
content_size
css_identifier
css_identifier_styled
css_path_nodes
disable_messages
dispatch_key
display
displayed_children
emit
emit_no_wait
enable_messages
expand
focus
focusable_children
get_child
get_component_rich_style
get_component_styles
get_content_height
get_content_width
get_default_css
get_pseudo_classes
get_style_at
gutter
has_class
has_focus
has_pseudo_class
highlight_link_id
horizontal_scrollbar
hover_style
id
is_container
is_scrollable
is_transparent
layer
layers
link_hover_style
link_style
log
max_scroll_x
max_scroll_y
mount
mouse_over
offset
on_event
outer_size
parent
post_message
post_message_no_wait
post_render
pseudo_classes
query
query_one
refresh
region
release_mouse
remove
remove_class
render
render_line
render_lines
reset_focus
reset_styles
rich_style
screen
scroll_down
scroll_end
scroll_home
scroll_left
scroll_offset
scroll_page_down
scroll_page_left
scroll_page_right
scroll_page_up
scroll_relative
scroll_right
scroll_target_x
scroll_target_y
scroll_to
scroll_to_region
scroll_to_widget
scroll_up
scroll_visible
scroll_x
scroll_y
scrollbar_corner
scrollbar_gutter
scrollbar_size_horizontal
scrollbar_size_vertical
scrollbars_enabled
set_class
set_interval
set_styles
set_timer
show_horizontal_scrollbar
show_vertical_scrollbar
shrink
siblings
size
text_style
toggle_class
tree
vertical_scrollbar
virtual_region
virtual_region_with_margin
virtual_size
visible
visible_siblings
walk_children
watch_has_focus
watch_mouse_over
window_region
class ColorButtons (*children: Widget, name: str | None = None, id: str | None = None, classes: str | None = None)
-
A container widget which aligns children vertically.
Expand source code
class ColorButtons(Vertical): def compose(self) -> ComposeResult: for border in ColorSystem.COLOR_NAMES: if border: yield Button(border, id=border)
Ancestors
Class variables
var COMPONENT_CLASSES : ClassVar[set[str]]
Inherited members
Vertical
:Vertical
:action
add_class
allow_horizontal_scroll
allow_vertical_scroll
ancestors
animate
app
auto_height
auto_links
auto_width
background_colors
call_later
capture_mouse
check_idle
classes
colors
container_size
container_viewport
content_offset
content_region
content_size
css_identifier
css_identifier_styled
css_path_nodes
disable_messages
dispatch_key
display
displayed_children
emit
emit_no_wait
enable_messages
expand
focus
focusable_children
get_child
get_component_rich_style
get_component_styles
get_content_height
get_content_width
get_default_css
get_pseudo_classes
get_style_at
gutter
has_class
has_focus
has_pseudo_class
horizontal_scrollbar
id
is_container
is_scrollable
is_transparent
layer
layers
link_hover_style
link_style
log
max_scroll_x
max_scroll_y
mount
mouse_over
offset
on_event
outer_size
parent
post_message
post_message_no_wait
post_render
pseudo_classes
query
query_one
refresh
region
release_mouse
remove
remove_class
render
render_line
render_lines
reset_focus
reset_styles
rich_style
screen
scroll_down
scroll_end
scroll_home
scroll_left
scroll_offset
scroll_page_down
scroll_page_left
scroll_page_right
scroll_page_up
scroll_relative
scroll_right
scroll_target_x
scroll_target_y
scroll_to
scroll_to_region
scroll_to_widget
scroll_up
scroll_visible
scroll_x
scroll_y
scrollbar_corner
scrollbar_gutter
scrollbar_size_horizontal
scrollbar_size_vertical
scrollbars_enabled
set_class
set_interval
set_styles
set_timer
show_horizontal_scrollbar
show_vertical_scrollbar
shrink
siblings
size
text_style
toggle_class
tree
vertical_scrollbar
virtual_region
virtual_region_with_margin
virtual_size
visible
visible_siblings
walk_children
watch_has_focus
watch_mouse_over
window_region
Widget
:
class ColorGroup (*children: Widget, name: str | None = None, id: str | None = None, classes: str | None = None)
-
A container widget which aligns children vertically.
Expand source code
class ColorGroup(Vertical): pass
Ancestors
Class variables
var COMPONENT_CLASSES : ClassVar[set[str]]
Inherited members
Vertical
:Vertical
:action
add_class
allow_horizontal_scroll
allow_vertical_scroll
ancestors
animate
app
auto_height
auto_links
auto_width
background_colors
call_later
capture_mouse
check_idle
classes
colors
compose
container_size
container_viewport
content_offset
content_region
content_size
css_identifier
css_identifier_styled
css_path_nodes
disable_messages
dispatch_key
display
displayed_children
emit
emit_no_wait
enable_messages
expand
focus
focusable_children
get_child
get_component_rich_style
get_component_styles
get_content_height
get_content_width
get_default_css
get_pseudo_classes
get_style_at
gutter
has_class
has_focus
has_pseudo_class
horizontal_scrollbar
id
is_container
is_scrollable
is_transparent
layer
layers
link_hover_style
link_style
log
max_scroll_x
max_scroll_y
mount
mouse_over
offset
on_event
outer_size
parent
post_message
post_message_no_wait
post_render
pseudo_classes
query
query_one
refresh
region
release_mouse
remove
remove_class
render
render_line
render_lines
reset_focus
reset_styles
rich_style
screen
scroll_down
scroll_end
scroll_home
scroll_left
scroll_offset
scroll_page_down
scroll_page_left
scroll_page_right
scroll_page_up
scroll_relative
scroll_right
scroll_target_x
scroll_target_y
scroll_to
scroll_to_region
scroll_to_widget
scroll_up
scroll_visible
scroll_x
scroll_y
scrollbar_corner
scrollbar_gutter
scrollbar_size_horizontal
scrollbar_size_vertical
scrollbars_enabled
set_class
set_interval
set_styles
set_timer
show_horizontal_scrollbar
show_vertical_scrollbar
shrink
siblings
size
text_style
toggle_class
tree
vertical_scrollbar
virtual_region
virtual_region_with_margin
virtual_size
visible
visible_siblings
walk_children
watch_has_focus
watch_mouse_over
window_region
Widget
:
class ColorItem (*children: Widget, name: str | None = None, id: str | None = None, classes: str | None = None)
-
A container widget which aligns children horizontally.
Expand source code
class ColorItem(Horizontal): pass
Ancestors
Class variables
var COMPONENT_CLASSES : ClassVar[set[str]]
Inherited members
Horizontal
:Horizontal
:action
add_class
allow_horizontal_scroll
allow_vertical_scroll
ancestors
animate
app
auto_height
auto_links
auto_width
background_colors
call_later
capture_mouse
check_idle
classes
colors
compose
container_size
container_viewport
content_offset
content_region
content_size
css_identifier
css_identifier_styled
css_path_nodes
disable_messages
dispatch_key
display
displayed_children
emit
emit_no_wait
enable_messages
expand
focus
focusable_children
get_child
get_component_rich_style
get_component_styles
get_content_height
get_content_width
get_default_css
get_pseudo_classes
get_style_at
gutter
has_class
has_focus
has_pseudo_class
horizontal_scrollbar
id
is_container
is_scrollable
is_transparent
layer
layers
link_hover_style
link_style
log
max_scroll_x
max_scroll_y
mount
mouse_over
offset
on_event
outer_size
parent
post_message
post_message_no_wait
post_render
pseudo_classes
query
query_one
refresh
region
release_mouse
remove
remove_class
render
render_line
render_lines
reset_focus
reset_styles
rich_style
screen
scroll_down
scroll_end
scroll_home
scroll_left
scroll_offset
scroll_page_down
scroll_page_left
scroll_page_right
scroll_page_up
scroll_relative
scroll_right
scroll_target_x
scroll_target_y
scroll_to
scroll_to_region
scroll_to_widget
scroll_up
scroll_visible
scroll_x
scroll_y
scrollbar_corner
scrollbar_gutter
scrollbar_size_horizontal
scrollbar_size_vertical
scrollbars_enabled
set_class
set_interval
set_styles
set_timer
show_horizontal_scrollbar
show_vertical_scrollbar
shrink
siblings
size
text_style
toggle_class
tree
vertical_scrollbar
virtual_region
virtual_region_with_margin
virtual_size
visible
visible_siblings
walk_children
watch_has_focus
watch_mouse_over
window_region
Widget
:
class ColorLabel (renderable: RenderableType = '', *, expand: bool = False, shrink: bool = False, markup: bool = True, name: str | None = None, id: str | None = None, classes: str | None = None)
-
A widget to display simple static content, or use as a base class for more complex widgets.
Args
renderable
:RenderableType
, optional- A Rich renderable, or string containing console markup. Defaults to "".
expand
:bool
, optional- Expand content if required to fill container. Defaults to False.
shrink
:bool
, optional- Shrink content if required to fill container. Defaults to False.
markup
:bool
, optional- True if markup should be parsed and rendered. Defaults to True.
name (str | None, optional): Name of widget. Defaults to None. id (str | None, optional): ID of Widget. Defaults to None. classes (str | None, optional): Space separated list of class names. Defaults to None.
Expand source code
class ColorLabel(Static): pass
Ancestors
- textual.widgets._static.Static
- Widget
- DOMNode
- MessagePump
Inherited members
Widget
:action
add_class
allow_horizontal_scroll
allow_vertical_scroll
ancestors
animate
app
auto_height
auto_links
auto_width
background_colors
call_later
can_focus
can_focus_children
capture_mouse
check_idle
classes
colors
compose
container_size
container_viewport
content_offset
content_region
content_size
css_identifier
css_identifier_styled
css_path_nodes
disable_messages
dispatch_key
display
displayed_children
emit
emit_no_wait
enable_messages
expand
focus
focusable_children
get_child
get_component_rich_style
get_component_styles
get_content_height
get_content_width
get_default_css
get_pseudo_classes
get_style_at
gutter
has_class
has_focus
has_pseudo_class
highlight_link_id
horizontal_scrollbar
hover_style
id
is_container
is_scrollable
is_transparent
layer
layers
link_hover_style
link_style
log
max_scroll_x
max_scroll_y
mount
mouse_over
offset
on_event
outer_size
parent
post_message
post_message_no_wait
post_render
pseudo_classes
query
query_one
refresh
region
release_mouse
remove
remove_class
render
render_line
render_lines
reset_focus
reset_styles
rich_style
screen
scroll_down
scroll_end
scroll_home
scroll_left
scroll_offset
scroll_page_down
scroll_page_left
scroll_page_right
scroll_page_up
scroll_relative
scroll_right
scroll_target_x
scroll_target_y
scroll_to
scroll_to_region
scroll_to_widget
scroll_up
scroll_visible
scroll_x
scroll_y
scrollbar_corner
scrollbar_gutter
scrollbar_size_horizontal
scrollbar_size_vertical
scrollbars_enabled
set_class
set_interval
set_styles
set_timer
show_horizontal_scrollbar
show_vertical_scrollbar
shrink
siblings
size
text_style
toggle_class
tree
vertical_scrollbar
virtual_region
virtual_region_with_margin
virtual_size
visible
visible_siblings
walk_children
watch_has_focus
watch_mouse_over
window_region
class ColorsApp (driver_class: Type[Driver] | None = None, css_path: CSSPathType = None, watch_css: bool = False)
-
The base class for Textual Applications.
Args
- driver_class (Type[Driver] | None, optional): Driver class or
None
to auto-detect. Defaults to None. - title (str | None, optional): Title of the application. If
None
, the title is set to the name of theApp
subclass. Defaults toNone
. - css_path (str | PurePath | None, optional): Path to CSS or
None
for no CSS file. Defaults to None. watch_css
:bool
, optional- Watch CSS for changes. Defaults to False.
Expand source code
class ColorsApp(App): CSS_PATH = "colors.css" BINDINGS = [("d", "toggle_dark", "Toggle dark mode")] def compose(self) -> ComposeResult: yield Content(ColorButtons()) yield Footer() def on_mount(self) -> None: self.call_later(self.update_view) def update_view(self) -> None: content = self.query_one("Content", Content) content.mount(ColorsView()) def on_button_pressed(self, event: Button.Pressed) -> None: self.bell() self.query(ColorGroup).remove_class("-active") group = self.query_one(f"#group-{event.button.id}", ColorGroup) group.add_class("-active") group.scroll_visible(top=True, speed=150)
Ancestors
- App
- typing.Generic
- DOMNode
- MessagePump
Class variables
var BINDINGS
var CSS_PATH : CSSPathType
var SCREENS : dict[str, Screen]
var SUB_TITLE : str | None
var TITLE : str | None
Methods
-
Expand source code
def on_button_pressed(self, event: Button.Pressed) -> None: self.bell() self.query(ColorGroup).remove_class("-active") group = self.query_one(f"#group-{event.button.id}", ColorGroup) group.add_class("-active") group.scroll_visible(top=True, speed=150)
def on_mount(self) ‑> None
-
Expand source code
def on_mount(self) -> None: self.call_later(self.update_view)
def update_view(self) ‑> None
-
Expand source code
def update_view(self) -> None: content = self.query_one("Content", Content) content.mount(ColorsView())
Inherited members
App
:action
action_bell
action_focus
action_pop_screen
action_push_screen
action_quit
action_screenshot
action_switch_screen
action_toggle_dark
add_class
ancestors
animate
app
background_colors
bell
bind
call_later
capture_mouse
check_bindings
check_idle
classes
colors
compose
css_identifier
css_identifier_styled
css_path_nodes
dark
debug
disable_messages
dispatch_key
display
displayed_children
emit
emit_no_wait
enable_messages
exit
export_screenshot
fatal_error
focused
get_child
get_component_styles
get_css_variables
get_default_css
get_driver_class
get_pseudo_classes
get_screen
get_widget_at
has_class
has_pseudo_class
id
install_screen
is_headless
is_mounted
is_screen_installed
log
mount
mount_all
namespace_bindings
on_event
panic
parent
pop_screen
post_message
post_message_no_wait
pseudo_classes
push_screen
query
query_one
refresh_css
remove_class
reset_styles
rich_style
run
save_screenshot
screen
screen_stack
set_class
set_focus
set_interval
set_styles
set_timer
size
sub_title
switch_screen
text_style
title
toggle_class
tree
uninstall_screen
update_styles
visible
walk_children
watch_dark
- driver_class (Type[Driver] | None, optional): Driver class or
class ColorsView (*children: Widget, name: str | None = None, id: str | None = None, classes: str | None = None)
-
A container widget which aligns children vertically.
Expand source code
class ColorsView(Vertical): def compose(self) -> ComposeResult: LEVELS = [ "darken-3", "darken-2", "darken-1", "", "lighten-1", "lighten-2", "lighten-3", ] for color_name in ColorSystem.COLOR_NAMES: items: list[Widget] = [ColorLabel(f'"{color_name}"')] for level in LEVELS: color = f"{color_name}-{level}" if level else color_name item = ColorItem( ColorBar(f"${color}", classes="text label"), ColorBar(f"$text-muted", classes="muted"), ColorBar(f"$text-disabled", classes="disabled"), classes=color, ) items.append(item) yield ColorGroup(*items, id=f"group-{color_name}")
Ancestors
Class variables
var COMPONENT_CLASSES : ClassVar[set[str]]
Inherited members
Vertical
:Vertical
:action
add_class
allow_horizontal_scroll
allow_vertical_scroll
ancestors
animate
app
auto_height
auto_links
auto_width
background_colors
call_later
capture_mouse
check_idle
classes
colors
container_size
container_viewport
content_offset
content_region
content_size
css_identifier
css_identifier_styled
css_path_nodes
disable_messages
dispatch_key
display
displayed_children
emit
emit_no_wait
enable_messages
expand
focus
focusable_children
get_child
get_component_rich_style
get_component_styles
get_content_height
get_content_width
get_default_css
get_pseudo_classes
get_style_at
gutter
has_class
has_focus
has_pseudo_class
horizontal_scrollbar
id
is_container
is_scrollable
is_transparent
layer
layers
link_hover_style
link_style
log
max_scroll_x
max_scroll_y
mount
mouse_over
offset
on_event
outer_size
parent
post_message
post_message_no_wait
post_render
pseudo_classes
query
query_one
refresh
region
release_mouse
remove
remove_class
render
render_line
render_lines
reset_focus
reset_styles
rich_style
screen
scroll_down
scroll_end
scroll_home
scroll_left
scroll_offset
scroll_page_down
scroll_page_left
scroll_page_right
scroll_page_up
scroll_relative
scroll_right
scroll_target_x
scroll_target_y
scroll_to
scroll_to_region
scroll_to_widget
scroll_up
scroll_visible
scroll_x
scroll_y
scrollbar_corner
scrollbar_gutter
scrollbar_size_horizontal
scrollbar_size_vertical
scrollbars_enabled
set_class
set_interval
set_styles
set_timer
show_horizontal_scrollbar
show_vertical_scrollbar
shrink
siblings
size
text_style
toggle_class
tree
vertical_scrollbar
virtual_region
virtual_region_with_margin
virtual_size
visible
visible_siblings
walk_children
watch_has_focus
watch_mouse_over
window_region
Widget
:
class Content (*children: Widget, name: str | None = None, id: str | None = None, classes: str | None = None)
-
A container widget which aligns children vertically.
Expand source code
class Content(Vertical): pass
Ancestors
Class variables
var COMPONENT_CLASSES : ClassVar[set[str]]
Inherited members
Vertical
:Vertical
:action
add_class
allow_horizontal_scroll
allow_vertical_scroll
ancestors
animate
app
auto_height
auto_links
auto_width
background_colors
call_later
capture_mouse
check_idle
classes
colors
compose
container_size
container_viewport
content_offset
content_region
content_size
css_identifier
css_identifier_styled
css_path_nodes
disable_messages
dispatch_key
display
displayed_children
emit
emit_no_wait
enable_messages
expand
focus
focusable_children
get_child
get_component_rich_style
get_component_styles
get_content_height
get_content_width
get_default_css
get_pseudo_classes
get_style_at
gutter
has_class
has_focus
has_pseudo_class
horizontal_scrollbar
id
is_container
is_scrollable
is_transparent
layer
layers
link_hover_style
link_style
log
max_scroll_x
max_scroll_y
mount
mouse_over
offset
on_event
outer_size
parent
post_message
post_message_no_wait
post_render
pseudo_classes
query
query_one
refresh
region
release_mouse
remove
remove_class
render
render_line
render_lines
reset_focus
reset_styles
rich_style
screen
scroll_down
scroll_end
scroll_home
scroll_left
scroll_offset
scroll_page_down
scroll_page_left
scroll_page_right
scroll_page_up
scroll_relative
scroll_right
scroll_target_x
scroll_target_y
scroll_to
scroll_to_region
scroll_to_widget
scroll_up
scroll_visible
scroll_x
scroll_y
scrollbar_corner
scrollbar_gutter
scrollbar_size_horizontal
scrollbar_size_vertical
scrollbars_enabled
set_class
set_interval
set_styles
set_timer
show_horizontal_scrollbar
show_vertical_scrollbar
shrink
siblings
size
text_style
toggle_class
tree
vertical_scrollbar
virtual_region
virtual_region_with_margin
virtual_size
visible
visible_siblings
walk_children
watch_has_focus
watch_mouse_over
window_region
Widget
: