-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from ouhammmourachid/clean-up-mermaid
add to_svg and to_png methodes to Mermaid class
- Loading branch information
Showing
4 changed files
with
89 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
""" a beter docs sttrings | ||
""" | ||
from ._main import Mermaid | ||
from ._utils import load | ||
from .mermaid import * | ||
|
||
__version__: str = '0.1.6' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import base64 | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
import requests | ||
from requests import Response | ||
|
||
from .graph import Graph | ||
|
||
|
||
class Mermaid: | ||
def __init__(self, graph: Graph): | ||
self._diagram = self._process_diagram(graph.script) | ||
self._make_request_to_mermaid() | ||
|
||
@staticmethod | ||
def _process_diagram(diagram: str) -> str: | ||
graphbytes = diagram.encode('utf8') | ||
base64_bytes = base64.b64encode(graphbytes) | ||
diagram = base64_bytes.decode('ascii') | ||
return diagram | ||
|
||
def _repr_html_(self) -> str: | ||
return self.svg_response.text | ||
|
||
def _make_request_to_mermaid(self) -> None: | ||
self.svg_response: Response = requests.get('https://mermaid.ink/svg/' + | ||
self._diagram) | ||
self.img_response: Response = requests.get('https://mermaid.ink/img/' + | ||
self._diagram) | ||
|
||
def to_svg(self, path: Union[str, Path]) -> None: | ||
with open(path, 'w', encoding='utf-8') as file: | ||
file.write(self.svg_response.text) | ||
|
||
def to_png(self, path: Union[str, Path]) -> None: | ||
with open(path, 'w', encoding='utf-8') as file: | ||
file.write(self.img_response.text) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters