• Stars
    star
    64
  • Rank 479,282 (Top 10 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Magic to display dynamic call graphs of Python function calls

Callgraph Magic

Latest PyPI Version Documentation Status License Supported Python Versions

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")

image0

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

More Repositories

1

functional-javascript

Functional is a library for functional programming in JavaScript. It defines the standard higher-order functions such as map, reduce (aka foldl), and select (aka filter). It also defines functions such as curry, rcurry, and partial for partial function application; and compose, guard, and until for function-level programming.
JavaScript
381
star
2

gojekyll

A fast Go implementation of the Jekyll blogging engine
Go
317
star
3

liquid

A Liquid template engine in Go
Go
243
star
4

dart-tonic

Music theory Dart package
Dart
50
star
5

jquery-profile

jQuery plugin to profile calls to jQuery selectors.
JavaScript
39
star
6

p5-server

Command-line tool to create and run p5.js sketches. It runs a server with live reload, sketch-aware directory listings, automatic libraries for JavaScript-only sketches.
TypeScript
38
star
7

collections-js

Framework-independent JavaScript collection methods.
JavaScript
32
star
8

ipython-secrets

A Python package that simplifies the use of secrets in a Jupyter notebook
Python
21
star
9

matrix-archive

Export a Matrix room message archive
Python
20
star
10

cl-spec

BDD for Common Lisp
Common Lisp
18
star
11

jsspec

A clone of Alan Kang's JSSpec
JavaScript
17
star
12

tonic.ts

TypeScript music theory, pitch constellation diagram, and guitar chord calculator
TypeScript
15
star
13

sequentially

Sequentially is a library of temporal and frequency adverbs for JavaScript. It provides methods to queue a function for deferred or periodic execution, and to throttle the rate or number of times that a function can be called. You could think of it as a kind of memoization, where instead of caching the result it modifies when and whether a function is called.
JavaScript
14
star
14

imu-tools

Send sensor data from an ESP + BNO055 β†’ MQTT and/or serial port
Python
12
star
15

tuesday

Ruby-compatible strftime for golang
Go
12
star
16

mop-js

JavaScript utilities for Metaobject Programming
JavaScript
12
star
17

fluently

Fluent programming (chained method calls) for JavaScript.
JavaScript
12
star
18

Arduino-BLE-IMU

Publish IMU data over BLE
C++
10
star
19

javascript_fu

A Rails plugin to add more support for javascript files.
Ruby
9
star
20

banyan

Visualize Dropbox file and folder sizes
Elm
9
star
21

pyfsa

Python FSA constructor, determinizer, and minimizer.
Python
9
star
22

vscode-p5server

VSCode extension to launch a live server that is aware of p5.js
CSS
8
star
23

fingerboard

Display the violin, viola, and cello fingerings for each scale
CoffeeScript
8
star
24

gsheet-keyring

Python Keyring backend backed by a Google Sheet
Python
7
star
25

db_content

Rails plugin to add sql dump and restore tasks
Ruby
7
star
26

bootle

β€œShould array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration.” β€” Stan Kelly-Bootle
Python
7
star
27

cfdg-js

A JavaScript implementation of Chris Coyne's Context Free Design Grammar.
JavaScript
7
star
28

p5.libs

JavaScript
6
star
29

flinx

Configuration-free Sphinx documentation
Python
5
star
30

p5-react

A component that embeds a p5.js canvas in a React application
JavaScript
5
star
31

classroom-tools

Tools for collecting and analyzing assignments – mostly related to GitHub and Jupyter notebooks – plus a few other tasks.
Python
5
star
32

sneetches

A Chrome extension that adds star counts next to GitHub repo links
TypeScript
5
star
33

openlaszlo-json

JSON library for OpenLaszlo
JavaScript
5
star
34

multiclone

Clone forks of a GitHub repo, or copies of a GitHub Classroom assignment
Go
4
star
35

lzosutils

OpenLaszlo utilities: flash bridge, ajax, etc.
JavaScript
4
star
36

openlaszlo_plugin

The OpenLaszlo Rails plugin makes it easy to use OpenLaszlo client-side applications with Rails.
JavaScript
4
star
37

jcon

Conformance checking of JSON values against ECMAScript 4.0 types
Ruby
4
star
38

fretboard

Tools for rendering guitar fretboard and chord diagrams.
JavaScript
4
star
39

git-timelapse

Create time-lapse movies of git repo files, metrics, or other artifacts
Python
4
star
40

wideurl.com

The PHP behind 2006's wideurl.com.
PHP
3
star
41

html2dbk

HTML to Docbook converter
HTML
3
star
42

QtTileDual

Draw a graph and its duals. I wrote this to learn Qt.
C++
3
star
43

python-utils

Oliver's Python utilities
Python
3
star
44

p5pose

Starter kit for exploring WebCam-based pose recognition with PoseNet and p5.js
JavaScript
3
star
45

p5-template

https://www.notion.so/P5-js-Particle-Workshop-aba7992a689c457a8cca10e2f49e6a04
HTML
3
star
46

storyboard

An ruby-processing extension for scripting storyboarded explanatory visualizations. In progress.
Ruby
2
star
47

gem_recent-updates

A gem command plugin that displays the tops of the history files of recently updated gems.
Ruby
2
star
48

code.osteele.com

Elm
2
star
49

notebooks

Miscellaneous Jupyter notebooks.
Jupyter Notebook
2
star
50

inventiveminds.xyz

Source to Inventive Minds web site
Ruby
2
star
51

p5pose-optitrack

Use p5.js to render data from an OptiTrack CSV -> WebSocket server.
JavaScript
2
star
52

osteele.com

Source to personal web site
PHP
2
star
53

gh-forkstats

Displays GitHub forks and their stats, to identify successors to abandoned repos.
JavaScript
2
star
54

ffmachine

DEC Digital Logic Module editor and simulator
CoffeeScript
2
star
55

lztestkit

BDD for OpenLaszlo.
JavaScript
2
star
56

hackingthelibrary.org

Hacking the Library 2017 and 2018 @ Olin College
HTML
2
star
57

imu-client-examples

Example web programs that use the imu-tools npm package to connect via BTLE or MQTT to an ESP32 + BNO055
JavaScript
2
star
58

ropenlaszlo

ROpenLaszo is a Ruby interface to the OpenLaszlo compiler. It allows you to compile OpenLaszlo programs from within Ruby, in order to integrate OpenLaszlo development into Rake or Rails applications.
Ruby
2
star
59

git-keychain-secrets

Store some parts of a repo file in the macOS Keychain; keep the rest un-encrypted
Ruby
2
star
60

minimal-keys

Compute the unique minimal keys from a collection of strings or sequences.
Python
2
star
61

old-blog

Ruby
1
star
62

tidal-memories

On-the-ground piece for Dinacon
JavaScript
1
star
63

matrix-photo-gallery

A photo gallery for photos from a Matrix room.
JavaScript
1
star
64

liquid-tabulator

Olin Election Techology Co-Curricular Vote Tabulator
Python
1
star
65

html2cheatsheet

Create a Dash Cheatsheet from HTML on apple.com
Python
1
star
66

terminal-codes-to-html

Convert strings that include terminal color codes to HTML or plain text.
TypeScript
1
star
67

changelog-parser

An npm package that parses CHANGELOG files from markdown to JSON.
CSS
1
star
68

nbcollate

Collate Jupyter assignment notebooks
Jupyter Notebook
1
star
69

p5-orientation-and-motion-example

JavaScript
1
star
70

expialidocious

Timeline tag browser for delicious, in OpenLaszlo
JavaScript
1
star
71

protodoc

JavaScript documentation generator and pydoc equivalent
JavaScript
1
star
72

signage

Web site playlist manager for digital signage
JavaScript
1
star
73

openlaszlo-rails-example

Ruby
1
star
74

volatility

Web page that displays the current value of Bitcoin alongside the face of a six-sided die
Elm
1
star
75

elm-extras

Personal Elm extras. Extracted from Banyan.
Elm
1
star
76

pwm-explorer

Interactive visualization of Pulse Width Modulation (PWM)
JavaScript
1
star
77

micropython-stubs

Stubs for MicroPython APIs, to assist type-checking tools
Python
1
star