Skip to content

Commit b02046a

Browse files
JamshedAli18abidlabsgradio-pr-bot
authored
Add JSON export button for line plots and upgrade gradio dependency (#378)
Co-authored-by: = <=> Co-authored-by: Abubakar Abid <abubakar@huggingface.co> Co-authored-by: Abubakar Abid <islamrealm@gmail.com> Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
1 parent ce7345a commit b02046a

4 files changed

Lines changed: 52 additions & 7 deletions

File tree

.changeset/two-seas-burn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"trackio": patch
3+
---
4+
5+
feat:Add JSON export button for line plots and upgrade gradio dependency

examples/transformers-integration.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88
# ///
99

1010
import os
11-
from datasets import Dataset
12-
from transformers import AutoModelForSequenceClassification, AutoTokenizer, Trainer, TrainingArguments
11+
1312
import huggingface_hub
13+
from datasets import Dataset
14+
from transformers import (
15+
AutoModelForSequenceClassification,
16+
AutoTokenizer,
17+
Trainer,
18+
TrainingArguments,
19+
)
1420

1521
model_name = "distilbert-base-uncased"
1622
tokenizer = AutoTokenizer.from_pretrained(model_name)
@@ -38,7 +44,6 @@
3844
report_to="trackio",
3945
push_to_hub=bool(hub_model_id),
4046
hub_model_id=hub_model_id,
41-
4247
),
4348
train_dataset=dataset,
4449
)
@@ -47,4 +52,3 @@
4752
trainer.push_to_hub()
4853

4954
print(f"Model pushed to Hub, available at: https://huggingface.co/{hub_model_id}")
50-

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ requires-python = ">=3.10"
1414
dependencies = [
1515
"pandas<3.0.0",
1616
"huggingface-hub<2.0.0",
17-
"gradio[oauth]>=6.2.0,<7.0.0",
17+
"gradio[oauth]>=6.3.0,<7.0.0",
1818
"numpy<3.0.0",
1919
"pillow<12.0.0",
2020
"orjson>=3.0,<4.0.0",

trackio/ui/main.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,24 @@
8686
"""
8787

8888

89+
def generate_download_plot_js(metric_name: str) -> str:
90+
"""Generate JavaScript code for downloading plot data as JSON."""
91+
safe_filename = metric_name.replace("/", "_").replace("\\", "_")
92+
return f"""(data) => {{
93+
const jsonStr = JSON.stringify(data.value, null, 2);
94+
const blob = new Blob([jsonStr], {{type: 'application/json'}});
95+
const url = URL.createObjectURL(blob);
96+
const a = document.createElement('a');
97+
a.href = url;
98+
a.download = '{safe_filename}.json';
99+
document.body.appendChild(a);
100+
a.click();
101+
document.body.removeChild(a);
102+
URL.revokeObjectURL(url);
103+
return [];
104+
}}"""
105+
106+
89107
def get_runs(project) -> list[str]:
90108
if not project:
91109
return []
@@ -1049,6 +1067,7 @@ def update_dashboard(
10491067
x_lim_value,
10501068
)
10511069
if not metric_df.empty:
1070+
download_btn = gr.Button("📄")
10521071
plot = gr.LinePlot(
10531072
downsampled_df,
10541073
x=x_column,
@@ -1060,10 +1079,16 @@ def update_dashboard(
10601079
title=metric_name,
10611080
key=f"plot-{metric_idx}",
10621081
preserved_by_key=None,
1063-
buttons=["fullscreen", "export"],
1082+
buttons=[download_btn, "fullscreen", "export"],
10641083
x_lim=updated_x_lim,
10651084
min_width=400,
10661085
)
1086+
download_btn.click(
1087+
None,
1088+
inputs=[plot],
1089+
outputs=[],
1090+
js=generate_download_plot_js(metric_name),
1091+
)
10671092
plot.select(
10681093
update_x_lim,
10691094
outputs=x_lim,
@@ -1114,6 +1139,7 @@ def update_dashboard(
11141139
x_lim_value,
11151140
)
11161141
if not metric_df.empty:
1142+
download_btn = gr.Button("📄")
11171143
plot = gr.LinePlot(
11181144
downsampled_df,
11191145
x=x_column,
@@ -1125,10 +1151,20 @@ def update_dashboard(
11251151
title=metric_name,
11261152
key=f"plot-{metric_idx}",
11271153
preserved_by_key=None,
1128-
buttons=["fullscreen", "export"],
1154+
buttons=[
1155+
download_btn,
1156+
"fullscreen",
1157+
"export",
1158+
],
11291159
x_lim=updated_x_lim,
11301160
min_width=400,
11311161
)
1162+
download_btn.click(
1163+
None,
1164+
inputs=[plot],
1165+
outputs=[],
1166+
js=generate_download_plot_js(metric_name),
1167+
)
11321168
plot.select(
11331169
update_x_lim,
11341170
outputs=x_lim,

0 commit comments

Comments
 (0)