Skip to content

Commit 1fc5143

Browse files
committed
improve pretty print
1 parent 972d16f commit 1fc5143

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed

skore-hub-project/src/skore_hub_project/authentication/login.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from logging import getLogger
55

66
from rich.align import Align
7+
from rich.live import Live
78
from rich.panel import Panel
89

910
from skore_hub_project import console
@@ -31,8 +32,8 @@ def login(*, timeout: int = 600) -> None:
3132
logger.debug(f"Already logged in {URI} with {type(credentials)}.")
3233
console.print(
3334
Panel(
34-
Align.center("[blink]Already logged in."),
35-
title="[cyan]Login to [bold]Skore Hub",
35+
Align.center("Already logged in."),
36+
title="[cyan]Login to [b]Skore Hub",
3637
border_style="cyan",
3738
padding=1,
3839
)
@@ -43,36 +44,25 @@ def login(*, timeout: int = 600) -> None:
4344
try:
4445
credentials = APIKey()
4546
except KeyError:
46-
console.print(
47-
Panel(
48-
Align.center(
49-
"Falling back to interactive authentication for the session.\n"
50-
"We recommend that you set up an API key via [url](coming soon) "
51-
"and use it to log in."
52-
),
53-
title="[cyan]Login to [bold]Skore Hub",
54-
subtitle="[dark_orange bold]API key not detected",
55-
border_style="dark_orange",
56-
padding=1,
57-
)
58-
)
47+
with Live(console=console, auto_refresh=False) as live:
48+
credentials = Token(timeout=timeout, live=live)
5949

60-
credentials = Token(timeout=timeout)
61-
62-
console.print(
63-
Panel(
64-
Align.center("[blink]Login successful."),
65-
title="[cyan]Login to [b]Skore Hub",
66-
border_style="cyan",
67-
padding=1,
50+
live.update(
51+
Panel(
52+
Align.center(
53+
"Login successful using [b]interactive authentication."
54+
),
55+
title="[cyan]Login to [bold]Skore Hub",
56+
border_style="cyan",
57+
padding=1,
58+
)
6859
)
69-
)
60+
live.refresh()
7061
else:
7162
console.print(
7263
Panel(
73-
Align.center("[blink]Login successful."),
64+
Align.center("Login successful using [b]API key."),
7465
title="[cyan]Login to [bold]Skore Hub",
75-
subtitle="[cyan bold]API key detected",
7666
border_style="cyan",
7767
padding=1,
7868
)

skore-hub-project/src/skore_hub_project/authentication/token.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
from datetime import datetime, timezone
66
from threading import RLock
77
from time import sleep
8+
from typing import cast
89
from urllib.parse import urljoin
910
from webbrowser import open as open_webbrowser
1011

1112
from httpx import HTTPStatusError, TimeoutException
13+
from rich.align import Align
14+
from rich.console import Group
15+
from rich.live import Live
16+
from rich.panel import Panel
17+
from rich.text import Text
1218

1319
from skore_hub_project import console
1420
from skore_hub_project.authentication.uri import URI
@@ -196,17 +202,28 @@ class Token:
196202
Refresh the token on-the-fly if necessary.
197203
"""
198204

199-
def __init__(self, *, timeout: int = 600) -> None:
205+
def __init__(self, *, timeout: int = 600, live: Live | None = None) -> None:
200206
url, device_code, user_code = get_oauth_device_login()
201-
202-
console.print(
203-
(
204-
f"Opening browser for interactive authentication; "
205-
f"if this fails, please visit:\n{url}"
207+
panel = Panel(
208+
Align.center(
209+
"[b]API key not detected.[/b]\n\n"
210+
"Starting interactive authentication for the session.\n"
211+
"[i]We recommend that you set up an API key via url (coming soon) and "
212+
"use it to log in.[/i]\n\n"
213+
"Opening browser for interactive authentication; if this fails, "
214+
f"please visit:\n[link={url}]{url}[/link]"
206215
),
207-
soft_wrap=True,
216+
title="[cyan]Login to [bold]Skore Hub",
217+
border_style="cyan",
218+
padding=1,
208219
)
209220

221+
if live:
222+
live.update(panel)
223+
live.refresh()
224+
else:
225+
console.print(panel, soft_wrap=True)
226+
210227
open_webbrowser(url)
211228

212229
get_oauth_device_code_probe(device_code, timeout=timeout)

0 commit comments

Comments
 (0)