PyFlowchart
PyFlowchart is a package to:
- write flowchart in Python,
- translate Python source codes into flowchart.
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
Flowchart in Python
PyFlowchart including flowchart.js node types:
- StartNode
- OperationNode
- ConditionNode
- InputOutputNode
- SubroutineNode
- EndNode
Nodes can be connected by connect() method (connect_{yes|no} for ConditionNode).
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')
# define the direction the connection will leave the node from
sub.set_connect_direction("right")
st.connect(op)
op.connect(cond)
cond.connect_yes(io)
cond.connect_no(sub)
sub.connect(op)
io.connect(e)
fc = Flowchart(st)
print(fc.flowchart())
Output:
st4471442960=>start: start a_pyflow_test
op4471442064=>operation: do something
cond4471501392=>condition: Yes or No?
io4471501648=>inputoutput: output: something...
e4471501904=>end: end a_pyflow_test
sub4471501584=>subroutine: A Subroutine
st4471442960->op4471442064
op4471442064->cond4471501392
cond4471501392(yes)->io4471501648
io4471501648->e4471501904
cond4471501392(no)->sub4471501584
sub4471501584(right)->op4471442064
Go http://flowchart.js.org and translate the generated textual representation into SVG flow chart diagrams:
P.S. Many Markdown editors (for example, Typora) support this flowchart syntax, too. And if you prefer CLI, see francoislaberge/diagrams.
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.
Advanced Usages
- Specify a field of code to generate flowchart (new feature: v0.1.0)
# 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")
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())
# CLI
python3 -m pyflowchart example.py -f Bar.buzz.g
Output result:
The from_code is defined as:
Flowchart.from_code(code, field='', inner=True)
PyFlowchart CLI is a interface for this function:
python3 -m pyflowchart [-h] [-f FIELD] [-i] code_file
field
field is the path to a field (i.e. a function) you want to draw flowchart. For example.py above, available paths are:
- "" (means the whole code)
- "foo"
- "Bar.buzz"
- "Bar.buzz.g"
inner
inner controls parser's behaves. 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 field as a node.
For CLI, adding an argument -i means inner=True, else inner=False.
Beautify Flowcharts
Modify the generated flowchart code by yourself.
TODO
- Generate flowchart SVG/HTML.
$ pyflowchart example.py -o flowchart.svg
Depends 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 flowchart will be shown aside.
- The Chinese README your buddies waiting for!
Sadly, I am too busy (this word is 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
- Inspired by Vatsha/code_to_flowchart
- Based on adrai/flowchart.js, python ast, simonpercivall/astunparse
- A blog about this project
License
Copyright 2020 CDFMLR. All rights reserved.
Licensed under the MIT License.
Metadata
Release files for pyflowchart 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyflowchart-0.1.0.tar.gz | 12.5 kB | Without provenance | Details for pyflowchart-0.1.0.tar.gz |
Release files / pyflowchart-0.1.0.tar.gz
| Download URL | pyflowchart-0.1.0.tar.gz |
|---|---|
| Size | 12.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
353901339669ed92b44236c3bbc076fc82ef8162b1d0cf7521134c19340e91eb
|
|
BLAKE2b-256 checksum How to use checksums |
7fb1dab4786fa46b70d04547e88d54ef8a5335380aa831ab03ecff04842cb89a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.6
|
Security for pyflowchart 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.