### Install and Launch Example App Source: https://github.com/textualize/textual/blob/main/docs/how-to/package-with-hatch.md Commands to install the textual-calculator package via pip and execute the application from the command line. ```bash pip install textual-calculator ``` ```bash calculator ``` -------------------------------- ### Run Example App from Textual Repository Source: https://github.com/textualize/textual/blob/main/docs/getting_started.md Navigates to the examples directory and runs the code_browser example. Requires the repository cloned. ```bash cd textual/examples/ python code_browser.py ../ ``` -------------------------------- ### Providing usage examples with Examples Source: https://github.com/textualize/textual/wiki/Documentation-standards The Examples section is used for REPL-style usage demonstrations. ```python def add_with_a_twist(a: int, b: int) -> int: """Adds two integers with a twist. Examples: You can write prose and also snippets from a REPL session. >>> add_with_a_twist(10, 5) 5 >>> add_with_a_twist(5, 10) -5 """ return a - b ``` -------------------------------- ### Run text_style.py example Source: https://github.com/textualize/textual/blob/main/docs/examples/styles/README.md Use this command to run the `text_style.py` example. The example is part of the documentation examples and is used to generate screenshots. ```bash textual run text_style.py ``` -------------------------------- ### Mount widget with before and after parameters Source: https://github.com/textualize/textual/blob/main/docs/blog/posts/release0-4-0.md Examples of using the mount method to position widgets: before=0 for the start, after with a CSS selector, and after a specific widget instance. ```python # Mount at the start self.mount(Button(id="Buy Coffee"), before=0) # Mount after a selector self.mount(Static("Password is incorrect"), after="Dialog Input.-error") # Mount after a specific widget tweet = self.query_one("Tweet") self.mount(Static("Consider switching to Mastodon"), after=tweet) ``` -------------------------------- ### Install Textual and textual-dev Source: https://github.com/textualize/textual/blob/main/README.md Install Textual and the textual-dev package via pip. This is the recommended way to get started with Textual development. ```bash pip install textual textual-dev ``` -------------------------------- ### Pride example (interactive demo) Source: https://github.com/textualize/textual/blob/main/docs/index.md An interactive terminal demo of the Pride example. Click the tab to run the example in your browser. ```textual ```{.textual path="examples/pride.py"} ``` ``` -------------------------------- ### Run a Textual example application Source: https://github.com/textualize/textual/blob/main/examples/README.md Navigate to the examples directory and execute a specific Python file using the python command. ```bash cd textual/examples python pride.py ``` -------------------------------- ### CSS usage examples Source: https://github.com/textualize/textual/blob/main/docs/styles/links/link_background.md CSS examples showing how to set `link-background` with a color and percentage, and with a design token. ```css link-background: red 70%; link-background: $accent; ``` -------------------------------- ### Python examples for content-align Source: https://github.com/textualize/textual/blob/main/docs/styles/content_align.md Python examples showing how to set content alignment using the styles API. ```python # Align content in the very center of a widget widget.styles.content_align = ("center", "middle") # Align content at the top right of a widget widget.styles.content_align = ("right", "top") # Change the horizontal alignment of the content of a widget widget.styles.content_align_horizontal = "right" # Change the vertical alignment of the content of a widget widget.styles.content_align_vertical = "middle" ``` -------------------------------- ### Run the Textual demo Source: https://github.com/textualize/textual/blob/main/CONTRIBUTING.md Use this command to run the Textual demo after installation. It verifies that everything was installed correctly. ```bash python -m textual ``` -------------------------------- ### ListView widget example Source: https://github.com/textualize/textual/blob/main/docs/widget_gallery.md Displays a list of items, possibly other widgets. The example is shown with default settings. ```textual path="docs/examples/widgets/list_view.py" ``` -------------------------------- ### Interactive output of the example Source: https://github.com/textualize/textual/blob/main/docs/widgets/content_switcher.md Output of the example rendered in the documentation via a Textual app. ```textual ```{.textual path="docs/examples/widgets/content_switcher.py"} ``` ``` ```textual ```{.textual path="docs/examples/widgets/content_switcher.py" lines="40" press="tab,enter"} ``` ``` -------------------------------- ### Install faqtory via pipx Source: https://github.com/textualize/textual/blob/main/questions/README.md Use pipx to install the faqtory tool to ensure dependency isolation. ```bash pipx install faqtory ``` -------------------------------- ### CSS height examples Source: https://github.com/textualize/textual/blob/main/docs/styles/height.md Examples of setting height using explicit cell counts, percentages, or automatic sizing. ```css /* Explicit cell height */ height: 10; /* Percentage height */ height: 50%; /* Automatic height */ height: auto ``` -------------------------------- ### Hello example CSS (hello03.tcss) Source: https://github.com/textualize/textual/blob/main/docs/guide/widgets.md External CSS for the hello03 example, kept in a separate file for live editing. ```css --8<-- "docs/examples/guide/widgets/hello03.tcss" ``` -------------------------------- ### Iterate over all widgets with self.query() Source: https://github.com/textualize/textual/blob/main/docs/guide/queries.md Use no-argument query() to get a DOMQuery of all widgets and their children. This example prints each widget. ```python for widget in self.query(): print(widget) ``` -------------------------------- ### Verify entry point via Python REPL Source: https://github.com/textualize/textual/blob/main/docs/how-to/package-with-hatch.md Demonstrates the equivalent manual execution of the entry point from the Python REPL. ```python >>> import textual_calculator.entry_points >>> textual_calculator.entry_points.calculator() ``` -------------------------------- ### Set dynamic default with a callable Source: https://github.com/textualize/textual/blob/main/docs/guide/reactivity.md The default value can be a function or callable, which Textual calls to get the default. The example uses the `time` function to assign the current time in seconds when the widget is created. ```python from time import time from textual.reactive import reactive from textual.widget import Widget class Timer(Widget): start_time = reactive(time) # (1)! ``` -------------------------------- ### Inspect a file object with Rich's inspect Source: https://github.com/textualize/textual/blob/main/docs/blog/posts/rich-inspect.md Use `from rich import inspect` and call `inspect()` on any object to get a formatted report of its data attributes. This example creates a file object and inspects it directly from the REPL. ```python >>> from rich import inspect >>> text_file = open("foo.txt", "w") >>> inspect(text_file) ``` -------------------------------- ### View textual serve help Source: https://github.com/textualize/textual/blob/main/docs/guide/devtools.md Displays additional switches and options for the serve command. ```bash textual serve --help ``` -------------------------------- ### Launch the application Source: https://github.com/textualize/textual/blob/main/docs/how-to/package-with-hatch.md Execute the newly defined command from the command line. ```bash calculator ``` -------------------------------- ### Build the FAQ Source: https://github.com/textualize/textual/blob/main/questions/README.md Execute the build command from the root of the repository to generate the FAQ. ```bash faqtory build ``` -------------------------------- ### Example stylesheet (tint.tcss) Source: https://github.com/textualize/textual/blob/main/docs/styles/tint.md The Textual CSS stylesheet for the example. ```css --8<-- "docs/examples/styles/tint.tcss" ``` -------------------------------- ### Example source (tint.py) Source: https://github.com/textualize/textual/blob/main/docs/styles/tint.md The Python source for the example. It sets the tint to a `Color` instance with varying levels of opacity using `with_alpha`. ```python --8<-- "docs/examples/styles/tint.py" ``` -------------------------------- ### Python link-style examples Source: https://github.com/textualize/textual/blob/main/docs/styles/links/link_style.md Examples of setting the link_style property programmatically in Python. ```python widget.styles.link_style = "bold" widget.styles.link_style = "bold italic reverse" ``` -------------------------------- ### column-span CSS example Source: https://github.com/textualize/textual/blob/main/docs/styles/grid/column_span.md Example of the column-span style set to 3 columns. ```css column-span: 3; ``` -------------------------------- ### Run the stopwatch application Source: https://github.com/textualize/textual/blob/main/docs/tutorial.md Navigate to the tutorial directory and execute the stopwatch application script. ```bash cd textual/docs/examples/tutorial python stopwatch.py ``` -------------------------------- ### calculator.tcss stylesheet Source: https://github.com/textualize/textual/blob/main/docs/index.md The CSS stylesheet for the Calculator example, taken from the examples directory. ```css ```css --8<-- "examples/calculator.tcss" ``` ``` -------------------------------- ### Hello example with Static widget (hello03.py) Source: https://github.com/textualize/textual/blob/main/docs/guide/widgets.md Creates a widget that cycles through greetings in various languages. Uses Static's update() method to change content, and calls next_word from mount and click handlers. ```python --8<-- "docs/examples/guide/widgets/hello03.py" ``` -------------------------------- ### Install pytest-textual-snapshot Source: https://github.com/textualize/textual/blob/main/docs/guide/testing.md Install the official Textual pytest plugin using pip. ```bash pip install pytest-textual-snapshot ``` -------------------------------- ### Mount widgets at start, before, and after Source: https://github.com/textualize/textual/wiki/Mount-API-proposal Shows various ways to mount widgets: at the start, before a specific widget by selector, before the first Foo, mounting multiple widgets, at the end, and after a widget by CSS selector. ```python # Mount at start self.mount(Foo()) self.mount(Foo(), before=0) # Mount before first Foo self.mount(Foo(), before="Foo") # Equivalent to... self.mount(Foo(), before=self.query_one("Foo")) # Mount multiple widgets self.mount(Foo(), Bar(), Baz()) # Mount at end self.mount(Foo()) self.mount(Foo(), after=-1) # Add widget after another widget self.mount(Foo(), after="#bar") ``` -------------------------------- ### calculator.py source code Source: https://github.com/textualize/textual/blob/main/docs/index.md The source code for the Calculator example, taken from the examples directory. ```python ```python --8<-- "examples/calculator.py" ``` ``` -------------------------------- ### Run a Textual app from a module import Source: https://github.com/textualize/textual/blob/main/docs/guide/devtools.md Imports the module `music.play` and runs the `app` instance found there. ```bash textual run music.play ``` -------------------------------- ### Compose a Welcome widget Source: https://github.com/textualize/textual/blob/main/docs/guide/app.md Demonstrates implementing the compose() method to yield a builtin Welcome widget. ```python from textual.app import App from textual.widgets import Welcome class WelcomeApp(App): def compose(self): yield Welcome() if __name__ == "__main__": app = WelcomeApp() app.run() ``` -------------------------------- ### pride.py source code Source: https://github.com/textualize/textual/blob/main/docs/index.md The source code for the Pride example, taken from the examples directory. ```py ```py --8<-- "examples/pride.py" ``` ``` -------------------------------- ### actions06.tcss - stylesheet for actions06 example Source: https://github.com/textualize/textual/blob/main/docs/guide/actions.md CSS stylesheet used by the actions06 example app. ```css --8<-- "docs/examples/guide/actions/actions06.tcss" ``` -------------------------------- ### Calculator example (interactive demo) Source: https://github.com/textualize/textual/blob/main/docs/index.md An interactive terminal demo of the Calculator example, configured with specific columns, lines, and key presses. Click the tab to run it. ```textual ```{.textual path="examples/calculator.py" columns=100 lines=41 press="6,.,2,8,3,1,8,5,3,0,7,1,wait:400"} ``` ``` -------------------------------- ### Install calculator via pip Source: https://github.com/textualize/textual/blob/main/docs/how-to/package-with-hatch.md Command to install the textual_calculator package using pip. ```bash pip install textual_calculator ``` -------------------------------- ### Run the Greetings app with various arguments Source: https://github.com/textualize/textual/blob/main/docs/FAQ.md Demonstrates how to run the Greetings app with different argument combinations: defaults, keyword, and positional. ```python # Running with default arguments. Greetings().run() # Running with a keyword argument. Greetings(to_greet="davep").run() # Running with both positional arguments. Greetings("Well hello", "there").run() ``` -------------------------------- ### Python usage examples Source: https://github.com/textualize/textual/blob/main/docs/styles/links/link_background.md Python examples showing how to set `link-background` using the styles property, including using a `Color` object directly. ```python widget.styles.link_background = "red 70%" widget.styles.link_background = "$accent" # You can also use a `Color` object directly: widget.styles.link_background = Color(100, 30, 173) ``` -------------------------------- ### Install textual-keys via pipx Source: https://github.com/textualize/textual/blob/main/docs/blog/posts/be-the-keymaster.md Use this command to install the textual-keys utility to your environment. ```sh $ pipx install textual-keys ``` -------------------------------- ### Complete App Example with ProgressBar Source: https://github.com/textualize/textual/blob/main/docs/widgets/progress_bar.md A simple application tracking funding levels using a progress bar. The example includes the Python implementation and the associated TCSS styling. ```python --8<-- "docs/examples/widgets/progress_bar.py" ``` ```css --8<-- "docs/examples/widgets/progress_bar.tcss" ``` -------------------------------- ### Sorting example (data_table_sort.py) Source: https://github.com/textualize/textual/blob/main/docs/widgets/data_table.md Example demonstrating the sort method with column and key function parameters. ```python --8<-- "docs/examples/widgets/data_table_sort.py" ``` -------------------------------- ### Python examples for setting alignment via styles Source: https://github.com/textualize/textual/blob/main/docs/styles/align.md Python examples showing how to set alignment using the `styles.align`, `styles.align_horizontal`, and `styles.align_vertical` attributes. Comments explain each assignment. ```python # Align child widgets to the center widget.styles.align = ("center", "middle") # Align child widgets to the top right widget.styles.align = ("right", "top") # Change the horizontal alignment of the children of a widget widget.styles.align_horizontal = "right" # Change the vertical alignment of the children of a widget widget.styles.align_vertical = "middle" ``` -------------------------------- ### offset.tcss example Source: https://github.com/textualize/textual/blob/main/docs/styles/offset.md Stylesheet for the offset example, highlighting lines 13, 20, and 27 where offsets are applied. ```css --8<-- "docs/examples/styles/offset.tcss" ``` -------------------------------- ### Python min-width examples Source: https://github.com/textualize/textual/blob/main/docs/styles/min_width.md Python examples showing how to set the minimum width using the styles API, with values of 10 rows or 25vw. ```python # Set the minimum width to 10 rows widget.styles.min_width = 10 # Set the minimum width to 25% of the viewport width widget.styles.min_width = "25vw" ```