Skip to content

Commit

Permalink
ENH: Always write sg_execution_times and make DataTable (#1198)
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner committed Sep 22, 2023
1 parent 7e12f4f commit e2903c9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
57 changes: 43 additions & 14 deletions sphinx_gallery/gen_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from datetime import timedelta, datetime
from difflib import get_close_matches
from importlib import import_module
from pathlib import Path
import re
import os
import pathlib
Expand Down Expand Up @@ -805,8 +806,6 @@ def write_computation_times(gallery_conf, target_dir, costs):
List of dicts of computation costs and paths, see gen_rst.py for details.
"""
total_time = sum(cost["t"] for cost in costs)
if total_time == 0:
return
if target_dir is None: # all galleries together
out_dir = gallery_conf["src_dir"]
where = "all galleries"
Expand All @@ -818,9 +817,10 @@ def write_computation_times(gallery_conf, target_dir, costs):
kind = "rst"
ref_extra = f'{where.replace(os.path.sep, "_")}_'
new_ref = f"sphx_glr_{ref_extra}sg_execution_times"
with codecs.open(
os.path.join(out_dir, "sg_execution_times.rst"), "w", encoding="utf-8"
) as fid:
out_file = Path(out_dir) / "sg_execution_times.rst"
if out_file.is_file() and total_time == 0: # a re-run
return
with out_file.open("w", encoding="utf-8") as fid:
fid.write(SPHX_GLR_COMP_TIMES.format(new_ref))
fid.write(
f"**{_sec_to_readable(total_time)}** total execution time for "
Expand All @@ -832,15 +832,44 @@ def write_computation_times(gallery_conf, target_dir, costs):
kind=kind,
)
del costs
hline = "".join(("+" + "-" * (length + 2)) for length in lens) + "+\n"
fid.write(hline)
format_str = "".join(f"| {{{ii}}} " for ii in range(len(lines[0]))) + "|\n"
for line in lines:
line = [ll.ljust(len_) for ll, len_ in zip(line, lens)]
text = format_str.format(*line)
assert len(text) == len(hline)
fid.write(text)
fid.write(hline)
# https://datatables.net/examples/styling/bootstrap5.html
fid.write( # put it in a container to make the scoped style work
"""\
.. container::
.. raw:: html
<style scoped>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
</style>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
<script type="text/javascript" class="init">
$(document).ready( function () {
$('table.sg-datatable').DataTable({order: [[1, 'desc']]});
} );
</script>
.. list-table::
:header-rows: 1
:class: table table-striped sg-datatable
* - Example
- Time
- Mem (MB)
""" # noqa: E501
)
# Need at least one entry or Sphinx complains
for ex, t, mb in lines or [["N/A", "N/A", "N/A"]]:
fid.write(
f"""\
* - {ex}
- {t}
- {mb.rsplit(maxsplit=1)[0]}
"""
) # remove the "MB" from the right


def write_api_entries(app, what, name, obj, options, lines):
Expand Down
6 changes: 3 additions & 3 deletions sphinx_gallery/tests/test_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,11 +691,11 @@ def test_rebuild(tmpdir_factory, sphinx_app):
new_app.build(False, [])
status = new_app._status.getvalue()
lines = [line for line in status.split("\n") if "0 removed" in line]
# XXX on AppVeyor this can be 13
# XXX on Windows this can be more
if sys.platform.startswith("win"):
assert (
re.match(
".*[0|1] added, [1-9][0-3]? changed, 0 removed$.*",
".*[0|1] added, ([1-9]|1[0-4]) changed, 0 removed$.*",
status,
re.MULTILINE | re.DOTALL,
)
Expand All @@ -704,7 +704,7 @@ def test_rebuild(tmpdir_factory, sphinx_app):
else:
assert (
re.match(
".*[0|1] added, [1-9] changed, 0 removed$.*",
".*[0|1] added, ([1-9]|10) changed, 0 removed$.*",
status,
re.MULTILINE | re.DOTALL,
)
Expand Down
5 changes: 3 additions & 2 deletions sphinx_gallery/tests/test_gen_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,9 @@ def test_backreferences_dir_pathlib_config(sphinx_app_wrapper):
fill_gallery_conf_defaults(app, app.config, check_keys=False)


def test_write_computation_times_noop():
write_computation_times(None, None, [dict(t=0)])
def test_write_computation_times_noop(sphinx_app_wrapper):
app = sphinx_app_wrapper.create_sphinx_app()
write_computation_times(app.config.sphinx_gallery_conf, None, [])


def test_write_api_usage_noop(sphinx_app_wrapper):
Expand Down

0 comments on commit e2903c9

Please sign in to comment.