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): passAncestors
- textual.widgets._static.Static
- Widget
- DOMNode
- MessagePump
Inherited members
Widget:actionadd_classallow_horizontal_scrollallow_vertical_scrollancestorsanimateappauto_heightauto_linksauto_widthbackground_colorscall_latercan_focuscan_focus_childrencapture_mousecheck_idleclassescolorscomposecontainer_sizecontainer_viewportcontent_offsetcontent_regioncontent_sizecss_identifiercss_identifier_styledcss_path_nodesdisable_messagesdispatch_keydisplaydisplayed_childrenemitemit_no_waitenable_messagesexpandfocusfocusable_childrenget_childget_component_rich_styleget_component_stylesget_content_heightget_content_widthget_default_cssget_pseudo_classesget_style_atgutterhas_classhas_focushas_pseudo_classhighlight_link_idhorizontal_scrollbarhover_styleidis_containeris_scrollableis_transparentlayerlayerslink_hover_stylelink_stylelogmax_scroll_xmax_scroll_ymountmouse_overoffseton_eventouter_sizeparentpost_messagepost_message_no_waitpost_renderpseudo_classesqueryquery_onerefreshregionrelease_mouseremoveremove_classrenderrender_linerender_linesreset_focusreset_stylesrich_stylescreenscroll_downscroll_endscroll_homescroll_leftscroll_offsetscroll_page_downscroll_page_leftscroll_page_rightscroll_page_upscroll_relativescroll_rightscroll_target_xscroll_target_yscroll_toscroll_to_regionscroll_to_widgetscroll_upscroll_visiblescroll_xscroll_yscrollbar_cornerscrollbar_gutterscrollbar_size_horizontalscrollbar_size_verticalscrollbars_enabledset_classset_intervalset_stylesset_timershow_horizontal_scrollbarshow_vertical_scrollbarshrinksiblingssizetext_styletoggle_classtreevertical_scrollbarvirtual_regionvirtual_region_with_marginvirtual_sizevisiblevisible_siblingswalk_childrenwatch_has_focuswatch_mouse_overwindow_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:actionadd_classallow_horizontal_scrollallow_vertical_scrollancestorsanimateappauto_heightauto_linksauto_widthbackground_colorscall_latercapture_mousecheck_idleclassescolorscontainer_sizecontainer_viewportcontent_offsetcontent_regioncontent_sizecss_identifiercss_identifier_styledcss_path_nodesdisable_messagesdispatch_keydisplaydisplayed_childrenemitemit_no_waitenable_messagesexpandfocusfocusable_childrenget_childget_component_rich_styleget_component_stylesget_content_heightget_content_widthget_default_cssget_pseudo_classesget_style_atgutterhas_classhas_focushas_pseudo_classhorizontal_scrollbaridis_containeris_scrollableis_transparentlayerlayerslink_hover_stylelink_stylelogmax_scroll_xmax_scroll_ymountmouse_overoffseton_eventouter_sizeparentpost_messagepost_message_no_waitpost_renderpseudo_classesqueryquery_onerefreshregionrelease_mouseremoveremove_classrenderrender_linerender_linesreset_focusreset_stylesrich_stylescreenscroll_downscroll_endscroll_homescroll_leftscroll_offsetscroll_page_downscroll_page_leftscroll_page_rightscroll_page_upscroll_relativescroll_rightscroll_target_xscroll_target_yscroll_toscroll_to_regionscroll_to_widgetscroll_upscroll_visiblescroll_xscroll_yscrollbar_cornerscrollbar_gutterscrollbar_size_horizontalscrollbar_size_verticalscrollbars_enabledset_classset_intervalset_stylesset_timershow_horizontal_scrollbarshow_vertical_scrollbarshrinksiblingssizetext_styletoggle_classtreevertical_scrollbarvirtual_regionvirtual_region_with_marginvirtual_sizevisiblevisible_siblingswalk_childrenwatch_has_focuswatch_mouse_overwindow_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): passAncestors
Class variables
var COMPONENT_CLASSES : ClassVar[set[str]]
Inherited members
Vertical:Vertical:actionadd_classallow_horizontal_scrollallow_vertical_scrollancestorsanimateappauto_heightauto_linksauto_widthbackground_colorscall_latercapture_mousecheck_idleclassescolorscomposecontainer_sizecontainer_viewportcontent_offsetcontent_regioncontent_sizecss_identifiercss_identifier_styledcss_path_nodesdisable_messagesdispatch_keydisplaydisplayed_childrenemitemit_no_waitenable_messagesexpandfocusfocusable_childrenget_childget_component_rich_styleget_component_stylesget_content_heightget_content_widthget_default_cssget_pseudo_classesget_style_atgutterhas_classhas_focushas_pseudo_classhorizontal_scrollbaridis_containeris_scrollableis_transparentlayerlayerslink_hover_stylelink_stylelogmax_scroll_xmax_scroll_ymountmouse_overoffseton_eventouter_sizeparentpost_messagepost_message_no_waitpost_renderpseudo_classesqueryquery_onerefreshregionrelease_mouseremoveremove_classrenderrender_linerender_linesreset_focusreset_stylesrich_stylescreenscroll_downscroll_endscroll_homescroll_leftscroll_offsetscroll_page_downscroll_page_leftscroll_page_rightscroll_page_upscroll_relativescroll_rightscroll_target_xscroll_target_yscroll_toscroll_to_regionscroll_to_widgetscroll_upscroll_visiblescroll_xscroll_yscrollbar_cornerscrollbar_gutterscrollbar_size_horizontalscrollbar_size_verticalscrollbars_enabledset_classset_intervalset_stylesset_timershow_horizontal_scrollbarshow_vertical_scrollbarshrinksiblingssizetext_styletoggle_classtreevertical_scrollbarvirtual_regionvirtual_region_with_marginvirtual_sizevisiblevisible_siblingswalk_childrenwatch_has_focuswatch_mouse_overwindow_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): passAncestors
Class variables
var COMPONENT_CLASSES : ClassVar[set[str]]
Inherited members
Horizontal:Horizontal:actionadd_classallow_horizontal_scrollallow_vertical_scrollancestorsanimateappauto_heightauto_linksauto_widthbackground_colorscall_latercapture_mousecheck_idleclassescolorscomposecontainer_sizecontainer_viewportcontent_offsetcontent_regioncontent_sizecss_identifiercss_identifier_styledcss_path_nodesdisable_messagesdispatch_keydisplaydisplayed_childrenemitemit_no_waitenable_messagesexpandfocusfocusable_childrenget_childget_component_rich_styleget_component_stylesget_content_heightget_content_widthget_default_cssget_pseudo_classesget_style_atgutterhas_classhas_focushas_pseudo_classhorizontal_scrollbaridis_containeris_scrollableis_transparentlayerlayerslink_hover_stylelink_stylelogmax_scroll_xmax_scroll_ymountmouse_overoffseton_eventouter_sizeparentpost_messagepost_message_no_waitpost_renderpseudo_classesqueryquery_onerefreshregionrelease_mouseremoveremove_classrenderrender_linerender_linesreset_focusreset_stylesrich_stylescreenscroll_downscroll_endscroll_homescroll_leftscroll_offsetscroll_page_downscroll_page_leftscroll_page_rightscroll_page_upscroll_relativescroll_rightscroll_target_xscroll_target_yscroll_toscroll_to_regionscroll_to_widgetscroll_upscroll_visiblescroll_xscroll_yscrollbar_cornerscrollbar_gutterscrollbar_size_horizontalscrollbar_size_verticalscrollbars_enabledset_classset_intervalset_stylesset_timershow_horizontal_scrollbarshow_vertical_scrollbarshrinksiblingssizetext_styletoggle_classtreevertical_scrollbarvirtual_regionvirtual_region_with_marginvirtual_sizevisiblevisible_siblingswalk_childrenwatch_has_focuswatch_mouse_overwindow_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): passAncestors
- textual.widgets._static.Static
- Widget
- DOMNode
- MessagePump
Inherited members
Widget:actionadd_classallow_horizontal_scrollallow_vertical_scrollancestorsanimateappauto_heightauto_linksauto_widthbackground_colorscall_latercan_focuscan_focus_childrencapture_mousecheck_idleclassescolorscomposecontainer_sizecontainer_viewportcontent_offsetcontent_regioncontent_sizecss_identifiercss_identifier_styledcss_path_nodesdisable_messagesdispatch_keydisplaydisplayed_childrenemitemit_no_waitenable_messagesexpandfocusfocusable_childrenget_childget_component_rich_styleget_component_stylesget_content_heightget_content_widthget_default_cssget_pseudo_classesget_style_atgutterhas_classhas_focushas_pseudo_classhighlight_link_idhorizontal_scrollbarhover_styleidis_containeris_scrollableis_transparentlayerlayerslink_hover_stylelink_stylelogmax_scroll_xmax_scroll_ymountmouse_overoffseton_eventouter_sizeparentpost_messagepost_message_no_waitpost_renderpseudo_classesqueryquery_onerefreshregionrelease_mouseremoveremove_classrenderrender_linerender_linesreset_focusreset_stylesrich_stylescreenscroll_downscroll_endscroll_homescroll_leftscroll_offsetscroll_page_downscroll_page_leftscroll_page_rightscroll_page_upscroll_relativescroll_rightscroll_target_xscroll_target_yscroll_toscroll_to_regionscroll_to_widgetscroll_upscroll_visiblescroll_xscroll_yscrollbar_cornerscrollbar_gutterscrollbar_size_horizontalscrollbar_size_verticalscrollbars_enabledset_classset_intervalset_stylesset_timershow_horizontal_scrollbarshow_vertical_scrollbarshrinksiblingssizetext_styletoggle_classtreevertical_scrollbarvirtual_regionvirtual_region_with_marginvirtual_sizevisiblevisible_siblingswalk_childrenwatch_has_focuswatch_mouse_overwindow_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
Noneto auto-detect. Defaults to None. - title (str | None, optional): Title of the application. If
None, the title is set to the name of theAppsubclass. Defaults toNone. - css_path (str | PurePath | None, optional): Path to CSS or
Nonefor 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 BINDINGSvar CSS_PATH : CSSPathTypevar SCREENS : dict[str, Screen]var SUB_TITLE : str | Nonevar 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:actionaction_bellaction_focusaction_pop_screenaction_push_screenaction_quitaction_screenshotaction_switch_screenaction_toggle_darkadd_classancestorsanimateappbackground_colorsbellbindcall_latercapture_mousecheck_bindingscheck_idleclassescolorscomposecss_identifiercss_identifier_styledcss_path_nodesdarkdebugdisable_messagesdispatch_keydisplaydisplayed_childrenemitemit_no_waitenable_messagesexitexport_screenshotfatal_errorfocusedget_childget_component_stylesget_css_variablesget_default_cssget_driver_classget_pseudo_classesget_screenget_widget_athas_classhas_pseudo_classidinstall_screenis_headlessis_mountedis_screen_installedlogmountmount_allnamespace_bindingson_eventpanicparentpop_screenpost_messagepost_message_no_waitpseudo_classespush_screenqueryquery_onerefresh_cssremove_classreset_stylesrich_stylerunsave_screenshotscreenscreen_stackset_classset_focusset_intervalset_stylesset_timersizesub_titleswitch_screentext_styletitletoggle_classtreeuninstall_screenupdate_stylesvisiblewalk_childrenwatch_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:actionadd_classallow_horizontal_scrollallow_vertical_scrollancestorsanimateappauto_heightauto_linksauto_widthbackground_colorscall_latercapture_mousecheck_idleclassescolorscontainer_sizecontainer_viewportcontent_offsetcontent_regioncontent_sizecss_identifiercss_identifier_styledcss_path_nodesdisable_messagesdispatch_keydisplaydisplayed_childrenemitemit_no_waitenable_messagesexpandfocusfocusable_childrenget_childget_component_rich_styleget_component_stylesget_content_heightget_content_widthget_default_cssget_pseudo_classesget_style_atgutterhas_classhas_focushas_pseudo_classhorizontal_scrollbaridis_containeris_scrollableis_transparentlayerlayerslink_hover_stylelink_stylelogmax_scroll_xmax_scroll_ymountmouse_overoffseton_eventouter_sizeparentpost_messagepost_message_no_waitpost_renderpseudo_classesqueryquery_onerefreshregionrelease_mouseremoveremove_classrenderrender_linerender_linesreset_focusreset_stylesrich_stylescreenscroll_downscroll_endscroll_homescroll_leftscroll_offsetscroll_page_downscroll_page_leftscroll_page_rightscroll_page_upscroll_relativescroll_rightscroll_target_xscroll_target_yscroll_toscroll_to_regionscroll_to_widgetscroll_upscroll_visiblescroll_xscroll_yscrollbar_cornerscrollbar_gutterscrollbar_size_horizontalscrollbar_size_verticalscrollbars_enabledset_classset_intervalset_stylesset_timershow_horizontal_scrollbarshow_vertical_scrollbarshrinksiblingssizetext_styletoggle_classtreevertical_scrollbarvirtual_regionvirtual_region_with_marginvirtual_sizevisiblevisible_siblingswalk_childrenwatch_has_focuswatch_mouse_overwindow_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): passAncestors
Class variables
var COMPONENT_CLASSES : ClassVar[set[str]]
Inherited members
Vertical:Vertical:actionadd_classallow_horizontal_scrollallow_vertical_scrollancestorsanimateappauto_heightauto_linksauto_widthbackground_colorscall_latercapture_mousecheck_idleclassescolorscomposecontainer_sizecontainer_viewportcontent_offsetcontent_regioncontent_sizecss_identifiercss_identifier_styledcss_path_nodesdisable_messagesdispatch_keydisplaydisplayed_childrenemitemit_no_waitenable_messagesexpandfocusfocusable_childrenget_childget_component_rich_styleget_component_stylesget_content_heightget_content_widthget_default_cssget_pseudo_classesget_style_atgutterhas_classhas_focushas_pseudo_classhorizontal_scrollbaridis_containeris_scrollableis_transparentlayerlayerslink_hover_stylelink_stylelogmax_scroll_xmax_scroll_ymountmouse_overoffseton_eventouter_sizeparentpost_messagepost_message_no_waitpost_renderpseudo_classesqueryquery_onerefreshregionrelease_mouseremoveremove_classrenderrender_linerender_linesreset_focusreset_stylesrich_stylescreenscroll_downscroll_endscroll_homescroll_leftscroll_offsetscroll_page_downscroll_page_leftscroll_page_rightscroll_page_upscroll_relativescroll_rightscroll_target_xscroll_target_yscroll_toscroll_to_regionscroll_to_widgetscroll_upscroll_visiblescroll_xscroll_yscrollbar_cornerscrollbar_gutterscrollbar_size_horizontalscrollbar_size_verticalscrollbars_enabledset_classset_intervalset_stylesset_timershow_horizontal_scrollbarshow_vertical_scrollbarshrinksiblingssizetext_styletoggle_classtreevertical_scrollbarvirtual_regionvirtual_region_with_marginvirtual_sizevisiblevisible_siblingswalk_childrenwatch_has_focuswatch_mouse_overwindow_region
Widget: