11import contextvars
2+ import webbrowser
23from pathlib import Path
34
45from gradio_client import Client
56
67from trackio .run import Run
7- from trackio .ui import launch_gradio
8+ from trackio .ui import demo
9+ from trackio .utils import TRACKIO_DIR , block_except_in_notebook
810
911__version__ = Path (__file__ ).parent .joinpath ("version.txt" ).read_text ().strip ()
1012
1113
1214current_run : contextvars .ContextVar [Run | None ] = contextvars .ContextVar (
1315 "current_run" , default = None
1416)
15-
17+ current_project : contextvars .ContextVar [str | None ] = contextvars .ContextVar (
18+ "current_project" , default = None
19+ )
1620current_server : contextvars .ContextVar [str | None ] = contextvars .ContextVar (
1721 "current_server" , default = None
1822)
1923
2024config = {}
2125
2226
23- def init (project , name = None , config = None ):
27+ def init (project : str , name : str | None = None , config : dict | None = None ) -> Run :
2428 if not current_server .get ():
25- url = launch_gradio ( show_api = False , inline = False , quiet = True )
26- print ( f"* Trackio server launched at: { url } " )
27- print ( f"** View project dashboard at: { url } ?project= { project } " )
29+ _ , url , _ = demo . launch (
30+ show_api = False , inline = False , quiet = True , prevent_thread_lock = True
31+ )
2832 current_server .set (url )
2933 else :
3034 url = current_server .get ()
35+ if current_project .get () is None or current_project .get () != project :
36+ print (f"* Trackio project initialized: { project } " )
37+ print (f"* Trackio metrics logged to: { TRACKIO_DIR } " )
38+ print (
39+ f'\n * View dashboard by running in your terminal: trackio show --project "{ project } "'
40+ )
41+ print (f'* or by running in Python: trackio.show(project="{ project } ")' )
42+
43+ current_project .set (project )
3144 client = Client (url , verbose = False )
3245 run = Run (project = project , client = client , name = name , config = config )
3346 current_run .set (run )
3447 globals ()["config" ] = run .config
48+ return run
3549
3650
37- def log (metrics ) :
51+ def log (metrics : dict ) -> None :
3852 if current_run .get () is None :
3953 raise RuntimeError ("Call trackio.init() before log()." )
4054 current_run .get ().log (metrics )
@@ -44,3 +58,14 @@ def finish():
4458 if current_run .get () is None :
4559 raise RuntimeError ("Call trackio.init() before finish()." )
4660 current_run .get ().finish ()
61+
62+
63+ def show (project : str | None = None ):
64+ _ , url , share_url = demo .launch (
65+ show_api = False , quiet = True , inline = False , prevent_thread_lock = True
66+ )
67+ base_url = share_url + "/" if share_url else url
68+ dashboard_url = base_url + f"?project={ project } " if project else base_url
69+ print (f"* Trackio UI launched at: { dashboard_url } " )
70+ webbrowser .open (dashboard_url )
71+ block_except_in_notebook ()
0 commit comments