/usr/share/doc/python3-rich/examples
NameSizeModeActions
attrs.py10340644editdlrm
bars.py4170644editdlrm
columns.py7480644editdlrm
cp_progress.py5710644editdlrm
downloader.py22600644editdlrm
dynamic_progress.py40210644editdlrm
exception.py9190644editdlrm
export.py16930644editdlrm
file_progress.py4230644editdlrm
fullscreen.py54920644editdlrm
group.py2060644editdlrm
group2.py2240644editdlrm
highlighter.py5250644editdlrm
jobs.py9570644editdlrm
justify.py3490644editdlrm
justify2.py4260644editdlrm
layout.py11150644editdlrm
link.py1920644editdlrm
listdir.py10110644editdlrm
live_progress.py13690644editdlrm
log.py19430644editdlrm
overflow.py3700644editdlrm
padding.py1320644editdlrm
print_calendar.py19150644editdlrm
rainbow.py4410644editdlrm
README.md2640644editdlrm
recursive_error.py3240644editdlrm
repr.py5890644editdlrm
save_table_svg.py8460644editdlrm
screen.py3570644editdlrm
spinners.py5330644editdlrm
status.py3070644editdlrm
suppress.py4890644editdlrm
table.py6930644editdlrm
table_movie.py45720644editdlrm
top_lite_simulator.py21580644editdlrm
tree.py17550644editdlrm
Edit: /usr/share/doc/python3-rich/examples/top_lite_simulator.py (2158B)
"""Lite simulation of the top linux command.""" import datetime import random import sys import time from dataclasses import dataclass from rich import box from rich.console import Console from rich.live import Live from rich.table import Table if sys.version_info >= (3, 8): from typing import Literal else: from typing_extensions import Literal @dataclass class Process: pid: int command: str cpu_percent: float memory: int start_time: datetime.datetime thread_count: int state: Literal["running", "sleeping"] @property def memory_str(self) -> str: if self.memory > 1e6: return f"{int(self.memory/1e6)}M" if self.memory > 1e3: return f"{int(self.memory/1e3)}K" return str(self.memory) @property def time_str(self) -> str: return str(datetime.datetime.now() - self.start_time) def generate_process(pid: int) -> Process: return Process( pid=pid, command=f"Process {pid}", cpu_percent=random.random() * 20, memory=random.randint(10, 200) ** 3, start_time=datetime.datetime.now() - datetime.timedelta(seconds=random.randint(0, 500) ** 2), thread_count=random.randint(1, 32), state="running" if random.randint(0, 10) < 8 else "sleeping", ) def create_process_table(height: int) -> Table: processes = sorted( [generate_process(pid) for pid in range(height)], key=lambda p: p.cpu_percent, reverse=True, ) table = Table( "PID", "Command", "CPU %", "Memory", "Time", "Thread #", "State", box=box.SIMPLE ) for process in processes: table.add_row( str(process.pid), process.command, f"{process.cpu_percent:.1f}", process.memory_str, process.time_str, str(process.thread_count), process.state, ) return table console = Console() with Live(console=console, screen=True, auto_refresh=False) as live: while True: live.update(create_process_table(console.size.height - 4), refresh=True) time.sleep(1)