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

fetch the data on the table to add csv download button #648

Merged
merged 3 commits into from
Dec 8, 2023
Merged
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
37 changes: 37 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 @@ -447,6 +449,41 @@
note.save_note_with_version(storage, study_id, trial_id, req_note_ver, req_note_body)
response.status = 204 # No content
return {}

@app.get("/csv/<study_id:int>")
def download_csv(study_id: int) -> BottleViewReturn:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To follow the original logic, there are some differences as follows:

  • Displaying user_attrs instead of union_user_attrs
  • Displaying params without considering union_search_space and intersection_search_space

# TODO: 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 460 in optuna_dashboard/_app.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_app.py#L456-L460

Added lines #L456 - L460 were not covered by tests

param_names = list(trials[0].params.keys())
union_user_attrs = list(trials[0].user_attrs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

column_names = ["Number", "State", "Value"] + param_names + union_user_attrs

Check warning on line 464 in optuna_dashboard/_app.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_app.py#L462-L464

Added lines #L462 - L464 were not covered by tests

buf = io.StringIO("")
writer = csv.writer(buf)
writer.writerow(column_names)
for frozen_trial in trials:
row = [

Check warning on line 470 in optuna_dashboard/_app.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_app.py#L466-L470

Added lines #L466 - L470 were not covered by tests
frozen_trial.number,
frozen_trial.state,
frozen_trial.values[0]
]
row += [frozen_trial.params[param] for param in param_names]
row += [frozen_trial.user_attrs[attr] for attr in union_user_attrs]
writer.writerow(row)

Check warning on line 477 in optuna_dashboard/_app.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_app.py#L475-L477

Added lines #L475 - L477 were not covered by tests

# TODO: 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 481 in optuna_dashboard/_app.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_app.py#L480-L481

Added lines #L480 - L481 were not covered by tests

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

Check warning on line 485 in optuna_dashboard/_app.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_app.py#L484-L485

Added lines #L484 - L485 were not covered by tests


@app.get("/favicon.ico")
def favicon() -> BottleViewReturn:
Expand Down
11 changes: 11 additions & 0 deletions optuna_dashboard/ts/components/StudyDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import Grid2 from "@mui/material/Unstable_Grid2"
import ChevronRightIcon from "@mui/icons-material/ChevronRight"
import HomeIcon from "@mui/icons-material/Home"
import DownloadIcon from "@mui/icons-material/Download"

import { StudyNote } from "./Note"
import { actionCreator } from "../action"
Expand Down Expand Up @@ -143,6 +144,16 @@ export const StudyDetail: FC<{
content = (
<Card sx={{ margin: theme.spacing(2) }}>
<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>
<TrialTable studyDetail={studyDetail} initialRowsPerPage={50} />
</CardContent>
</Card>
Expand Down
Loading