Callgraph Magic
Callgraph is a Python package that defines a decorator, and Jupyter magic, to draw dynamic call graphs of Python function calls.
It’s intended for classroom use, but may also be useful for self-guided exploration.
The package defines a Jupyter IPython magic, %callgraph, that displays a call graph within a Jupyter cell:
from functools import lru_cache
@lru_cache()
def lev(a, b):
if "" in (a, b):
return len(a) + len(b)
candidates = []
if a[0] == b[0]:
candidates.append(lev(a[1:], b[1:]))
else:
candidates.append(lev(a[1:], b[1:]) + 1)
candidates.append(lev(a, b[1:]) + 1)
candidates.append(lev(a[1:], b) + 1)
return min(candidates)
%callgraph -w10 lev("big", "dog"); lev("dig", "dog")
It also provides a Python decorator, callgraph.decorator, that instruments a function to collect call graph information and render the result.
Jupyter / IPython Usage
$ pip install callgraph
In a Jupyter IPython notebook:
%load_ext callgraph
def nchoosek(n, k):
if k == 0:
return 1
if n == k:
return 1
return nchoosek(n - 1, k - 1) + nchoosek(n - 1, k)
%callgraph nchoosek(4, 2)
As an alternative to including %load_ext callgraph in each notebook that uses %callgraph, you can add the extension to the Notebook configuration file in your IPython profile.
Your configuration file is probably called ~/.ipython/profile_default/ipython_config.py. (You can run ipython profile locate to find it.) Edit this file to include the following line:
c.InteractiveShellApp.extensions = ["callgraph.extension"]
(If your configuration file already includes an uncommented statement c.InteractiveShellApp.extensions = […], edit the list of extensions in that line to include "callgraph.extension".
See extension example notebook for additional examples.
Decorator Usage
$ pip install callgraph
from functools import lru_cache
import callgraph.decorator as callgraph
@callgraph()
@lru_cache()
def nchoosek(n, k):
if k == 0:
return 1
if n == k:
return 1
return nchoosek(n - 1, k - 1) + nchoosek(n - 1, k)
nchoosek(5, 2)
nchoosek.__callgraph__.view()
See the API documentation for additional documentation.
See the decorator example notebook for additional instructions and examples.
Development
Install dev tools, and set up a Jupyter kernel for the current python enviromnent:
$ pip install -r requirements-dev.txt
$ python -m ipykernel install --user
Install locally:
flit install --symlink
Acknowledgements
Callgraph uses the Python graphviz package. Python graphviz uses the Graphviz package.
License
MIT
Metadata
Release files for callgraph 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| callgraph-1.0.0.tar.gz | 55.6 kB | Without provenance | Details for callgraph-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| callgraph-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for callgraph-1.0.0-py3-none-any.whl |
Total release size: 71.0 kB
Release files / callgraph-1.0.0.tar.gz
| Download URL | callgraph-1.0.0.tar.gz |
|---|---|
| Size | 55.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7bf40a314734a5deff4cb6a1ad71b164dccf66b049dafe69536bea9d80fd4d07
|
|
BLAKE2b-256 checksum How to use checksums |
178c19aa947e7bfe9020c59b07e3ba3a3b39e1f16c54fd32c4bdf2f12d3d6b40
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / callgraph-1.0.0-py3-none-any.whl
| Download URL | callgraph-1.0.0-py3-none-any.whl |
|---|---|
| Size | 15.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
24db0ffa82a1eb037d90d030dacaa9494002a88bb06f4daf81266ebfa564eb2a
|
|
BLAKE2b-256 checksum How to use checksums |
6a6cbcd23ace79b168d8a6c2436d92c1321704e2fbb2165ef78119c9895d1db7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for callgraph 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.