Skip to content

Commit 9fcc510

Browse files
committed
Addressing comments
1 parent c217577 commit 9fcc510

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

mne/report/report.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,6 +2222,63 @@ def remove(self, *, title=None, tags=None, remove_all=False):
22222222
def _add_or_replace(self, *, title, section, tags, html_partial, replace=False):
22232223
"""Append HTML content report, or replace it if it already exists.
22242224
2225+
Parameters
2226+
----------
2227+
title : str
2228+
The title entry.
2229+
%(section_report)s
2230+
tags : tuple of str
2231+
The tags associated with the added element.
2232+
html_partial : callable
2233+
Callable that renders a HTML string, called as::
2234+
html_partial(id_=...)
2235+
replace : bool
2236+
Whether to replace existing content if the title and section match.
2237+
If an object is replaced, its dom_id is preserved.
2238+
"""
2239+
assert callable(html_partial), type(html_partial)
2240+
2241+
# Temporarily set HTML and dom_id to dummy values
2242+
new_content = _ContentElement(
2243+
name=title, section=section, dom_id="", tags=tags, html=""
2244+
)
2245+
2246+
append = True
2247+
if replace:
2248+
matches = [
2249+
ii
2250+
for ii, element in enumerate(self._content)
2251+
if (element.name, element.section) == (title, section)
2252+
]
2253+
if matches:
2254+
dom_id = self._content[matches[-1]].dom_id
2255+
self._content[matches[-1]] = new_content
2256+
append = False
2257+
if append:
2258+
dom_id = self._get_dom_id(section=section, title=title)
2259+
self._content.append(new_content)
2260+
new_content.dom_id = dom_id
2261+
new_content.html = html_partial(id_=dom_id)
2262+
assert isinstance(new_content.html, str), type(new_content.html)
2263+
2264+
def _add_code(self, *, code, title, language, section, tags, replace):
2265+
if isinstance(code, Path):
2266+
code = Path(code).read_text()
2267+
html_partial = partial(
2268+
_html_code_element,
2269+
tags=tags,
2270+
title=title,
2271+
code=code,
2272+
language=language,
2273+
)
2274+
self._add_or_replace(
2275+
title=title,
2276+
section=section,
2277+
tags=tags,
2278+
html_partial=html_partial,
2279+
replace=replace,
2280+
)
2281+
22252282
@fill_doc
22262283
def add_code(
22272284
self,

0 commit comments

Comments
 (0)