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

simplify using Mermaid class #249

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ representative at an online or offline event.

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
rachidouhammou21@gmail.com.
ouhammmourachid@gmail.com.
All complaints will be reviewed and investigated promptly and fairly.

All community leaders are obligated to respect the privacy and security of the
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pip install mermaid-py
import mermaid as md
from mermaid.graph import Graph

sequence = Graph('Sequence-diagram',"""
render = md.Mermaid("""
stateDiagram-v2
[*] --> Still
Still --> [*]
Expand All @@ -42,7 +42,6 @@ stateDiagram-v2
Moving --> Crash
Crash --> [*]
""")
render = md.Mermaid(sequence)
render # !! note this only works in the notebook that rendered the html.
```

Expand Down
8 changes: 5 additions & 3 deletions mermaid/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Mermaid:

def __init__(
self,
graph: Graph,
graph: Union[Graph, str],
width: Optional[int] = None,
height: Optional[int] = None,
scale: Optional[float] = None,
Expand All @@ -44,7 +44,7 @@ def __init__(
The constructor for the Mermaid class.

Parameters:
graph (Graph): The Graph object containing the Mermaid diagram script.
graph (Graph): The Mermaid diagram. It can be an instance of the Graph class or a string representing the script.
width (Optional[int]): The width of the SVG image.
height (Optional[int]): The height of the SVG image.
scale (Optional[float]): The scale of the SVG image.
Expand All @@ -62,7 +62,9 @@ def __init__(
self.__width = width if width else None
self.__scale = scale if scale else None

self._diagram = self._process_diagram(graph.script)
self._diagram = self._process_diagram(
graph if isinstance(graph, str) else graph.script
)

if any([self.__width, self.__height, self.__scale]):
self._diagram += "?" + self._build_query_params()
Expand Down
9 changes: 7 additions & 2 deletions tests/test_mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

class TestMermaid(unittest.TestCase):
def setUp(self) -> None:
script: str = """graph TD;
self.script: str = """graph TD;
A-->B;
A-->C;
B-->D;
C-->D;"""
self.name: str = "simple-graph"
self.graph: Graph = Graph(self.name, script)
self.graph: Graph = Graph(self.name, self.script)
self.mermaid_object = Mermaid(self.graph)

def test_make_request_to_mermaid_api_for_svg(self):
Expand All @@ -23,6 +23,11 @@ def test_make_request_to_mermaid_api_for_svg(self):
def test_make_request_to_mermaid_api_for_png(self):
self.assertTrue(self.mermaid_object.img_response.status_code == 200)

def test_using_mermaid_with_str_script(self):
mermaid_with_str_script = Mermaid(self.script)
self.assertTrue(mermaid_with_str_script.svg_response.status_code == 200)
self.assertTrue(mermaid_with_str_script.img_response.status_code == 200)

def test_query_params_to_mermaid_api_for_svg(self):
mermaid_with_params = Mermaid(self.graph, height=1024, scale=2)
self.assertTrue(mermaid_with_params.img_response.status_code == 200)
Expand Down
Loading