Skip to main content

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!

Metadata

Release files for code-flowchart 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for code-flowchart 0.2.0
File Size Uploaded
code_flowchart-0.2.0.tar.gz 8.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for code-flowchart 0.2.0
File Interpreter ABI Platform
code_flowchart-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.2 kB

Release files / code_flowchart-0.2.0.tar.gz

Download URL code_flowchart-0.2.0.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
071dd6849c244a850405991358d210b7aa24e3a967ad2556a4757411c4a5d550
BLAKE2b-256 checksum
How to use checksums
3f42f327209ccda3aefe0531c8bfb485f35763d470aef949348a2a6e39815b86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Release files / code_flowchart-0.2.0-py3-none-any.whl

Download URL code_flowchart-0.2.0-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
795461a46891b7fda9257d440c557ce0221e877b299967815560d2b61f883806
BLAKE2b-256 checksum
How to use checksums
61c63946bc65553e2edcd2adfee576026ab088a7a494de6052c50b15c6f6ddf1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page