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

Add docstring comment to prefer node_name over name #316

Merged
merged 5 commits 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added:
- Tree Export: Mermaid diagram to include theme.
### Fixed:
- Misc: Doctest for docstrings, docstring to indicate usage prefers `node_name` to `name`.
- Tree Export: Mermaid diagram title to add newline.

## [0.22.1] - 2024-11-03
### Added:
Expand Down
4 changes: 3 additions & 1 deletion bigtree/node/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ class Node(basenode.BaseNode):

Get `Node` configuration

1. ``node_name``: Get node name, without accessing `name` directly
1. ``node_name``: Get node name, without accessing `name` directly.
This is the preferred way to access node name as `node_name` is
immutable, whereas `name` is mutable.
2. ``path_name``: Get path name from root, separated by `sep`

**Node Methods**
Expand Down
18 changes: 16 additions & 2 deletions bigtree/tree/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,7 @@ def get_list_of_text_dimensions(
def tree_to_mermaid(
tree: T,
title: str = "",
theme: Optional[str] = None,
rankdir: str = "TB",
line_shape: str = "basis",
node_colour: str = "",
Expand All @@ -1447,6 +1448,7 @@ def tree_to_mermaid(

Parameters for customizations that apply to entire flowchart include:
- Title, `title`
- Theme, `theme`
- Layout direction, `rankdir`
- Line shape or curvature, `line_shape`
- Fill colour of nodes, `node_colour`
Expand All @@ -1465,6 +1467,13 @@ def tree_to_mermaid(

**Accepted Parameter Values**

Possible theme:
- default
- neutral: great for black and white documents
- dark: great for dark-mode
- forest: shades of geen
- base: theme that can be modified, use it for customizations

Possible rankdir:
- `TB`: top-to-bottom
- `BT`: bottom-to-top
Expand Down Expand Up @@ -1546,6 +1555,7 @@ def tree_to_mermaid(
>>> graph = tree_to_mermaid(
... root,
... title="Mermaid Diagram",
... theme="forest",
... node_shape_attr="node_shape",
... edge_label="edge_label",
... edge_arrow_attr="edge_arrow",
Expand All @@ -1556,7 +1566,7 @@ def tree_to_mermaid(
---
title: Mermaid Diagram
---
%%{ init: { 'flowchart': { 'curve': 'basis' } } }%%
%%{ init: { 'flowchart': { 'curve': 'basis' }, 'theme': 'forest' } }%%
flowchart TB
0{"a"} ==>|Child 1| 0-0("b")
0-0 --> 0-0-0("d"):::class0-0-0
Expand Down Expand Up @@ -1593,12 +1603,15 @@ def tree_to_mermaid(
"""
from bigtree.tree.helper import clone_tree

themes = constants.MermaidConstants.THEMES
rankdirs = constants.MermaidConstants.RANK_DIR
line_shapes = constants.MermaidConstants.LINE_SHAPES
node_shapes = constants.MermaidConstants.NODE_SHAPES
edge_arrows = constants.MermaidConstants.EDGE_ARROWS

# Assertions
if theme:
assertions.assert_str_in_list("theme", theme, themes)
assertions.assert_str_in_list("rankdir", rankdir, rankdirs)
assertions.assert_key_in_dict("node_shape", node_shape, node_shapes)
assertions.assert_str_in_list("line_shape", line_shape, line_shapes)
Expand All @@ -1609,8 +1622,9 @@ def tree_to_mermaid(
style_template = "classDef {style_name} {style}"

# Content
theme_mermaid = f", 'theme': '{theme}'" if theme else ""
title = f"---\ntitle: {title}\n---\n" if title else ""
line_style = f"%%{{ init: {{ 'flowchart': {{ 'curve': '{line_shape}' }} }} }}%%"
line_style = f"%%{{ init: {{ 'flowchart': {{ 'curve': '{line_shape}' }}{theme_mermaid} }} }}%%"
styles = []
flows = []

Expand Down
1 change: 1 addition & 0 deletions bigtree/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def __post_init__(self) -> None:


class MermaidConstants:
THEMES: List[str] = ["default", "neutral", "dark", "forest", "base"]
RANK_DIR: List[str] = ["TB", "BT", "LR", "RL"]
LINE_SHAPES: List[str] = [
"basis",
Expand Down
Loading
Loading