Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ‘ŒπŸŽ¨ Wrap model section in result markdown in details tag for notebooks #1098

Merged
merged 4 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
model_is_detail: 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.
model_is_detail: bool
s-weigand marked this conversation as resolved.
Show resolved Hide resolved
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 model_is_detail 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, model_is_detail=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