Skip to main content

A simple tool for creating flowcharts of code

Project description

code-flowchart

GitHub Tag PyPI - Version PyPI - Python Version GitHub License

code-flowchart is a simple tool for generating flowcharts (in both DOT and PNG formats) directly from your Python source code. It leverages Python's built-in AST (Abstract Syntax Tree) parsing and utilizes Graphviz to render visual diagrams.

Installation

Install via pip:

pip install code-flowchart

Note
This tool depends on Graphviz. Even if you install a Python package for Graphviz, you might need to install Graphviz system-wide or configure your environment path. If you encounter issues, try installing Graphviz via conda:

conda install graphviz

Usage

Once installed, you can run code-flowchart from the command line:

code_flowchart {python_file_path} -o {output_file_name}
  • {python_file_path}: Path to your Python source file.
  • -o {output_file_name}: (Optional) Output filename prefix. By default, it saves as code_flowchart.dot and code_flowchart.png.

The command will generate a DOT file (.dot) and a corresponding PNG image in the current directory.

Example

Consider the following main.py

def isOdd(num):
    if not num%2==0:
        return True
    else:
        print("It's not odd!!!")
    return False

for i in range(3):
    print(isOdd(i))

To create a flowchart (DOT + PNG), simply run:

code_flowchart main.py -o output

output.dot

Click to expand
digraph {
	rankdir=TB
	nodesep=0.5 ranksep=0.75
	dpi=300 size="10,10"
{rank=source; "start_1";}{rank=same; "for_2";}{rank=same; "expr_3";}{rank=same; "call_4";}{rank=same; "function_5";}{rank=same; "if_6";}{rank=same; "return_7";}{rank=same; "else_8";}{rank=same; "return_9";}{rank=same; "if_end_10";}	return_7 -> call_4 [label=Return color=black fontsize=10 style=dashed]
	return_9 -> call_4 [label=Return color=black fontsize=10 style=dashed]
{rank=sink; "end_11";}	subgraph cluster_functions {
		color=green fontsize=16 label=Functions style=dashed
		function_5 [label="Function: isOdd" fontsize=12 shape=ellipse]
		if_6 [label="If: num % 2 == 0" fontsize=12 shape=diamond]
		function_5 -> if_6 [label="" color=black fontsize=10 style=solid]
		return_7 [label="Return: \"It's not odd!!!\"" fontsize=12 shape=box]
		if_6 -> return_7 [label=True color=black fontsize=10 style=solid]
		else_8 [label=Else fontsize=12 shape=diamond]
		if_6 -> else_8 [label=False color=black fontsize=10 style=solid]
		return_9 [label="Return: \"It's odd!!!\"" fontsize=12 shape=box]
		else_8 -> return_9 [label="" color=black fontsize=10 style=solid]
		if_end_10 [label="If End" fontsize=12 shape=circle]
	}
	subgraph cluster_main {
		color=blue fontsize=16 label="Main Code" style=dashed
		start_1 [label=Start fontsize=12 shape=ellipse]
		for_2 [label="For: i in range(3)" fontsize=12 shape=box]
		start_1 -> for_2 [label="" color=black fontsize=10 style=solid]
		expr_3 [label="Expression: print(isOdd(i))" fontsize=12 shape=box]
		for_2 -> expr_3 [label="" color=black fontsize=10 style=solid]
		call_4 [label="Call: isOdd(i)" fontsize=12 shape=box]
		expr_3 -> call_4 [label="Arg Call" color=black fontsize=10 style=solid]
		call_4 -> function_5 [label=Call color=black fontsize=10 style=dashed]
		expr_3 -> for_2 [label=Repeat color=black fontsize=10 style=dotted]
		end_11 [label=End fontsize=12 shape=ellipse]
		for_2 -> end_11 [label="" color=black fontsize=10 style=solid]
	}
}

output.png

Below is a preview of the generated flowchart (the actual image may vary based on the code and your Graphviz configuration):

Supported syntax

The library currently supports visualizing:

  • FunctionDef (function definitions)
  • Call (function calls, including user-defined functions)
  • If / Else / Elif
  • For
  • While
  • Return
  • Expr (print, assignments, or any standalone expressions) Other Python syntax nodes are either skipped or handled as generic expressions.

License

This project is licensed under the MIT License.
Feel free to use, modify, and distribute this tool in accordance with the license terms.
Contributions are welcome!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

code_flowchart-0.2.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

code_flowchart-0.2.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file code_flowchart-0.2.0.tar.gz.

File metadata

  • Download URL: code_flowchart-0.2.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for code_flowchart-0.2.0.tar.gz
Algorithm Hash digest
SHA256 071dd6849c244a850405991358d210b7aa24e3a967ad2556a4757411c4a5d550
MD5 08b4d2d0ff5b0ef045ae08bf6985e092
BLAKE2b-256 3f42f327209ccda3aefe0531c8bfb485f35763d470aef949348a2a6e39815b86

See more details on using hashes here.

File details

Details for the file code_flowchart-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: code_flowchart-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for code_flowchart-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 795461a46891b7fda9257d440c557ce0221e877b299967815560d2b61f883806
MD5 db8023fc53a0322ecff62651b0f4a789
BLAKE2b-256 61c63946bc65553e2edcd2adfee576026ab088a7a494de6052c50b15c6f6ddf1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page