Skip to content

Commit

Permalink
Merge pull request #464 from dyson-ai/feature/publish
Browse files Browse the repository at this point in the history
Feature/publish
  • Loading branch information
blooop authored Dec 14, 2024
2 parents 56c7218 + 8aed27b commit 5cf1d3c
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 94 deletions.
2 changes: 1 addition & 1 deletion bencher/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
from .results.bench_result import BenchResult
from .results.panel_result import PanelResult
from .results.holoview_result import ReduceType, HoloviewResult
from .bench_report import BenchReport
from .bench_report import BenchReport, GithubPagesCfg
from .job import Executors
from .video_writer import VideoWriter, add_image
from .class_enum import ClassEnum, ExampleEnum
Expand Down
49 changes: 42 additions & 7 deletions bencher/bench_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@
from pathlib import Path
import tempfile
from threading import Thread
from dataclasses import dataclass

import panel as pn
from bencher.results.bench_result import BenchResult
from bencher.bench_plot_server import BenchPlotServer
from bencher.bench_cfg import BenchRunCfg


@dataclass
class GithubPagesCfg:
github_user: str
repo_name: str
folder_name: str = "report"
branch_name: str = "gh-pages"


class BenchReport(BenchPlotServer):
def __init__(
self,
Expand Down Expand Up @@ -111,6 +120,39 @@ def show(self, run_cfg: BenchRunCfg = None) -> Thread: # pragma: no cover

return BenchPlotServer().plot_server(self.bench_name, run_cfg, self.pane)

def publish_gh_pages(
self,
github_user: str,
repo_name: str,
folder_name: str = "report",
branch_name: str = "gh-pages",
) -> str: # pragma: no cover
remote = f"https://github.com/{github_user}/{repo_name}.git"
publish_url = f"https://{github_user}.github.io/{repo_name}/{folder_name}"

with tempfile.TemporaryDirectory() as td:
directory = td
report_path = self.save(
directory + f"/{folder_name}/",
filename="index.html",
in_html_folder=False,
)
logging.info(f"created report at: {report_path.absolute()}")

cd_dir = f"cd {directory} &&"
# TODO DON'T OVERWRITE EVERYTHING
os.system(f"{cd_dir} git init")
os.system(f"{cd_dir} git checkout -b {branch_name}")
os.system(f"{cd_dir} git add {folder_name}/index.html")
os.system(f'{cd_dir} git commit -m "publish {branch_name}"')
os.system(f"{cd_dir} git remote add origin {remote}")
os.system(f"{cd_dir} git push --set-upstream origin {branch_name} -f")

logging.info("Published report @")
logging.info(publish_url)

return publish_url

def publish(
self, remote_callback: Callable, branch_name: str = None, debug: bool = False
) -> str: # pragma: no cover
Expand Down Expand Up @@ -155,10 +197,3 @@ def publish_args(branch_name) -> Tuple[str, str]:
logging.info(publish_url)

return publish_url

# @staticmethod
# def publish_github(github_user: str, repo_name: str, branch_name: str) -> Tuple[str, str]:
# return (
# f"https://github.com/{github_user}/{repo_name}.git",
# f"https://github.com/{github_user}/{repo_name}/blob/{branch_name}",
# )
15 changes: 12 additions & 3 deletions bencher/bench_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from bencher.bench_cfg import BenchRunCfg, BenchCfg
from bencher.variables.parametrised_sweep import ParametrizedSweep
from bencher.bencher import Bench
from bencher.bench_report import BenchReport
from bencher.bench_report import BenchReport, GithubPagesCfg
from copy import deepcopy


Expand Down Expand Up @@ -47,7 +47,12 @@ def from_parametrized_sweep(
run_cfg: BenchRunCfg = BenchRunCfg(),
report: BenchReport = BenchReport(),
):
return Bench(f"bench_{class_instance.name}", class_instance, run_cfg=run_cfg, report=report)
return Bench(
f"bench_{class_instance.name}",
class_instance,
run_cfg=run_cfg,
report=report,
)

def add_run(self, bench_fn: Benchable) -> None:
self.bench_fns.append(bench_fn)
Expand Down Expand Up @@ -125,7 +130,11 @@ def show_publish(self, report: BenchReport, show: bool, publish: bool, save: boo
if save:
report.save_index()
if publish and self.publisher is not None:
report.publish(remote_callback=self.publisher, debug=debug)
if isinstance(self.publisher, GithubPagesCfg):
p = self.publisher
report.publish_gh_pages(p.github_user, p.repo_name, p.folder_name, p.branch_name)
else:
report.publish(remote_callback=self.publisher, debug=debug)
if show:
self.servers.append(report.show())

Expand Down
31 changes: 31 additions & 0 deletions bencher/example/example_publish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""This file has an example of how to publish a report to github pages"""

import math
import bencher as bch


class SimpleFloat(bch.ParametrizedSweep):
theta = bch.FloatSweep(
default=0, bounds=[0, math.pi], doc="Input angle", units="rad", samples=30
)
out_sin = bch.ResultVar(units="v", doc="sin of theta")

def __call__(self, **kwargs):
self.update_params_from_kwargs(**kwargs)
self.out_sin = math.sin(self.theta)
return super().__call__(**kwargs)


if __name__ == "__main__":
# publish from report
bench = SimpleFloat().to_bench()
bench.plot_sweep()
bench.report.publish_gh_pages(github_user="blooop", repo_name="reports", folder_name="r5")
# TODO DON'T OVERWRITE ^ EXAMPLE WHEN RUNNING CODE BELOW

# publish from benchrunner
bench_r = bch.BenchRunner(
"SimpleFloat", publisher=bch.GithubPagesCfg("blooop", "reports", "r6")
)
bench_r.add_bench(SimpleFloat())
bench_r.run(level=3, show=True, publish=True)
Loading

0 comments on commit 5cf1d3c

Please sign in to comment.