|
| 1 | +import random |
| 2 | +from statistics import mean |
| 3 | + |
| 4 | +from textual.app import ComposeResult |
| 5 | +from textual.containers import Container, Center, Middle |
| 6 | +from textual.widgets import ( |
| 7 | + DirectoryTree, |
| 8 | + Footer, |
| 9 | + Header, |
| 10 | + Input, |
| 11 | + Label, |
| 12 | + ListView, |
| 13 | + ListItem, |
| 14 | + LoadingIndicator, |
| 15 | + Sparkline, |
| 16 | + Static, |
| 17 | + Tree, |
| 18 | + ProgressBar, |
| 19 | + TabbedContent, |
| 20 | + TabPane, |
| 21 | + Markdown, |
| 22 | + Tabs, |
| 23 | +) |
| 24 | + |
| 25 | +from .button import button_example |
| 26 | +from .checkbox import checkbox_example |
| 27 | +from .content_switcher import content_switcher_example |
| 28 | +from .data_table import data_table_example |
| 29 | +from .markdown import markdown_viewer_example, markdown_example |
| 30 | +from .option_list import option_list_example |
| 31 | +from .placeholder import placeholder_example |
| 32 | +from .pretty import pretty_example |
| 33 | +from .radio import radio_button_example, radio_set_example |
| 34 | +from .select import select_example, selection_list_example |
| 35 | +from .switch import switch_example |
| 36 | + |
| 37 | +# from .text_log import text_log_example |
| 38 | + |
| 39 | + |
| 40 | +def directory_tree_example(id: str) -> ComposeResult: |
| 41 | + yield Container(DirectoryTree("./"), id=id) |
| 42 | + |
| 43 | + |
| 44 | +def footer_example(id: str) -> ComposeResult: |
| 45 | + yield Container(Footer(), id=id) |
| 46 | + |
| 47 | + |
| 48 | +def header_example(id: str) -> ComposeResult: |
| 49 | + yield Container(Header(), id=id) |
| 50 | + |
| 51 | + |
| 52 | +def input_example(id: str) -> ComposeResult: |
| 53 | + yield Container( |
| 54 | + Input(placeholder="First Name"), Input(placeholder="Last Name"), id=id |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def label_example(id: str) -> ComposeResult: |
| 59 | + yield Container(Label("Hello, world!"), id=id) |
| 60 | + |
| 61 | + |
| 62 | +def list_item_example(id: str) -> ComposeResult: |
| 63 | + yield Container( |
| 64 | + ListView( |
| 65 | + ListItem(Label("One")), |
| 66 | + ListItem(Label("Two")), |
| 67 | + ListItem(Label("Three")), |
| 68 | + ), |
| 69 | + id=id, |
| 70 | + ) |
| 71 | + |
| 72 | + yield Footer() |
| 73 | + |
| 74 | + |
| 75 | +def loading_example(id: str) -> ComposeResult: |
| 76 | + yield Container(LoadingIndicator(), id=id) |
| 77 | + |
| 78 | + |
| 79 | +def sparkline_example(id: str) -> ComposeResult: |
| 80 | + data = [random.expovariate(1 / 3) for _ in range(1000)] |
| 81 | + |
| 82 | + yield Container( |
| 83 | + Sparkline(data, summary_function=max), |
| 84 | + Sparkline(data, summary_function=mean), |
| 85 | + Sparkline(data, summary_function=min), |
| 86 | + id=id, |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +def static_example(id: str) -> ComposeResult: |
| 91 | + yield Container(Static("Hello, world!"), id=id) |
| 92 | + |
| 93 | + |
| 94 | +def tree_example(id: str) -> ComposeResult: |
| 95 | + tree: Tree[dict] = Tree("Dune") |
| 96 | + tree.root.expand() |
| 97 | + characters = tree.root.add("Characters", expand=True) |
| 98 | + characters.add_leaf("Paul") |
| 99 | + characters.add_leaf("Jessica") |
| 100 | + characters.add_leaf("Chani") |
| 101 | + yield Container(tree, id=id) |
| 102 | + |
| 103 | + |
| 104 | +def progress_bar_example(id: str) -> ComposeResult: |
| 105 | + bar = ProgressBar(total=100, show_eta=False) |
| 106 | + bar.advance(50) |
| 107 | + yield Container(Center(Middle(bar)), id=id) |
| 108 | + |
| 109 | + |
| 110 | +def tabbed_content_example(id: str): |
| 111 | + content = TabbedContent() |
| 112 | + content._tab_content = [ |
| 113 | + TabPane("Leto", Markdown("# Leto"), id="leto"), |
| 114 | + TabPane("Jessica", Markdown("# Jessica"), id="jessica"), |
| 115 | + TabPane("Paul", Markdown("# Paul"), id="paulo"), |
| 116 | + ] |
| 117 | + # This does not work, reason why I used _tab_content above |
| 118 | + # content.add_pane(TabPane("Leto", Markdown("#Leto"), id="letoo")) |
| 119 | + # content.add_pane(TabPane("Jessica", Markdown(""), id="jessicoa")) |
| 120 | + # content.add_pane(TabPane("Paul", Markdown(""), id="paulo")) |
| 121 | + |
| 122 | + yield Container(content, id=id) |
| 123 | + |
| 124 | + |
| 125 | +def tabs_example(id: str): |
| 126 | + yield Container(Tabs("First tab", "Second tab", "Third tab"), id=id) |
| 127 | + |
| 128 | + |
| 129 | +__all__ = [ |
| 130 | + "button_example", |
| 131 | + "checkbox_example", |
| 132 | + "content_switcher_example", |
| 133 | + "data_table_example", |
| 134 | + "directory_tree_example", |
| 135 | + "footer_example", |
| 136 | + "header_example", |
| 137 | + "input_example", |
| 138 | + "label_example", |
| 139 | + "list_item_example", |
| 140 | + "loading_example", |
| 141 | + "markdown_viewer_example", |
| 142 | + "markdown_example", |
| 143 | + "option_list_example", |
| 144 | + "placeholder_example", |
| 145 | + "pretty_example", |
| 146 | + "progress_bar_example", |
| 147 | + "radio_button_example", |
| 148 | + "radio_set_example", |
| 149 | + "select_example", |
| 150 | + "selection_list_example", |
| 151 | + "sparkline_example", |
| 152 | + "static_example", |
| 153 | + "switch_example", |
| 154 | + "tabbed_content_example", |
| 155 | + "tabs_example", |
| 156 | + "tree_example", |
| 157 | + # "text_log_example", |
| 158 | +] |
0 commit comments