Skip to content

Commit 0d12a35

Browse files
committed
changes
1 parent e32b0f0 commit 0d12a35

3 files changed

Lines changed: 56 additions & 12 deletions

File tree

trackio/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
11
from pathlib import Path
2-
import os
2+
33
from .storage import TrackioStorage
44
from .ui import launch_ui
55

66
__version__ = Path(__file__).parent.joinpath("version.txt").read_text().strip()
77

88
_current_run = None
99

10+
1011
class Run:
1112
def __init__(self, project, name, config):
1213
self.project = project
1314
self.name = name
1415
self.config = config
1516
self.storage = TrackioStorage(project, name, config)
17+
1618
def log(self, metrics):
1719
self.storage.log(metrics)
20+
1821
def finish(self):
1922
self.storage.finish()
2023

24+
2125
def init(project, name=None, config=None):
2226
global _current_run
2327
_current_run = Run(project, name, config)
2428
return _current_run
2529

30+
2631
def log(metrics):
2732
if _current_run is None:
2833
raise RuntimeError("Call trackio.init() before log().")
2934
_current_run.log(metrics)
3035

36+
3137
def finish():
3238
if _current_run is None:
3339
raise RuntimeError("Call trackio.init() before finish().")
3440
_current_run.finish()
3541
global _current_run
3642
_current_run = None
3743

44+
3845
def ui():
3946
launch_ui()

trackio/storage.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from datasets import Dataset
33
import pandas as pd
44

5+
56
class TrackioStorage:
67
def __init__(self, project, name, config):
78
self.project = project
@@ -14,12 +15,15 @@ def __init__(self, project, name, config):
1415
self.config_path = os.path.join(self.dir, "config.json")
1516
# Save config immediately
1617
import json
18+
1719
with open(self.config_path, "w") as f:
1820
json.dump(self.config, f, indent=2)
21+
1922
def log(self, metrics):
2023
self.logs.append(metrics)
24+
2125
def finish(self):
2226
if self.logs:
2327
df = pd.DataFrame(self.logs)
2428
ds = Dataset.from_pandas(df)
25-
ds.to_parquet(self.run_path)
29+
ds.to_parquet(self.run_path)

trackio/ui.py

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
1-
import gradio as gr
2-
import os
31
import json
2+
import os
3+
4+
import gradio as gr
45
import pandas as pd
56
import plotly.express as px
67

78
TRACKIO_DIR = "trackio"
89

10+
911
def get_projects():
1012
if not os.path.exists(TRACKIO_DIR):
1113
return []
12-
return [d for d in os.listdir(TRACKIO_DIR) if os.path.isdir(os.path.join(TRACKIO_DIR, d))]
14+
return [
15+
d
16+
for d in os.listdir(TRACKIO_DIR)
17+
if os.path.isdir(os.path.join(TRACKIO_DIR, d))
18+
]
19+
1320

1421
def get_runs(project):
1522
project_dir = os.path.join(TRACKIO_DIR, project)
1623
if not os.path.exists(project_dir):
1724
return []
18-
return [d for d in os.listdir(project_dir) if os.path.isdir(os.path.join(project_dir, d))]
25+
return [
26+
d
27+
for d in os.listdir(project_dir)
28+
if os.path.isdir(os.path.join(project_dir, d))
29+
]
30+
1931

2032
def load_run_data(project, run):
2133
run_dir = os.path.join(TRACKIO_DIR, project, run)
@@ -30,37 +42,58 @@ def load_run_data(project, run):
3042
config = json.load(f)
3143
return df, config
3244

45+
3346
def plot_metrics(df):
3447
if df is None or df.empty:
3548
return None
3649
plots = []
37-
numeric_cols = df.select_dtypes(include='number').columns
50+
numeric_cols = df.select_dtypes(include="number").columns
3851
for col in numeric_cols:
3952
fig = px.line(df, y=col, title=col)
4053
plots.append(fig)
4154
return plots
4255

56+
4357
def update_runs(project):
4458
return gr.Dropdown.update(choices=get_runs(project), value=None)
4559

60+
4661
def update_dashboard(project, run):
4762
if not project or not run:
48-
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
63+
return (
64+
gr.update(visible=False),
65+
gr.update(visible=False),
66+
gr.update(visible=False),
67+
)
4968
df, config = load_run_data(project, run)
5069
plots = plot_metrics(df)
5170
config_str = json.dumps(config, indent=2)
5271
return plots, gr.JSON.update(value=config), gr.update(visible=True)
5372

73+
5474
def launch_ui():
5575
with gr.Blocks() as demo:
5676
gr.Markdown("# Trackio Dashboard")
5777
with gr.Row():
5878
project_dd = gr.Dropdown(label="Project", choices=get_projects())
5979
run_dd = gr.Dropdown(label="Run", choices=[])
6080
with gr.Row():
61-
plot_output = gr.Plot(label="Metrics", visible=False, show_label=True, elem_id="metrics-plot", interactive=True, scale=2)
81+
plot_output = gr.Plot(
82+
label="Metrics",
83+
visible=False,
84+
show_label=True,
85+
elem_id="metrics-plot",
86+
interactive=True,
87+
scale=2,
88+
)
6289
config_output = gr.JSON(label="Config", visible=False)
6390
# Events
64-
project_dd.change(fn=lambda p: update_runs(p), inputs=project_dd, outputs=run_dd)
65-
run_dd.change(fn=lambda p, r: update_dashboard(p, r), inputs=[project_dd, run_dd], outputs=[plot_output, config_output, plot_output])
66-
demo.launch()
91+
project_dd.change(
92+
fn=lambda p: update_runs(p), inputs=project_dd, outputs=run_dd
93+
)
94+
run_dd.change(
95+
fn=lambda p, r: update_dashboard(p, r),
96+
inputs=[project_dd, run_dd],
97+
outputs=[plot_output, config_output, plot_output],
98+
)
99+
demo.launch()

0 commit comments

Comments
 (0)