-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathconsole_utils.py
More file actions
170 lines (149 loc) Β· 5.16 KB
/
console_utils.py
File metadata and controls
170 lines (149 loc) Β· 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
Console and Rich utility functions for MCP tutorial labs.
This module provides consistent console output formatting across all MCP lab exercises.
"""
from rich.console import Console
from rich.panel import Panel
from rich.prompt import Prompt
from rich.markdown import Markdown
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.syntax import Syntax
from rich.table import Table
from rich.text import Text
import time
import asyncio
# Initialize the global console
console = Console()
def display_header(title: str, subtitle: str = None):
"""Display a formatted header for lab exercises."""
console.print()
console.print(Panel(
f"[bold blue]{title}[/]" + (f"\n[yellow]{subtitle}[/]" if subtitle else ""),
title="π§ MCP Tutorial",
title_align="left",
border_style="blue",
padding=(1, 2)
))
console.print()
def display_info_panel(content: str, title: str = "βΉοΈ Information", style: str = "cyan"):
"""Display information in a styled panel."""
console.print(Panel(
Markdown(content),
title=title,
title_align="left",
border_style=style,
padding=(1, 2),
expand=False
))
console.print()
def display_success_panel(content: str, title: str = "β
Success"):
"""Display success message in a green panel."""
console.print(Panel(
Markdown(content),
title=title,
title_align="left",
border_style="green",
padding=(1, 2),
expand=False
))
console.print()
def display_error_panel(content: str, title: str = "β Error"):
"""Display error message in a red panel."""
console.print(Panel(
Markdown(content),
title=title,
title_align="left",
border_style="red",
padding=(1, 2),
expand=False
))
console.print()
def display_code_panel(code: str, language: str = "python", title: str = "π Code Example"):
"""Display code in a syntax-highlighted panel."""
syntax = Syntax(code, language, theme="monokai", line_numbers=True)
console.print(Panel(
syntax,
title=title,
title_align="left",
border_style="yellow",
padding=(1, 2),
expand=False
))
console.print()
def display_step(step_number: int, title: str, description: str = None):
"""Display a numbered step in the tutorial."""
step_text = f"[bold blue]Step {step_number}:[/] [bold]{title}[/]"
if description:
step_text += f"\n{description}"
console.print(Panel(
step_text,
title=f"π Step {step_number}",
title_align="left",
border_style="magenta",
padding=(1, 2),
expand=False
))
console.print()
def prompt_user(message: str, default: str = None) -> str:
"""Get user input with rich formatting."""
return Prompt.ask(f"[bold green]{message}[/]", default=default)
def prompt_continue(message: str = "Press Enter to continue..."):
"""Pause execution and wait for user to continue."""
Prompt.ask(f"[dim]{message}[/]", default="")
def show_progress(description: str, duration: float = 2.0):
"""Show a progress spinner for the given duration."""
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
) as progress:
task = progress.add_task(description, total=None)
time.sleep(duration)
def create_table(title: str, headers: list, rows: list) -> Table:
"""Create a rich table with the given headers and rows."""
table = Table(title=title, show_header=True, header_style="bold magenta")
for header in headers:
table.add_column(header, style="cyan")
for row in rows:
table.add_row(*row)
return table
def display_table(title: str, headers: list, rows: list):
"""Display a table with rich formatting."""
table = create_table(title, headers, rows)
console.print(table)
console.print()
def display_json_data(data: dict, title: str = "π JSON Data"):
"""Display JSON data in a formatted panel."""
import json
json_str = json.dumps(data, indent=2)
syntax = Syntax(json_str, "json", theme="monokai", line_numbers=True)
console.print(Panel(
syntax,
title=title,
title_align="left",
border_style="cyan",
padding=(1, 2),
expand=False
))
console.print()
def section_separator():
"""Print a visual separator between sections."""
console.print("\n" + "β" * 80 + "\n")
def lab_complete():
"""Display lab completion message."""
console.print(Panel(
"[bold green]π Lab Complete![/]\n\nYou have successfully completed this lab exercise.",
title="β
Congratulations",
title_align="center",
border_style="green",
padding=(1, 2)
))
async def async_show_progress(description: str, duration: float = 2.0):
"""Async version of show_progress."""
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
) as progress:
task = progress.add_task(description, total=None)
await asyncio.sleep(duration)