Installation:
- Install using pip:
$ pip install pyflowchart
Basic Usage:
-
Flowchart Python code from the command line:
$ python -m pyflowchart example.py
- Replace
example.pywith the name of your Python file. - This outputs the flowchart in flowchart.js DSL.
- Replace
-
Output to an HTML file:
$ python -m pyflowchart example.py -o example.html
- This creates an interactive HTML file (
example.html) displaying the flowchart.
- This creates an interactive HTML file (
-
Flowchart a specific function or method:
$ python -m pyflowchart example.py -f function_name
- Replace
function_namewith the actual function name. - For methods:
$ python -m pyflowchart example.py -f ClassName.method_name
- Replace
Flowcharting in Python:
-
Import necessary classes:
from pyflowchart import *
-
Create nodes:
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')
-
Connect the nodes:
st.connect(op) op.connect(cond) cond.connect_yes(io) cond.connect_no(sub) sub.connect(op, "right") io.connect(e)
-
Create a Flowchart object and generate the flowchart DSL:
fc = Flowchart(st) print(fc.flowchart())
-
Output to an HTML file (programmatically):
output_html('output.html', 'a_pyflow_test', fc.flowchart())
Python to Flowchart (from code):
- Generate a flowchart from Python code:
from pyflowchart import Flowchart
with open('simple.py') as f:
code = f.read()
fc = Flowchart.from_code(code)
print(fc.flowchart())- Replace
simple.pywith your Python filename.
Advanced Usage (from code):
- Using
Flowchart.from_code():
Flowchart.from_code(code, field="", inner=True, simplify=True, conds_align=False) code: Python code to convert.field: Specific function/method to flowchart (e.g., "foo" or "Bar.buzz").inner: IfTrue, parse the body offieldas nested. IfFalse, treat as a single node.simplify: IfTrue, simplify one-lineifandwhilestatements.conds_align: IfTrue, align consecutiveifstatements.
Command Line Interface (CLI):
- Advanced options with CLI:
python -m pyflowchart [-f FIELD] [-i] [--no-simplify] [--conds-align] [-o OUTPUT] code_file -f FIELD: Specify thefield(function/method).-i: Equivalent toinner=True.--no-simplify: Disable simplification (simplify=False).--conds-align: Enable alignment ofifstatements.-o OUTPUT: Output to a file (e.g.,-o output.html).