Skip to content

Commit

Permalink
feat: add local mermaid.ink server support and update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ouhammmourachid committed Nov 25, 2024
1 parent dd3880d commit 17c8ac4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .github/actions/test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ inputs:
runs:
using: 'composite'
steps:
- name: setup mermaid.ink locally
run : |
docker run -p 3000:3000 -d --cap-add=SYS_ADMIN ghcr.io/jihchi/mermaid.ink
- name: install dependencies with Python ${{ inputs.python-version }}
uses: ouhammmourachid/poetry-install@v2
with:
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ lint:
coverage:
$(POETRY_RUN) pytest --cov ./mermaid --cov-report=xml

mermaid.ink/up:
docker run -p 3000:3000 -d --cap-add=SYS_ADMIN ghcr.io/jihchi/mermaid.ink

mermaid.ink/down:
docker stop $(shell docker ps -q --filter ancestor=ghcr.io/jihchi/mermaid.ink)

.PHONY: bumpversion
bumpversion:
$(eval name=$(filter-out $@,$(MAKECMDGOALS)))
Expand All @@ -60,3 +66,5 @@ help:
@echo " bumpversion bump version"
@echo " help show this help message"
@echo ""
@echo " mermaid.ink/up start mermaid.ink Local server"
@echo " mermaid.ink/down stop mermaid.ink local server"
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ diagram
```
- more examples on [mermaid](https://www.kaggle.com/code/ouhammourachid/mermaid-py) and [test-mermaid](https://www.kaggle.com/code/ouhammourachid/testing-mermaid-py)

## Server

By default, the library uses the mermaid.ink service [github](https://github.com/jihchi/mermaid.ink) for generating diagrams. The service can be run locally using docker, following instructions [here](https://github.com/jihchi/mermaid.ink?tab=readme-ov-file#launch-a-container).

The `MERMAID_INK_SERVER` environment variable can be used to specify the server to use, for example.

## List of Diagrames
- [x] [~~FlowChart~~](https://mermaid.js.org/syntax/flowchart.html)
Expand All @@ -99,6 +103,7 @@ diagram
- [ ] [XYChart 🔥](https://mermaid.js.org/syntax/xyChart.html)
- [ ] [Block Diagram 🔥](https://mermaid.js.org/syntax/block.html)
- [ ] [Packet 🔥](https://mermaid.js.org/syntax/packet.html)
- [ ] [Kanban 🔥](https://mermaid.js.org/syntax/kanban.html)
- [ ] [Architecture 🔥](https://mermaid.js.org/syntax/architecture.html)

## License
Expand Down
8 changes: 6 additions & 2 deletions mermaid/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import os
from enum import Enum
from pathlib import Path
from typing import Optional, Union
Expand Down Expand Up @@ -124,12 +125,15 @@ def _make_request_to_mermaid(self) -> None:
Make GET requests to the Mermaid SVG and IMG APIs using
the base64 encoded string of the Mermaid diagram script.
"""
mermaid_server_adress: str = os.getenv(
"MERMAID_INK_SERVER", "https://mermaid.ink"
)

self.svg_response: Response = requests.get(
"https://mermaid.ink/svg/" + self._diagram
mermaid_server_adress + "/svg/" + self._diagram
)
self.img_response: Response = requests.get(
"https://mermaid.ink/img/" + self._diagram
mermaid_server_adress + "/img/" + self._diagram
)

def to_svg(self, path: Union[str, Path]) -> None:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,27 @@ def tearDown(self) -> None:
os.remove(output_png_path)

return super().tearDown()


class TestMermaidWithLocalServer(unittest.TestCase):
def setUp(self) -> None:
script: str = """graph TD;
A-->B;
A-->C;
B-->D;
C-->D;"""
os.environ["MERMAID_INK_SERVER"] = "http://localhost:3000"
self.name: str = "simple-graph"
self.graph: Graph = Graph(self.name, script)

def test_make_request_to_mermaid_api_for_svg(self):
mermaid_object = Mermaid(self.graph)
self.assertTrue(mermaid_object.svg_response.status_code == 200)

def test_make_request_to_mermaid_api_for_png(self):
mermaid_object = Mermaid(self.graph)
self.assertTrue(mermaid_object.img_response.status_code == 200)

def tearDown(self) -> None:
del os.environ["MERMAID_INK_SERVER"]
return super().tearDown()

0 comments on commit 17c8ac4

Please sign in to comment.