Skip to content

Commit

Permalink
feat: make addition of ToC idempotent
Browse files Browse the repository at this point in the history
By design it was possible to add table of contents multiple times.
The rendered report then contained multiple table of contents.
Having a separate argument for table of contents instead of having
it in sections list makes multiple calls of add_table_of_contents()
idempotent — always only one ToC is rendered in the end.
  • Loading branch information
lukany committed Aug 10, 2023
1 parent 59b1c81 commit c85a86c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
17 changes: 8 additions & 9 deletions edvart/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ def __init__(
self.df = dataframe
self.sections: list[Section] = []
self.verbosity = Verbosity(verbosity)
self._table_of_contents = None

def show(self) -> None:
"""Renders the report in the calling notebook."""
if self._table_of_contents is not None:
self._table_of_contents.show(self.sections)
for section in self.sections:
if isinstance(section, TableOfContents):
section.show(self.sections)
else:
section.show(self.df)
section.show(self.df)

def _select_columns(
self,
Expand Down Expand Up @@ -173,11 +173,10 @@ def _generate_notebook(
nb["cells"].append(nbf4.new_code_cell(load_df))

# Generate code for each report section
if self._table_of_contents is not None:
self._table_of_contents.add_cells(self.sections, nb["cells"])
for section in self.sections:
if isinstance(section, TableOfContents):
section.add_cells(self.sections, nb["cells"])
else:
section.add_cells(nb["cells"])
section.add_cells(nb["cells"])

return nb

Expand Down Expand Up @@ -592,7 +591,7 @@ def add_table_of_contents(self, include_subsections: bool = True) -> "ReportBase
contents. However, they won't be included in an exported notebook created by report's
export_notebook function.
"""
self.sections.append(TableOfContents(include_subsections))
self._table_of_contents = TableOfContents(include_subsections)
return self


Expand Down
6 changes: 2 additions & 4 deletions edvart/report_sections/table_of_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ def add_cells(self, sections: List[Section], cells: List[Dict[str, Any]]) -> Non
# Add links to all main sections (not including subsections) besides the first (table of
# content) section
for section in sections:
if not isinstance(section, TableOfContents):
lines.append(TableOfContents._get_section_link(section, 1))
lines.append(TableOfContents._get_section_link(section, 1))
cells.append(nbfv4.new_markdown_cell("\n".join(lines)))

# pylint: disable=arguments-renamed
Expand All @@ -108,6 +107,5 @@ def show(self, sections: List[Section]) -> None:
# Add links to all sections including their subsections besides the first (table of content)
# section
for section in sections:
if not isinstance(section, TableOfContents):
self._add_section_lines(section, 1, lines, self._include_subsections)
self._add_section_lines(section, 1, lines, self._include_subsections)
display(Markdown("\n".join(lines)))

0 comments on commit c85a86c

Please sign in to comment.