Function Graph Generator

Plot mathematical functions in the browser.

Automatically generate function call graphs for Python code.

graph.py is a Python script that analyzes Python source code and generates visual function call graphs showing dependencies between functions.

Features

Usage

1. Prepare Source Code

Create a file containing your Python source code. For example, you can combine multiple Python files into a single input file:

find ./src/ -name '*.py' -exec cat {} \; > input.txt

or

cat ./src/*.py > input.txt

2. Generate Call Graph

Run the script:

python3 graph.py

A window will display the interactive graph. You can zoom, pan, and inspect nodes.

Configuration

Modify the generator's configuration before generating the graph:

generator = CallGraphGenerator()
generator.config["allow_external_funcs"] = False  # Exclude functions called but not defined in the source
generator.config["allow_self_reference"] = False  # Hide recursive calls
generator.config["allow_childless_funcs"] = False  # Omit functions that do not call other functions
generator.generate_call_graph("input.txt")
Setting Default Description
allow_external_funcs True Show functions called but not defined in the source
allow_self_reference True Display edges for recursive calls
allow_childless_funcs True Include functions that don't call other functions

Testing

Unit tests verify function extraction and graph generation. Run tests with:

python3 -m unittest test.py

Dependencies

Install requirements:

pip install --user networkx matplotlib

How It Works

  1. Language Detection: Checks the first 10 lines for the Python-specific pattern:

    • Python: def keywords at the beginning of a line (allowing for leading spaces).
  2. Parsing: The Python parser extracts function definitions and calls.

  3. Graph Construction: Uses networkx to build a directed graph from call relationships.

  4. Visualization: Applies the spring layout algorithm and matplotlib for interactive display.

Notes

Example Output

Interact with the graph to:

Documentation (1)

Files in Documentation

Related