Skip to content

Commit 4aa928b

Browse files
authored
Merge pull request #316 from kayjan/bugfix/docstring
Add docstring comment to prefer node_name over name
2 parents 63961b2 + ebfda72 commit 4aa928b

File tree

5 files changed

+426
-22
lines changed

5 files changed

+426
-22
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added:
9+
- Tree Export: Mermaid diagram to include theme.
10+
### Fixed:
11+
- Misc: Doctest for docstrings, docstring to indicate usage prefers `node_name` to `name`.
12+
- Tree Export: Mermaid diagram title to add newline.
813

914
## [0.22.1] - 2024-11-03
1015
### Added:

bigtree/node/node.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ class Node(basenode.BaseNode):
6161
6262
Get `Node` configuration
6363
64-
1. ``node_name``: Get node name, without accessing `name` directly
64+
1. ``node_name``: Get node name, without accessing `name` directly.
65+
This is the preferred way to access node name as `node_name` is
66+
immutable, whereas `name` is mutable.
6567
2. ``path_name``: Get path name from root, separated by `sep`
6668
6769
**Node Methods**

bigtree/tree/export.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,7 @@ def get_list_of_text_dimensions(
14301430
def tree_to_mermaid(
14311431
tree: T,
14321432
title: str = "",
1433+
theme: Optional[str] = None,
14331434
rankdir: str = "TB",
14341435
line_shape: str = "basis",
14351436
node_colour: str = "",
@@ -1447,6 +1448,7 @@ def tree_to_mermaid(
14471448
14481449
Parameters for customizations that apply to entire flowchart include:
14491450
- Title, `title`
1451+
- Theme, `theme`
14501452
- Layout direction, `rankdir`
14511453
- Line shape or curvature, `line_shape`
14521454
- Fill colour of nodes, `node_colour`
@@ -1465,6 +1467,13 @@ def tree_to_mermaid(
14651467
14661468
**Accepted Parameter Values**
14671469
1470+
Possible theme:
1471+
- default
1472+
- neutral: great for black and white documents
1473+
- dark: great for dark-mode
1474+
- forest: shades of geen
1475+
- base: theme that can be modified, use it for customizations
1476+
14681477
Possible rankdir:
14691478
- `TB`: top-to-bottom
14701479
- `BT`: bottom-to-top
@@ -1546,6 +1555,7 @@ def tree_to_mermaid(
15461555
>>> graph = tree_to_mermaid(
15471556
... root,
15481557
... title="Mermaid Diagram",
1558+
... theme="forest",
15491559
... node_shape_attr="node_shape",
15501560
... edge_label="edge_label",
15511561
... edge_arrow_attr="edge_arrow",
@@ -1556,7 +1566,7 @@ def tree_to_mermaid(
15561566
---
15571567
title: Mermaid Diagram
15581568
---
1559-
%%{ init: { 'flowchart': { 'curve': 'basis' } } }%%
1569+
%%{ init: { 'flowchart': { 'curve': 'basis' }, 'theme': 'forest' } }%%
15601570
flowchart TB
15611571
0{"a"} ==>|Child 1| 0-0("b")
15621572
0-0 --> 0-0-0("d"):::class0-0-0
@@ -1593,12 +1603,15 @@ def tree_to_mermaid(
15931603
"""
15941604
from bigtree.tree.helper import clone_tree
15951605

1606+
themes = constants.MermaidConstants.THEMES
15961607
rankdirs = constants.MermaidConstants.RANK_DIR
15971608
line_shapes = constants.MermaidConstants.LINE_SHAPES
15981609
node_shapes = constants.MermaidConstants.NODE_SHAPES
15991610
edge_arrows = constants.MermaidConstants.EDGE_ARROWS
16001611

16011612
# Assertions
1613+
if theme:
1614+
assertions.assert_str_in_list("theme", theme, themes)
16021615
assertions.assert_str_in_list("rankdir", rankdir, rankdirs)
16031616
assertions.assert_key_in_dict("node_shape", node_shape, node_shapes)
16041617
assertions.assert_str_in_list("line_shape", line_shape, line_shapes)
@@ -1609,8 +1622,9 @@ def tree_to_mermaid(
16091622
style_template = "classDef {style_name} {style}"
16101623

16111624
# Content
1625+
theme_mermaid = f", 'theme': '{theme}'" if theme else ""
16121626
title = f"---\ntitle: {title}\n---\n" if title else ""
1613-
line_style = f"%%{{ init: {{ 'flowchart': {{ 'curve': '{line_shape}' }} }} }}%%"
1627+
line_style = f"%%{{ init: {{ 'flowchart': {{ 'curve': '{line_shape}' }}{theme_mermaid} }} }}%%"
16141628
styles = []
16151629
flows = []
16161630

bigtree/utils/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ def __post_init__(self) -> None:
171171

172172

173173
class MermaidConstants:
174+
THEMES: List[str] = ["default", "neutral", "dark", "forest", "base"]
174175
RANK_DIR: List[str] = ["TB", "BT", "LR", "RL"]
175176
LINE_SHAPES: List[str] = [
176177
"basis",

0 commit comments

Comments
 (0)