Skip to content

Commit 59c217c

Browse files
Update Mermaid package to include Position enum and set_position method
1 parent 41755ec commit 59c217c

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

mermaid/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"""
1313
from enum import Enum
1414

15-
from ._main import Mermaid
15+
from ._main import Mermaid, Position
1616
from ._utils import load, text_to_snake_case
1717
from .configuration import Config
1818
from .graph import Graph
@@ -30,4 +30,7 @@ class Direction(Enum):
3030
BOTTOM_TO_TOP = 'BT'
3131

3232

33-
__all__ = ['Mermaid', 'load', 'Direction', 'Graph', 'Style', 'Config', 'Icon']
33+
__all__ = [
34+
'Mermaid', 'load', 'Direction', 'Graph', 'Style', 'Config', 'Icon',
35+
'Position'
36+
]

mermaid/_main.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
from enum import Enum
23
from pathlib import Path
34
from typing import Union
45

@@ -8,6 +9,16 @@
89
from .graph import Graph
910

1011

12+
class Position(Enum):
13+
"""
14+
This class represents the position of the node in a Mermaid diagram.
15+
"""
16+
LEFT = 'left'
17+
RIGHT = 'right'
18+
CENTER = 'center'
19+
NONE = 'none'
20+
21+
1122
class Mermaid:
1223
"""
1324
This class represents a Mermaid diagram.
@@ -17,16 +28,30 @@ class Mermaid:
1728
svg_response (Response): The response from the GET request to the Mermaid SVG API.
1829
img_response (Response): The response from the GET request to the Mermaid IMG API.
1930
"""
20-
def __init__(self, graph: Graph):
31+
def __init__(self,
32+
graph: Graph,
33+
position: Union[Position, str] = Position.NONE):
2134
"""
2235
The constructor for the Mermaid class.
2336
2437
Parameters:
2538
graph (Graph): The Graph object containing the Mermaid diagram script.
2639
"""
40+
self.__position: str = position if isinstance(position,
41+
str) else position.value
2742
self._diagram = self._process_diagram(graph.script)
2843
self._make_request_to_mermaid()
2944

45+
def set_position(self, position: Union[Position, str]) -> None:
46+
"""
47+
Set the position of the node in the Mermaid diagram.
48+
49+
Parameters:
50+
position (Union[Position, str]): The position of the node.
51+
"""
52+
self.__position = position if isinstance(position,
53+
str) else position.value
54+
3055
@staticmethod
3156
def _process_diagram(diagram: str) -> str:
3257
"""
@@ -50,7 +75,9 @@ def _repr_html_(self) -> str:
5075
Returns:
5176
str: The text of the SVG response.
5277
"""
53-
return self.svg_response.text
78+
if self.__position == Position.NONE.value:
79+
return self.svg_response.text
80+
return f'<div style="text-align:{self.__position}">{self.svg_response.text}</div>'
5481

5582
def _make_request_to_mermaid(self) -> None:
5683
"""

mermaid/tests/test_mermaid.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import unittest
33
from pathlib import Path
44

5-
from mermaid import Mermaid
5+
from mermaid import Mermaid, Position
66
from mermaid.graph import Graph
77

88

@@ -47,6 +47,24 @@ def test_to_png_on_mermaid_with_path(self):
4747

4848
self.assertTrue(Path.exists(output_path))
4949

50+
def test_repr_html_on_mermaid_with_default_position(self):
51+
self.assertEqual(self.mermaid_object._repr_html_(),
52+
self.mermaid_object.svg_response.text)
53+
54+
def test_repr_html_on_mermaid_with_str_position(self):
55+
position = 'center'
56+
self.mermaid_object.set_position(position)
57+
self.assertTrue(self.mermaid_object._repr_html_().startswith(
58+
f'<div style="text-align:{position}">'))
59+
self.assertTrue(self.mermaid_object._repr_html_().endswith('</div>'))
60+
61+
def test_repr_html_on_mermaid_with_enum_position(self):
62+
position = Position.RIGHT
63+
self.mermaid_object.set_position(position)
64+
self.assertTrue(self.mermaid_object._repr_html_().startswith(
65+
f'<div style="text-align:{position.value}">'))
66+
self.assertTrue(self.mermaid_object._repr_html_().endswith('</div>'))
67+
5068
def tearDown(self) -> None:
5169
output_svg: str = f'./{self.name}.svg'
5270
output_png: str = f'./{self.name}.png'

0 commit comments

Comments
 (0)