Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
This repository was archived by the owner on Feb 22, 2020. It is now read-only.

Commit efd5866

Browse files
authored
Merge pull request #320 from gnes-ai/fix-flow-3
feat(flow): add dump to jpg
2 parents 8ca5ef0 + 2fb0f4f commit efd5866

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

gnes/flow/__init__.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ def from_yaml(orchestration: str) -> 'Flow':
139139

140140
@_build_level(BuildLevel.GRAPH)
141141
def to_mermaid(self, left_right: bool = True):
142+
"""
143+
Output the mermaid graph for visualization
144+
145+
:param left_right: render the flow in left-to-right manner, otherwise top-down manner.
146+
:return:
147+
"""
142148
mermaid_graph = OrderedDict()
143149
for k in self._service_nodes.keys():
144150
mermaid_graph[k] = []
@@ -166,10 +172,28 @@ def to_mermaid(self, left_right: bool = True):
166172
['graph %s' % ('LR' if left_right else 'TD')] + [ss for s in mermaid_graph.values() for ss in
167173
s] + style + class_def)
168174

169-
self.logger.info(
170-
'copy-paste the output and visualize it with: https://mermaidjs.github.io/mermaid-live-editor/')
171175
return mermaid_str
172176

177+
@_build_level(BuildLevel.GRAPH)
178+
def to_jpg(self, path: str = 'flow.jpg', left_right: bool = True):
179+
"""
180+
Rendering the current flow as a jpg image, this will call :py:meth:`to_mermaid` and it needs internet connection
181+
182+
:param path: the file path of the image
183+
:param left_right: render the flow in left-to-right manner, otherwise top-down manner.
184+
:return:
185+
"""
186+
import base64
187+
from urllib.request import Request, urlopen
188+
mermaid_str = self.to_mermaid(left_right)
189+
encoded_str = base64.b64encode(bytes(mermaid_str, 'utf-8')).decode('utf-8')
190+
print('https://mermaidjs.github.io/mermaid-live-editor/#/view/%s' % encoded_str)
191+
self.logger.info('saving jpg...')
192+
req = Request('https://mermaid.ink/img/%s' % encoded_str, headers={'User-Agent': 'Mozilla/5.0'})
193+
with open(path, 'wb') as fp:
194+
fp.write(urlopen(req).read())
195+
self.logger.info('done')
196+
173197
def train(self, bytes_gen: Iterator[bytes] = None, **kwargs):
174198
"""Do training on the current flow
175199

tests/test_gnes_flow.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def test_flow5(self):
113113
.build(backend=None))
114114
print(f._service_edges)
115115
print(f.to_mermaid())
116+
f.to_jpg()
116117

117118
def _test_index_flow(self, backend):
118119
for k in [self.indexer1_bin, self.indexer2_bin, self.encoder_bin]:

0 commit comments

Comments
 (0)