1 Star 1 Fork 1

CDFMLR/pyflowchart

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

PyFlowchart

PyFlowchart is a package to:

  • write flowcharts in the Python language,
  • translate Python source codes into flowcharts.

PyFlowchart produces flowcharts in flowchart.js flowchart DSL, a widely used flow chart textual representation. It's easy to convert these flowcharts text into a picture via flowchart.js.org, francoislaberge/diagrams, or some markdown editors.

Get PyFlowchart

$ pip3 install pyflowchart

Quick Start

To flowchartlize your python codes in example.py,run:

$ python3 -m pyflowchart example.py

PyFlowchart will output the generated flowchart.js DSL. Go to http://flowchart.js.org or use editors like Typora to turn the output code into a rendered diagram.

To specify a function (or a method in a class) to flowchartlize:

$ python3 -m pyflowchart example.py -f function_name
# or
$ python3 -m pyflowchart example.py -f ClassName.method_name

🎉 Now you are ready to enjoy the flowchartlization.

Keep reading this document to learn more usages.

Flowchart in Python

PyFlowchart allows you to write a flowchart in Python which could be translated into the flowchart.js DSL automatically.

PyFlowchart supports flowchart.js node types:

  • StartNode
  • OperationNode
  • ConditionNode
  • InputOutputNode
  • SubroutineNode
  • EndNode

Nodes can be connected by connect() method (connect_{yes|no} for ConditionNode). An optional second parameter to connect() is used to specify the connect_direction.

Get a Flowchart with your start node and call its flowchart() method to generate flowchart.js flowchart DSL:

from pyflowchart import *

st = StartNode('a_pyflow_test')
op = OperationNode('do something')
cond = ConditionNode('Yes or No?')
io = InputOutputNode(InputOutputNode.OUTPUT, 'something...')
sub = SubroutineNode('A Subroutine')
e = EndNode('a_pyflow_test')

st.connect(op)
op.connect(cond)
cond.connect_yes(io)
cond.connect_no(sub)
sub.connect(op, "right")  # sub->op line starts from the right of sub
io.connect(e)
 
fc = Flowchart(st)
print(fc.flowchart())

Output:

st0=>start: start a_pyflow_test
op1=>operation: do something
cond2=>condition: Yes or No?
io3=>inputoutput: output: something...
e5=>end: end a_pyflow_test
sub4=>subroutine: A Subroutine

st0->op1
op1->cond2
cond2->
cond2->
cond2(yes)->io3
io3->e5
cond2(no)->sub4
sub4(right)->op1

Then you can visit http://flowchart.js.org and translate the generated textual representation into SVG flow chart diagrams:

screenshot on flowchart.js page

P.S. Many Markdown editors (for example, Typora) support this flowchart syntax, too (reference: Typora doc about flowchart). And if you prefer CLI, see francoislaberge/diagrams.

Set Params to Nodes

Since v0.2.0, we support a Node.set_param(key, value) method to generate flowchart like this:

element(param1=value1,param2=value2)=>start: Start

(See also adrai/flowchart.js#node-specific-specifiers-by-type)

And for convenience, there are grammar sugars to set param align-next=no for ConditionNodes:

cond = ConditionNode("a cond node")
cond.no_align_next()
# or do this at __init__:
cond = ConditionNode("a cond node", align_next=False)

This usually works with a connect_direction customization:

cond.connect_yes(op, "right")

The generated flowchart will look like:

cond(align-next=no)=>condition: Yes or No?
...

cond(yes,right)->op

Python to Flowchart

PyFlowchart can also translate your Python Codes into Flowcharts.

For example, you got a simple.py:

def foo(a, b):
    if a:
        print("a")
    else:
        for i in range(3):
            print("b")
    return a + b

Run PyFlowchart in CLI to generate flowchart code:

$ python3 -m pyflowchart simple.py

# output flowchart code.

Or, in Python

>>> from pyflowchart import Flowchart
>>> with open('simple.py') as f:
...     code = f.read()
... 
>>> fc = Flowchart.from_code(code)
>>> print(fc.flowchart())

# output flowchart code.

simple.py to flowchart

Advanced Usages

As mentioned above, we use Flowchart.from_code to translate Python codes into Flowcharts. The from_code is defined as:

Flowchart.from_code(code, field="", inner=True, simplify=True, conds_align=False)

PyFlowchart CLI is a 1:1 interface for this function:

python3 -m pyflowchart [-f FIELD] [-i] [--no-simplify] [--conds-align] code_file

Let's talk about those three args:

  • field: str: Specify a field of code to generate a flowchart
  • inner: bool: True to parse the body of field; whereas False to parse the body as a single object.
  • simplify: bool: for If & Loop statements: simplify the one-line-body or not
  • conds_align: bool: improve the flowchart of consecutive If statements converted from python code. (Beta)

field

the field is the path to a field (i.e. a function) you want to draw a flowchart.

# example.py
print("start")

def foo():
    foo = "foo"

class Bar():
    def buzz(self, f):
        def g(self):
            print("g")
            f(self)
        return g(self)

Bar().buzz(foo)
print("end")

For example.py above, available paths are:

- "" (means the whole code)
- "foo"
- "Bar.buzz"
- "Bar.buzz.g"

To generate a flowchart of Bar.buzz.g

# Python
from pyflowchart import Flowchart
with open('example.py') as f:
	code = f.read()
fc = Flowchart.from_code(code, field='Bar.buzz.g', inner=False)
print(fc.flowchart())

Or:

# CLI
python3 -m pyflowchart example.py -f Bar.buzz.g

Output result:

specify a field

inner

inner controls parser's behaving. Techly, inner=True means parsing field.body, while inner=False parses [field]. So, if inner=True, pyflowchart will look into the field, otherwise, it takes the field as a node.

pyflowchart_inner

For CLI, adding an argument -i means inner=True, else inner=False.

simplify

simplify is for If & Loop statements: simplify the one-line-body.

For example:

# example_simplify.py
a = 1
if a == 1:
    print(a)
while a < 4:
    a = a + 1
  • Default: simplify=True:
flowchart = Flowchart.from_code(example_simplify_py, field="", inner=True)
print(flowchart.flowchart())
# SH $ python3 -m pyflowchart example_simplify.py 

simplify result

  • simplify=False:
flowchart = Flowchart.from_code(example_simplify_py, field="", inner=True, simplify=False)
print(flowchart.flowchart())
# SH $ python3 -m pyflowchart --no-simplify example_simplify.py 

no simplify result

conds-align (Beta)

Improve the flowchart of consecutive If statements converted from python code with the new feature of v0.2.0.

# example-conds-align.py
if cond1:
	op1
if cond2:
	op2
if cond3:
	op3
op_end

conds-align-result

Beautify Flowcharts

Sometimes, the generated flowchart is awful. In those cases, you are encouraged to modify the generated flowchart code by yourself OR consider making your python source code at bottom more clear if it's exceedingly complex.

TODOs

  • Directly generate flowchart SVG/HTML:
$ pyflowchart example.py -o flowchart.svg

Depends on node.js and flowchart.js.

  • PyFlowchart GUI

Well, I guess a GUI for PyFlowchart may be remarkable. Pasting your code into it, the flowchart DSL will be generated just in time, and the flowchart will be shown aside.

  • The Chinese README your buddies waiting for! 希望有同学帮助贡献个中文 README 呀。
  • Tests automation.

Sadly, I am too busy (pronounced as [ˈlеizi]——lazy) to code these ideas. Please submit an issue to push me on. Or, PR to make it by yourself. I cannot wait to appreciate your great contribution!

References

License

Copyright 2020-2022 CDFMLR. All rights reserved.

Licensed under the MIT License.

MIT License Copyright (c) 2020 CDFMLR Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Python codes to Flowcharts 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/cdfmlr/pyflowchart.git
git@gitee.com:cdfmlr/pyflowchart.git
cdfmlr
pyflowchart
pyflowchart
master

搜索帮助