Hướng dẫn flow chart generator python - Python trình tạo biểu đồ luồng

PyflowChart là một gói để:

  • Viết sơ đồ bằng ngôn ngữ Python,
  • Dịch mã nguồn Python thành sơ đồ.

PyflowChart tạo ra sơ đồ trong sơ đồ DSL lưu đồ, một biểu diễn văn bản biểu đồ dòng chảy được sử dụng rộng rãi. Thật dễ dàng để chuyển đổi văn bản sơ đồ này thành một bức tranh thông qua sơ đồ.js.org, Francoislaberge/Sơ đồ hoặc một số biên tập viên đánh dấu.

Nhận pyflowchart

$ pip3 install pyflowchart

Bắt đầu nhanh

Để lưu đồ mã python của bạn trong ____ 23 , chạy:

$ python3 -m pyflowchart example.py

PyflowChart sẽ xuất ra DSL lưu trữ được tạo. Truy cập http://flowchart.js.org hoặc sử dụng các biên tập viên như typora để biến mã đầu ra thành sơ đồ được hiển thị.

Để chỉ định một hàm (hoặc một phương thức trong một lớp) để lưu đồ:

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

Bây giờ bạn đã sẵn sàng để tận hưởng sơ đồ.

Hãy đọc tài liệu này để tìm hiểu thêm cách sử dụng.

Sơ đồ trong Python

PyflowChart cho phép bạn viết một sơ đồ trong Python có thể được dịch thành DSL lưu trữ.js DSL.

PyflowChart hỗ trợ sơ đồ các loại nút:

  • StartNode
  • Hoạt động
  • Điều kiện
  • InputOutputNode
  • Subroutinenode
  • Mã cuối

Các nút có thể được kết nối bằng phương pháp

$ python3 -m pyflowchart example.py -f function_name
# or
$ python3 -m pyflowchart example.py -f ClassName.method_name
4 (
$ python3 -m pyflowchart example.py -f function_name
# or
$ python3 -m pyflowchart example.py -f ClassName.method_name
5 cho điều kiệnNode). Một tham số thứ hai tùy chọn đến
$ python3 -m pyflowchart example.py -f function_name
# or
$ python3 -m pyflowchart example.py -f ClassName.method_name
4 được sử dụng để chỉ định kết nối.

Nhận sơ đồ với nút bắt đầu của bạn và gọi phương thức

$ python3 -m pyflowchart example.py -f function_name
# or
$ python3 -m pyflowchart example.py -f ClassName.method_name
7 của nó để tạo sơ đồ sơ đồ.

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

Sau đó, bạn có thể truy cập http://flowchart.js.org và dịch biểu diễn văn bản được tạo thành sơ đồ biểu đồ dòng chảy SVG:

Hướng dẫn flow chart generator python - Python trình tạo biểu đồ luồng

P.S. Nhiều trình chỉnh sửa Markdown (ví dụ, Typora) cũng hỗ trợ cú pháp sơ đồ này (tham khảo: Typora Doc về sơ đồ). Và nếu bạn thích CLI, hãy xem Francoislaberge/Sơ đồ.

Đặt các tham số thành các nút

Vì v0.2.0, chúng tôi hỗ trợ phương thức

$ python3 -m pyflowchart example.py -f function_name
# or
$ python3 -m pyflowchart example.py -f ClassName.method_name
8 để tạo sơ đồ như thế này:

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

.

Và để thuận tiện, có những đường ngữ pháp để đặt param

$ python3 -m pyflowchart example.py -f function_name
# or
$ python3 -m pyflowchart example.py -f ClassName.method_name
9 cho điều kiện:

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

Điều này thường hoạt động với một tùy chỉnh kết nối:

cond.connect_yes(op, "right")

Sơ đồ được tạo sẽ trông giống như:

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

cond(yes,right)->op

Python đến sơ đồ

Pyflowchart cũng có thể dịch mã Python của bạn thành sơ đồ.

Ví dụ: bạn có một

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())
0:

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

Chạy pyflowchart trong CLI để tạo mã sơ đồ:

$ python3 -m pyflowchart example.py
0

Hoặc, trong Python

$ python3 -m pyflowchart example.py
1

Sử dụng nâng cao

Như đã đề cập ở trên, chúng tôi sử dụng

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())
1 để dịch mã python thành sơ đồ.
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())
2 được định nghĩa là:

$ python3 -m pyflowchart example.py
2

PyflowChart CLI là giao diện 1: 1 cho chức năng này:

$ python3 -m pyflowchart example.py
3

Hãy nói về ba cuộc tranh luận đó:

  • 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())
    
    3: STR: Chỉ định trường mã để tạo sơ đồ
  • 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())
    
    4: bool:
    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())
    
    5 để phân tích cơ thể của trường; trong khi
    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())
    
    6 để phân tích cơ thể như một đối tượng.
  • 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())
    
    7: Bool: for If & Loop From
  • 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())
    
    8: Bool: Cải thiện sơ đồ liên tiếp nếu các câu lệnh được chuyển đổi từ mã Python. (Beta)

đồng ruộng

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())
3 là đường dẫn đến một trường (nghĩa là một hàm) bạn muốn vẽ một sơ đồ.

$ python3 -m pyflowchart example.py
4

Đối với

$ python3 -m pyflowchart example.py -f function_name
# or
$ python3 -m pyflowchart example.py -f ClassName.method_name
3 ở trên, các đường dẫn có sẵn là:

$ python3 -m pyflowchart example.py
5

Để tạo ra một sơ đồ

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
1

$ python3 -m pyflowchart example.py
6

Or:

$ python3 -m pyflowchart example.py
7

Kết quả đầu ra:

bên trong

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())
4 kiểm soát hành vi của trình phân tích cú pháp. Công nghệ,
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
3 có nghĩa là phân tích cú pháp
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
4, trong khi
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
5 phân tích cú pháp
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
6. Vì vậy, nếu
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
3, PyFlowChart sẽ nhìn vào trường, nếu không, nó sẽ lấy
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())
3 làm nút.

Đối với CLI, việc thêm một đối số

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
9 có nghĩa là
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
3, khác
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
5.

đơn giản hóa

Đơn giản hóa là cho các câu lệnh IF & LOOP: Đơn giản hóa cơ thể một dòng.

Ví dụ:

$ python3 -m pyflowchart example.py
8
  • Mặc định:
    element(param1=value1,param2=value2)=>start: Start
    
    2:
$ python3 -m pyflowchart example.py
9

  • element(param1=value1,param2=value2)=>start: Start
    
    3:
$ python3 -m pyflowchart example.py -f function_name
# or
$ python3 -m pyflowchart example.py -f ClassName.method_name
0

conds-align (beta)

Cải thiện sơ đồ liên tiếp nếu các câu lệnh được chuyển đổi từ mã Python với tính năng mới của

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

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

Làm đẹp sơ đồ

Đôi khi, sơ đồ tạo ra là khủng khiếp. Trong những trường hợp đó, bạn được khuyến khích tự sửa đổi mã sơ đồ được tạo hoặc xem xét việc làm cho mã nguồn Python của bạn ở dưới cùng rõ ràng hơn nếu nó cực kỳ phức tạp.

Todos

  • Trực tiếp tạo sơ đồ SVG/HTML:
$ python3 -m pyflowchart example.py -f function_name
# or
$ python3 -m pyflowchart example.py -f ClassName.method_name
2

Phụ thuộc vào

element(param1=value1,param2=value2)=>start: Start
5 và
element(param1=value1,param2=value2)=>start: Start
6.

  • GUI Pyflowchart

Chà, tôi đoán một GUI cho Pyflowchart có thể rất đáng chú ý. Dán mã của bạn vào nó, DSL sơ đồ sẽ được tạo đúng lúc và sơ đồ sẽ được hiển thị sang một bên.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.

  • Người Trung Quốc Readme bạn bè của bạn chờ đợi!希望 有 帮助 个 中文 中文 readme 呀
  • Kiểm tra tự động hóa.

Đáng buồn thay, tôi quá bận rộn (phát âm là ____ 57 Vui lòng gửi một vấn đề để đẩy tôi vào. Hoặc, PR để làm cho nó một mình. Tôi không thể chờ đợi để đánh giá cao sự đóng góp lớn của bạn!

Người giới thiệu

  • Lấy cảm hứng từ Vatsha/Code_To_FlowChart
  • Dựa trên adrai/sơ đồ
  • Một blog về dự án này

Giấy phép

Bản quyền 2020-2022 CDFMLR.Đã đăng ký Bản quyền.

Được cấp phép theo giấy phép MIT.