0

I'm working on a big python script that is getting more and more complex. I would like to generate a diagram (flow-chart?) of my code to have a better understanding of all the conditions and loops that are involved, even if the diagram doesn't understand the functions. That way, I could also check that the logic is OK.

Here is an example to illustrate : Let say I have this simple python script

a = 33
b = 200
name = "martin"

print("Starting script") if b > a: print("b is greater than a") if name == "martin": print("The name is martin") else: print("The name is not martin")

I could run a command in terminal like diagram generate myScript.py and it would output something like this :

example of diagram

Is there any package that could help me with that ?

karel
  • 114,770
B.T
  • 125
  • surely you're better off with unit and acceptance tests? Anything more than a trivial program is going to create an enormous diagram that you're going to struggle to comprehend. – Robert Longson Sep 16 '22 at 08:50
  • @RobertLongson I understand your point of view and I agree. The goal is exactly to review short-of trivial program – B.T Sep 16 '22 at 08:56

1 Answers1

1

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 an image via flowchart.js.org, francoislaberge/diagrams or some markdown editors.

To install PyFlowchart in all currently supported versions of Ubuntu open the terminal and type:

sudo apt install python3-pip
pip3 install pyflowchart

To make a flowchart for your example.py Python code run:

python3 -m pyflowchart example.py

PyFlowchart will output the generated flowchart.js DSL. Go to http://flowchart.js.org or use a markdown editor like Typora (sudo snap install typora) to turn the output code into a rendered logical diagram.

Source: revised from pyflowchart - PyPI

karel
  • 114,770