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 ?

2
  • 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. Sep 16, 2022 at 8: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, 2022 at 8:56

1 Answer 1

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

5
  • Thank you I'm going to try that out
    – B.T
    Sep 16, 2022 at 8:57
  • The command only display the flowchat right ? Cause I don't see any new file using it
    – B.T
    Sep 16, 2022 at 9: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 flowchart.js.org.
    – karel
    Sep 16, 2022 at 9:05
  • 1
    Indeed. It worked like that ! Thank you
    – B.T
    Sep 16, 2022 at 9:08
  • 1
    To automatically generate any kind of flowchart in Ubuntu using the dot programming language see this answer.
    – karel
    Sep 16, 2022 at 9:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .