Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to download a csv file #722

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions optuna_dashboard/_app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import csv
import functools
import io
import logging
import os
import typing
Expand Down Expand Up @@ -152,7 +154,7 @@
storage=storage, study_name=dst_study_name, directions=src_study.directions
)
dst_study.add_trials(src_study.get_trials(deepcopy=False))
note.copy_notes(storage, src_study, dst_study)

Check warning on line 157 in optuna_dashboard/_app.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_app.py#L157

Added line #L157 was not covered by tests
except DuplicatedStudyError:
response.status = 400 # Bad request
return {"reason": f"study_name={dst_study_name} is duplicaated"}
Expand Down Expand Up @@ -449,6 +451,60 @@
response.status = 204 # No content
return {}

@app.get("/csv/<study_id:int>")
def download_csv(study_id: int) -> BottleViewReturn:
# Create a CSV file
summary = get_study_summary(storage, study_id)
if summary is None:
response.status = 404 # Not found
return {"reason": f"study_id={study_id} is not found"}
trials = get_trials(storage, study_id)

Check warning on line 461 in optuna_dashboard/_app.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_app.py#L457-L461

Added lines #L457 - L461 were not covered by tests

param_names = []
user_attr_names = []
for trial in trials:
for param_name in trial.params.keys():
if param_name not in param_names:
param_names.append(param_name)
for attr_name in trial.user_attrs.keys():
if attr_name not in user_attr_names:
user_attr_names.append(attr_name)

Check warning on line 471 in optuna_dashboard/_app.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_app.py#L463-L471

Added lines #L463 - L471 were not covered by tests

param_names_heading = [f"Param {x}" for x in param_names]
user_attr_names_heading = [f"UserAttribute {x}" for x in user_attr_names]
value_heading = ["Value"]
if len(trials[0].values) > 1:
value_heading = [f"Objective {x}" for x in range(len(trials[0].values))]
column_names = (

Check warning on line 478 in optuna_dashboard/_app.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_app.py#L473-L478

Added lines #L473 - L478 were not covered by tests
["Number", "State"] + value_heading + param_names_heading + user_attr_names_heading
)

buf = io.StringIO("")
writer = csv.writer(buf)
writer.writerow(column_names)
for frozen_trial in trials:
row = [frozen_trial.number, frozen_trial.state.name]
row += frozen_trial.values
for param_name in param_names:
if param_name in frozen_trial.params.keys():
row += [frozen_trial.params[param_name]]

Check warning on line 490 in optuna_dashboard/_app.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_app.py#L482-L490

Added lines #L482 - L490 were not covered by tests
else:
row += [None]
for attr_name in user_attr_names:
if attr_name in frozen_trial.user_attrs.keys():
row += [frozen_trial.user_attrs[attr_name]]

Check warning on line 495 in optuna_dashboard/_app.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_app.py#L492-L495

Added lines #L492 - L495 were not covered by tests
else:
row += [None]
writer.writerow(row)

Check warning on line 498 in optuna_dashboard/_app.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_app.py#L497-L498

Added lines #L497 - L498 were not covered by tests

# Set response headers
response.headers["Content-Type"] = "text/csv; chatset=cp932"
response.headers["Content-Disposition"] = f"attachment; filename=trials_{study_id}.csv"

Check warning on line 502 in optuna_dashboard/_app.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_app.py#L501-L502

Added lines #L501 - L502 were not covered by tests

# Response body
buf.seek(0)
return buf.read()

Check warning on line 506 in optuna_dashboard/_app.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_app.py#L505-L506

Added lines #L505 - L506 were not covered by tests

@app.get("/favicon.ico")
def favicon() -> BottleViewReturn:
use_gzip = "gzip" in request.headers["Accept-Encoding"]
Expand Down
38 changes: 33 additions & 5 deletions optuna_dashboard/ts/components/StudyDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,39 @@ export const StudyDetail: FC<{
content = <TrialList studyDetail={studyDetail} />
} else if (page === "trialTable") {
content = (
<Card sx={{ margin: theme.spacing(2) }}>
<CardContent>
<TrialTable studyDetail={studyDetail} initialRowsPerPage={50} />
</CardContent>
</Card>
<Box sx={{ display: "flex", width: "100%", flexDirection: "column" }}>
<Card sx={{ margin: theme.spacing(2) }}>
<CardContent>
<TrialTable studyDetail={studyDetail} initialRowsPerPage={50} />
</CardContent>
</Card>
<Typography variant="h5" sx={{ margin: theme.spacing(2) }}>
Download CSV File
</Typography>{" "}
<Card
sx={{
margin: theme.spacing(2),
width: 80,
height: 80,
display: "flex",
justifyContent: "center",
alignItems: "center",
}}
>
<CardContent>
<IconButton
aria-label="download csv"
size="small"
color="inherit"
download={`trials_${studyDetail?.id}.csv`}
sx={{ margin: "auto 0" }}
href={`/csv/${studyDetail?.id}`}
>
<DownloadIcon />
</IconButton>
</CardContent>
</Card>
</Box>
)
} else if (page === "note" && studyDetail !== null) {
content = (
Expand Down
Loading