0
# Interactive Components
1
2
Live updating displays, user input prompts, and real-time interface components. Rich provides comprehensive interactive capabilities for dynamic terminal applications and user input handling.
3
4
## Capabilities
5
6
### Live Display
7
8
Real-time updating display system for dynamic content.
9
10
```python { .api }
11
class Live:
12
"""
13
Live updating display context manager.
14
15
Args:
16
renderable: Initial content to display
17
console: Console instance for output
18
screen: Use full screen mode
19
auto_refresh: Enable automatic refresh
20
refresh_per_second: Refresh rate in Hz
21
transient: Remove display when exiting context
22
redirect_stdout: Redirect stdout during live display
23
redirect_stderr: Redirect stderr during live display
24
vertical_overflow: Overflow method for vertical content
25
get_renderable: Function to get current renderable
26
"""
27
def __init__(
28
self,
29
renderable: Optional[RenderableType] = None,
30
*,
31
console: Optional[Console] = None,
32
screen: bool = False,
33
auto_refresh: bool = True,
34
refresh_per_second: float = 4,
35
transient: bool = False,
36
redirect_stdout: bool = True,
37
redirect_stderr: bool = True,
38
vertical_overflow: OverflowMethod = "ellipsis",
39
get_renderable: Optional[Callable[[], RenderableType]] = None,
40
): ...
41
42
def update(
43
self,
44
renderable: RenderableType,
45
*,
46
refresh: bool = False
47
) -> None:
48
"""
49
Update the live display content.
50
51
Args:
52
renderable: New content to display
53
refresh: Force immediate refresh
54
"""
55
56
def refresh(self) -> None:
57
"""Force refresh of the display."""
58
59
def start(self, refresh: bool = False) -> None:
60
"""
61
Start the live display.
62
63
Args:
64
refresh: Refresh immediately after starting
65
"""
66
67
def stop(self) -> None:
68
"""Stop the live display."""
69
70
# Properties
71
@property
72
def is_started(self) -> bool:
73
"""Check if live display is active."""
74
75
@property
76
def renderable(self) -> RenderableType:
77
"""Get current renderable content."""
78
79
@property
80
def console(self) -> Console:
81
"""Get the console instance."""
82
```
83
84
### Status Display
85
86
Status indicator with animated spinner.
87
88
```python { .api }
89
class Status:
90
"""
91
Status display with spinner animation.
92
93
Args:
94
status: Status message or renderable
95
console: Console instance for output
96
spinner: Spinner animation name
97
spinner_style: Style for spinner
98
speed: Animation speed multiplier
99
refresh_per_second: Refresh rate in Hz
100
"""
101
def __init__(
102
self,
103
status: RenderableType,
104
*,
105
console: Optional[Console] = None,
106
spinner: str = "dots",
107
spinner_style: StyleType = "status.spinner",
108
speed: float = 1.0,
109
refresh_per_second: float = 12.5,
110
): ...
111
112
def update(
113
self,
114
status: Optional[RenderableType] = None,
115
*,
116
spinner: Optional[str] = None,
117
spinner_style: Optional[StyleType] = None,
118
speed: Optional[float] = None,
119
) -> None:
120
"""
121
Update status display.
122
123
Args:
124
status: New status message
125
spinner: New spinner style
126
spinner_style: New spinner styling
127
speed: New animation speed
128
"""
129
130
def start(self) -> None:
131
"""Start the status display."""
132
133
def stop(self) -> None:
134
"""Stop the status display."""
135
136
# Properties
137
@property
138
def renderable(self) -> RenderableType:
139
"""Get current status renderable."""
140
```
141
142
### User Input Prompts
143
144
Interactive prompts for user input with validation.
145
146
```python { .api }
147
class PromptBase(Generic[PromptType]):
148
"""Base class for user input prompts."""
149
150
@classmethod
151
def ask(
152
cls,
153
prompt: TextType = "",
154
*,
155
console: Optional[Console] = None,
156
password: bool = False,
157
choices: Optional[List[str]] = None,
158
show_default: bool = True,
159
show_choices: bool = True,
160
default: Any = ...,
161
stream: Optional[TextIO] = None,
162
) -> PromptType:
163
"""
164
Prompt user for input.
165
166
Args:
167
prompt: Prompt text to display
168
console: Console instance for I/O
169
password: Hide input for passwords
170
choices: Valid choices for input
171
show_default: Show default value in prompt
172
show_choices: Show available choices
173
default: Default value if no input
174
stream: Input stream or None for stdin
175
176
Returns:
177
User input converted to appropriate type
178
"""
179
180
def get_input(
181
self,
182
console: Console,
183
prompt: TextType,
184
password: bool,
185
stream: Optional[TextIO] = None,
186
) -> str:
187
"""
188
Get raw input from user.
189
190
Args:
191
console: Console for output
192
prompt: Prompt to display
193
password: Hide input characters
194
stream: Input stream
195
196
Returns:
197
Raw input string
198
"""
199
200
def check_choice(self, value: str) -> bool:
201
"""
202
Check if input matches available choices.
203
204
Args:
205
value: User input to check
206
207
Returns:
208
True if valid choice
209
"""
210
211
def process_response(self, value: str) -> PromptType:
212
"""
213
Process and validate user response.
214
215
Args:
216
value: Raw user input
217
218
Returns:
219
Processed and validated response
220
"""
221
222
def on_validate_error(self, value: str, error: ValueError) -> None:
223
"""
224
Handle validation errors.
225
226
Args:
227
value: Invalid input value
228
error: Validation error
229
"""
230
231
def render_default(self, default: Any) -> Text:
232
"""
233
Render default value for display.
234
235
Args:
236
default: Default value
237
238
Returns:
239
Formatted default value
240
"""
241
242
class Prompt(PromptBase[str]):
243
"""String input prompt."""
244
245
@classmethod
246
def ask(
247
cls,
248
prompt: TextType = "",
249
*,
250
console: Optional[Console] = None,
251
password: bool = False,
252
choices: Optional[List[str]] = None,
253
show_default: bool = True,
254
show_choices: bool = True,
255
default: str = "",
256
stream: Optional[TextIO] = None,
257
) -> str:
258
"""
259
Prompt for string input.
260
261
Args:
262
prompt: Prompt text
263
console: Console instance
264
password: Hide input
265
choices: Valid string choices
266
show_default: Show default in prompt
267
show_choices: Show available choices
268
default: Default string value
269
stream: Input stream
270
271
Returns:
272
User input string
273
"""
274
275
class IntPrompt(PromptBase[int]):
276
"""Integer input prompt."""
277
278
@classmethod
279
def ask(
280
cls,
281
prompt: TextType = "",
282
*,
283
console: Optional[Console] = None,
284
password: bool = False,
285
choices: Optional[List[int]] = None,
286
show_default: bool = True,
287
show_choices: bool = True,
288
default: int = ...,
289
stream: Optional[TextIO] = None,
290
) -> int:
291
"""
292
Prompt for integer input.
293
294
Args:
295
prompt: Prompt text
296
console: Console instance
297
password: Hide input
298
choices: Valid integer choices
299
show_default: Show default in prompt
300
show_choices: Show available choices
301
default: Default integer value
302
stream: Input stream
303
304
Returns:
305
User input as integer
306
"""
307
308
class FloatPrompt(PromptBase[float]):
309
"""Float input prompt."""
310
311
@classmethod
312
def ask(
313
cls,
314
prompt: TextType = "",
315
*,
316
console: Optional[Console] = None,
317
password: bool = False,
318
choices: Optional[List[float]] = None,
319
show_default: bool = True,
320
show_choices: bool = True,
321
default: float = ...,
322
stream: Optional[TextIO] = None,
323
) -> float:
324
"""
325
Prompt for float input.
326
327
Args:
328
prompt: Prompt text
329
console: Console instance
330
password: Hide input
331
choices: Valid float choices
332
show_default: Show default in prompt
333
show_choices: Show available choices
334
default: Default float value
335
stream: Input stream
336
337
Returns:
338
User input as float
339
"""
340
341
class Confirm(PromptBase[bool]):
342
"""Yes/no confirmation prompt."""
343
344
@classmethod
345
def ask(
346
cls,
347
prompt: TextType = "",
348
*,
349
console: Optional[Console] = None,
350
password: bool = False,
351
choices: Optional[List[str]] = None,
352
show_default: bool = True,
353
show_choices: bool = True,
354
default: bool = False,
355
stream: Optional[TextIO] = None,
356
) -> bool:
357
"""
358
Prompt for yes/no confirmation.
359
360
Args:
361
prompt: Prompt text
362
console: Console instance
363
password: Hide input (not recommended for confirm)
364
choices: Custom yes/no choices
365
show_default: Show default in prompt
366
show_choices: Show y/n choices
367
default: Default boolean value
368
stream: Input stream
369
370
Returns:
371
True for yes, False for no
372
"""
373
```
374
375
### Spinner Animations
376
377
Animated spinner components for loading indicators.
378
379
```python { .api }
380
class Spinner:
381
"""
382
Animated spinner for loading indicators.
383
384
Args:
385
name: Spinner animation name
386
text: Text to display with spinner
387
style: Style for spinner and text
388
speed: Animation speed multiplier
389
"""
390
def __init__(
391
self,
392
name: str = "dots",
393
text: TextType = "",
394
*,
395
style: Optional[StyleType] = None,
396
speed: float = 1.0,
397
): ...
398
399
def render(self, time: float) -> RenderableType:
400
"""
401
Render spinner at given time.
402
403
Args:
404
time: Current time for animation
405
406
Returns:
407
Renderable spinner frame
408
"""
409
410
# Properties
411
@property
412
def name(self) -> str:
413
"""Get spinner name."""
414
415
@property
416
def frames(self) -> List[str]:
417
"""Get animation frames."""
418
419
@property
420
def interval(self) -> float:
421
"""Get frame interval in seconds."""
422
```
423
424
**Usage Examples:**
425
426
```python
427
from rich.live import Live
428
from rich.console import Console
429
from rich.table import Table
430
from rich.spinner import Spinner
431
from rich.status import Status
432
from rich.prompt import Prompt, IntPrompt, FloatPrompt, Confirm
433
import time
434
435
console = Console()
436
437
# Basic live display
438
def generate_table():
439
"""Generate a sample table."""
440
table = Table()
441
table.add_column("ID")
442
table.add_column("Name")
443
table.add_column("Status")
444
445
table.add_row("1", "Task A", "Running")
446
table.add_row("2", "Task B", "Complete")
447
table.add_row("3", "Task C", "Pending")
448
449
return table
450
451
with Live(generate_table(), refresh_per_second=4) as live:
452
for i in range(40):
453
time.sleep(0.1)
454
# Update with new content
455
table = generate_table()
456
# Add dynamic row
457
table.add_row(str(i+4), f"Dynamic {i}", "Active")
458
live.update(table)
459
460
# Live display with periodic updates
461
from rich.text import Text
462
463
def get_current_time():
464
return Text(f"Current time: {time.strftime('%H:%M:%S')}", style="bold green")
465
466
with Live(get_current_time(), refresh_per_second=1) as live:
467
for _ in range(10):
468
time.sleep(1)
469
live.update(get_current_time())
470
471
# Status with spinner
472
with console.status("Loading data...") as status:
473
time.sleep(2)
474
status.update("Processing data...")
475
time.sleep(2)
476
status.update("Finalizing...")
477
time.sleep(1)
478
479
# Custom status display
480
status = Status(
481
"Custom loading...",
482
spinner="dots",
483
spinner_style="bold blue"
484
)
485
486
with status:
487
time.sleep(3)
488
489
# User input prompts
490
name = Prompt.ask("What's your name?")
491
print(f"Hello {name}!")
492
493
age = IntPrompt.ask("What's your age?", default=25)
494
print(f"You are {age} years old")
495
496
height = FloatPrompt.ask("What's your height in meters?", default=1.75)
497
print(f"Your height is {height}m")
498
499
# Confirmation prompt
500
delete_files = Confirm.ask("Delete all files?")
501
if delete_files:
502
print("Files would be deleted")
503
else:
504
print("Operation cancelled")
505
506
# Prompt with choices
507
color = Prompt.ask(
508
"Choose a color",
509
choices=["red", "green", "blue"],
510
default="blue"
511
)
512
print(f"You chose {color}")
513
514
# Password input
515
password = Prompt.ask("Enter password", password=True)
516
print("Password entered (hidden)")
517
518
# Complex live display with multiple updates
519
from rich.panel import Panel
520
from rich.columns import Columns
521
522
def create_dashboard():
523
"""Create a dashboard layout."""
524
left_panel = Panel("System Status: OK", title="Status")
525
right_panel = Panel(f"Time: {time.strftime('%H:%M:%S')}", title="Clock")
526
527
return Columns([left_panel, right_panel])
528
529
with Live(create_dashboard(), refresh_per_second=1) as live:
530
for i in range(20):
531
time.sleep(0.5)
532
if i % 4 == 0: # Update every 2 seconds
533
live.update(create_dashboard())
534
535
# Progress with live updates
536
from rich.progress import Progress, TaskID
537
538
def live_progress_demo():
539
"""Demonstrate live progress updates."""
540
progress = Progress()
541
542
with Live(progress, refresh_per_second=10) as live:
543
task1 = progress.add_task("Download", total=1000)
544
task2 = progress.add_task("Process", total=1000)
545
546
while not progress.finished:
547
progress.advance(task1, 3)
548
progress.advance(task2, 1)
549
time.sleep(0.02)
550
551
# Live display with error handling
552
def safe_live_display():
553
"""Live display with error handling."""
554
try:
555
with Live("Starting...", transient=True) as live:
556
for i in range(5):
557
live.update(f"Step {i+1}/5")
558
time.sleep(1)
559
live.update("Complete!")
560
time.sleep(1)
561
except KeyboardInterrupt:
562
console.print("Display interrupted by user")
563
564
# Interactive menu using prompts
565
def interactive_menu():
566
"""Create an interactive menu system."""
567
while True:
568
console.print("\n[bold blue]Main Menu[/bold blue]")
569
console.print("1. View Status")
570
console.print("2. Update Settings")
571
console.print("3. Exit")
572
573
choice = IntPrompt.ask("Choose option", choices=[1, 2, 3])
574
575
if choice == 1:
576
console.print("[green]Status: All systems operational[/green]")
577
elif choice == 2:
578
console.print("[yellow]Settings updated[/yellow]")
579
elif choice == 3:
580
if Confirm.ask("Really exit?"):
581
break
582
583
console.print("Goodbye!")
584
585
# Example: interactive_menu()
586
```