Skip to content

Commit

Permalink
FIX: Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner committed Feb 16, 2024
1 parent db7f4c3 commit 84c930a
Showing 1 changed file with 57 additions and 56 deletions.
113 changes: 57 additions & 56 deletions mne/report/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -3837,62 +3837,7 @@ def _add_epochs_metadata(self, *, epochs, section, tags, replace):
assert metadata.index.is_unique
index_name = metadata.index.name # store for later use
metadata = metadata.reset_index() # We want "proper" columns only
html = metadata.to_html(
border=0,
index=False,
show_dimensions=True,
justify="unset",
float_format=lambda x: f"{x:.3f}",
classes="table table-hover table-striped "
"table-sm table-responsive small",
na_rep="",
)
del metadata

# Massage the table such that it woks nicely with bootstrap-table
htmls = html.split("\n")
header_pattern = "<th>(.*)</th>"

for idx, html in enumerate(htmls):
if "<table" in html:
htmls[idx] = html.replace(
"<table",
"<table "
'id="mytable" '
'data-toggle="table" '
f'data-unique-id="{index_name}" '
'data-search="true" ' # search / filter
'data-search-highlight="true" '
'data-show-columns="true" ' # show/hide columns
'data-show-toggle="true" ' # allow card view
'data-show-columns-toggle-all="true" '
'data-click-to-select="true" '
'data-show-copy-rows="true" '
'data-show-export="true" ' # export to a file
'data-export-types="[csv]" '
'data-export-options=\'{"fileName": "metadata"}\' '
'data-icon-size="sm" '
'data-height="400"',
)
continue
elif "<tr" in html:
# Add checkbox for row selection
htmls[idx] = (
f"{html}\n" f'<th data-field="state" data-checkbox="true"></th>'
)
continue

col_headers = re.findall(pattern=header_pattern, string=html)
if col_headers:
# Make columns sortable
assert len(col_headers) == 1
col_header = col_headers[0]
htmls[idx] = html.replace(
"<th>",
f'<th data-field="{col_header.lower()}" ' f'data-sortable="true">',
)

html = "\n".join(htmls)
html = _df_bootstrap_table(df=metadata, index_name=index_name)
self._add_html_element(
div_klass="epochs",
tags=tags,
Expand Down Expand Up @@ -4389,3 +4334,59 @@ def __call__(self, block, block_vars, gallery_conf):
def copyfiles(self, *args, **kwargs):
for key, value in self.files.items():
copyfile(key, value)


def _df_bootstrap_table(*, df, index_name):
html = df.to_html(
border=0,
index=False,
show_dimensions=True,
justify="unset",
float_format=lambda x: f"{x:.3f}",
classes="table table-hover table-striped table-sm table-responsive small",
na_rep="",
)
htmls = html.split("\n")
header_pattern = "<th>(.*)</th>"

for idx, html in enumerate(htmls):
if "<table" in html:
htmls[idx] = html.replace(
"<table",
"<table "
'id="mytable" '
'data-toggle="table" '
f'data-unique-id="{index_name}" '
'data-search="true" ' # search / filter
'data-search-highlight="true" '
'data-show-columns="true" ' # show/hide columns
'data-show-toggle="true" ' # allow card view
'data-show-columns-toggle-all="true" '
'data-click-to-select="true" '
'data-show-copy-rows="true" '
'data-show-export="true" ' # export to a file
'data-export-types="[csv]" '
'data-export-options=\'{"fileName": "metadata"}\' '
'data-icon-size="sm" '
'data-height="400"',
)
continue
elif "<tr" in html:
# Add checkbox for row selection
htmls[idx] = (
f"{html}\n" f'<th data-field="state" data-checkbox="true"></th>'
)
continue

col_headers = re.findall(pattern=header_pattern, string=html)
if col_headers:
# Make columns sortable
assert len(col_headers) == 1
col_header = col_headers[0]
htmls[idx] = html.replace(
"<th>",
f'<th data-field="{col_header.lower()}" ' f'data-sortable="true">',
)

html = "\n".join(htmls)
return html

0 comments on commit 84c930a

Please sign in to comment.