mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Rich log (#3046)
* log * tests * snapshot tests * change to richlog * keep raw lines * disable highlighting by default * simplify * superfluous test * optimization * update cell length * add refresh * write method * version bump * doc fix link * makes lines private * docstring * relax dev dependancy * remove superfluous code [skip ci] * added FAQ [skipci] * fix code in faq [skipci] * fix typo * max lines fix
This commit is contained in:
65
docs/examples/widgets/rich_log.py
Normal file
65
docs/examples/widgets/rich_log.py
Normal file
@@ -0,0 +1,65 @@
|
||||
import csv
|
||||
import io
|
||||
|
||||
from rich.syntax import Syntax
|
||||
from rich.table import Table
|
||||
|
||||
from textual import events
|
||||
from textual.app import App, ComposeResult
|
||||
from textual.widgets import RichLog
|
||||
|
||||
CSV = """lane,swimmer,country,time
|
||||
4,Joseph Schooling,Singapore,50.39
|
||||
2,Michael Phelps,United States,51.14
|
||||
5,Chad le Clos,South Africa,51.14
|
||||
6,László Cseh,Hungary,51.14
|
||||
3,Li Zhuhao,China,51.26
|
||||
8,Mehdy Metella,France,51.58
|
||||
7,Tom Shields,United States,51.73
|
||||
1,Aleksandr Sadovnikov,Russia,51.84"""
|
||||
|
||||
|
||||
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\
|
||||
'''
|
||||
|
||||
|
||||
class RichLogApp(App):
|
||||
def compose(self) -> ComposeResult:
|
||||
yield RichLog(highlight=True, markup=True)
|
||||
|
||||
def on_ready(self) -> None:
|
||||
"""Called when the DOM is ready."""
|
||||
text_log = self.query_one(RichLog)
|
||||
|
||||
text_log.write(Syntax(CODE, "python", indent_guides=True))
|
||||
|
||||
rows = iter(csv.reader(io.StringIO(CSV)))
|
||||
table = Table(*next(rows))
|
||||
for row in rows:
|
||||
table.add_row(*row)
|
||||
|
||||
text_log.write(table)
|
||||
text_log.write("[bold magenta]Write text or any Rich renderable!")
|
||||
|
||||
def on_key(self, event: events.Key) -> None:
|
||||
"""Write Key events to log."""
|
||||
text_log = self.query_one(RichLog)
|
||||
text_log.write(event)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = RichLogApp()
|
||||
app.run()
|
||||
Reference in New Issue
Block a user