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

Fix doctest and mermaid title #315

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions bigtree/tree/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -1543,9 +1543,19 @@ def tree_to_mermaid(

**Customize node shape, edge label, edge arrow, and custom node attributes**

>>> graph = tree_to_mermaid(root, node_shape_attr="node_shape", edge_label="edge_label", edge_arrow_attr="edge_arrow", node_attr="node_style")
>>> graph = tree_to_mermaid(
... root,
... title="Mermaid Diagram",
... node_shape_attr="node_shape",
... edge_label="edge_label",
... edge_arrow_attr="edge_arrow",
... node_attr="node_style",
... )
>>> print(graph)
```mermaid
---
title: Mermaid Diagram
---
%%{ init: { 'flowchart': { 'curve': 'basis' } } }%%
flowchart TB
0{"a"} ==>|Child 1| 0-0("b")
Expand Down Expand Up @@ -1599,7 +1609,7 @@ def tree_to_mermaid(
style_template = "classDef {style_name} {style}"

# Content
title = f"---\ntitle: {title}\n---" if title else ""
title = f"---\ntitle: {title}\n---\n" if title else ""
line_style = f"%%{{ init: {{ 'flowchart': {{ 'curve': '{line_shape}' }} }} }}%%"
styles = []
flows = []
Expand Down
8 changes: 4 additions & 4 deletions bigtree/tree/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ def get_tree_diff(
>>> tree_diff = get_tree_diff(root, root_other)
>>> tree_diff.show()
Downloads
├── photo2.jpg (-)
└── Pictures
└── photo2.jpg (+)
├── Pictures
└── photo2.jpg (+)
└── photo2.jpg (-)

>>> tree_diff = get_tree_diff(root, root_other, only_diff=False)
>>> tree_diff.show()
Expand All @@ -302,7 +302,7 @@ def get_tree_diff(
├── file1.doc
└── photo2.jpg (-)

>>> tree_diff = get_tree_diff(root, root_other, detail=True)
>>> tree_diff = get_tree_diff(root, root_other, only_diff=False, detail=True)
>>> tree_diff.show()
Downloads
├── Pictures
Expand Down
22 changes: 22 additions & 0 deletions tests/tree/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -2099,6 +2099,28 @@ def test_tree_to_mermaid(tree_node):
)
assert mermaid_md == expected_str

@staticmethod
def test_tree_to_mermaid_title(tree_node):
mermaid_md = export.tree_to_mermaid(tree_node, title="Mermaid Diagram")
expected_str = (
"""```mermaid\n"""
"""---\n"""
"""title: Mermaid Diagram\n"""
"""---\n"""
"""%%{ init: { \'flowchart\': { \'curve\': \'basis\' } } }%%\n"""
"""flowchart TB\n"""
"""0("a") --> 0-0("b")\n"""
"""0-0 --> 0-0-0("d")\n"""
"""0-0 --> 0-0-1("e")\n"""
"""0-0-1 --> 0-0-1-0("g")\n"""
"""0-0-1 --> 0-0-1-1("h")\n"""
"""0("a") --> 0-1("c")\n"""
"""0-1 --> 0-1-0("f")\n"""
"""classDef default stroke-width:1\n"""
"""```"""
)
assert mermaid_md == expected_str

@staticmethod
def test_tree_to_mermaid_invalid_rankdir_error(tree_node):
with pytest.raises(ValueError) as exc_info:
Expand Down
Loading