@@ -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
0 commit comments