0

I'm working on a big python script that is getting more and more complexe. 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 ?

B.T
  • 125
  • 1
  • 2
  • 5
  • 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
  • 110,292
  • 102
  • 269
  • 299
  • Thank you I'm going to try that out – B.T Sep 16 '22 at 08:57
  • The command only display the flowchat right ? Cause I don't see any new file using it – B.T Sep 16 '22 at 09:03
  • flowchart DSL is a widely used flow chart textual representation. You need another app like Typora to turn the output code into a rendered logical diagram, or go to http://flowchart.js.org/. – karel Sep 16 '22 at 09:05
  • 1
    Indeed. It worked like that ! Thank you – B.T Sep 16 '22 at 09:08
  • 1
    To automatically generate any kind of flowchart in Ubuntu using the dot programming language see [this answer](https://askubuntu.com/a/917033/). – karel Sep 16 '22 at 09:14