Skip to content

Commit

Permalink
👌🎨 Wrap model section in result markdown in details tag for notebooks (
Browse files Browse the repository at this point in the history
…#1098)

* 👌🎨 Wrap model section in result markdown in details tag for notebooks

* 🧪 Added test for details tag in ipython render but not in call to markdown

* 🚧📚 Added change to changelog

* ♻️ Renamed 'model_is_detail' to 'wrap_model_in_details'
  • Loading branch information
s-weigand committed Jun 17, 2022
1 parent ff66d8a commit 35467ed
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### 👌 Minor Improvements:

- 👌🎨 Wrap model section in result markdown in details tag for notebooks (#1098)

### 🩹 Bug fixes

### 📚 Documentation
Expand Down
17 changes: 14 additions & 3 deletions glotaran/project/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,13 @@ def get_scheme(self) -> Scheme:

return replace(self.scheme, parameters=self.optimized_parameters)

def markdown(self, with_model: bool = True, base_heading_level: int = 1) -> MarkdownStr:
def markdown(
self,
with_model: bool = True,
*,
base_heading_level: int = 1,
wrap_model_in_details: bool = False,
) -> MarkdownStr:
"""Format the model as a markdown text.
Parameters
Expand All @@ -178,6 +184,8 @@ def markdown(self, with_model: bool = True, base_heading_level: int = 1) -> Mark
If `True`, the model will be printed with initial and optimized parameters filled in.
base_heading_level : int
The level of the base heading.
wrap_model_in_details: bool
Wraps model into details tag. Defaults to ``False``
Returns
-------
Expand Down Expand Up @@ -230,7 +238,10 @@ def markdown(self, with_model: bool = True, base_heading_level: int = 1) -> Mark
initial_parameters=self.initial_parameters,
base_heading_level=base_heading_level,
)
result_table = f"{result_table}\n\n{model_md}"
if wrap_model_in_details is False:
result_table = f"{result_table}\n\n{model_md}"
else:
result_table = f"{result_table}\n\n<br><details>\n\n{model_md}\n</details>"

return MarkdownStr(result_table)

Expand All @@ -244,7 +255,7 @@ def _repr_markdown_(self) -> str:
str
The scheme as markdown string.
"""
return str(self.markdown(base_heading_level=3))
return str(self.markdown(base_heading_level=3, wrap_model_in_details=True))

def __str__(self) -> str:
"""Overwrite of ``__str__``."""
Expand Down
4 changes: 4 additions & 0 deletions glotaran/project/test/test_result.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import re
from pathlib import Path

import numpy as np
Expand Down Expand Up @@ -29,16 +30,19 @@ def dummy_result():

def test_result_ipython_rendering(dummy_result: Result):
"""Autorendering in ipython"""
details_pattern = re.compile(r".+<br><details>\n\n### Model.+<\/details>", re.DOTALL)

rendered_obj = format_display_data(dummy_result)[0]

assert "text/markdown" in rendered_obj
assert rendered_obj["text/markdown"].startswith("| Optimization Result")
assert details_pattern.match(rendered_obj["text/markdown"]) is not None

rendered_markdown_return = format_display_data(dummy_result.markdown())[0]

assert "text/markdown" in rendered_markdown_return
assert rendered_markdown_return["text/markdown"].startswith("| Optimization Result")
assert details_pattern.match(rendered_markdown_return["text/markdown"]) is None


def test_result_markdown_nested_parameters():
Expand Down

0 comments on commit 35467ed

Please sign in to comment.