- Notifications
You must be signed in to change notification settings - Fork8
Magic to display dynamic call graphs of Python function calls
License
osteele/callgraph
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Callgraph is a Python package that defines a decorator, and Jupyter magic,to drawdynamic call graphs of Python function calls.
It’s intended for classroom use, but may also be useful for self-guidedexploration.
The package defines a JupyterIPythonmagic,%callgraph, thatdisplays a call graph within a Jupyter cell:
fromfunctoolsimportlru_cache@lru_cache()deflev(a,b):if""in (a,b):returnlen(a)+len(b)candidates= []ifa[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)returnmin(candidates)%callgraph-w10lev("big","dog");lev("dig","dog")
It also provides a Python decorator,callgraph.decorator, thatinstruments a function to collect call graph information and render theresult.
$ pip install callgraph
In a Jupyter IPython notebook:
%load_extcallgraphdefnchoosek(n,k):ifk==0:return1ifn==k:return1returnnchoosek(n-1,k-1)+nchoosek(n-1,k)%callgraphnchoosek(4,2)
As an alternative to including%load_ext callgraph in each notebook thatuses%callgraph, you can add the extension to the Notebookconfiguration file in your IPython profile.
Your configuration file is probably called~/.ipython/profile_default/ipython_config.py.(You can runipython 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 statementc.InteractiveShellApp.extensions = […], edit the list of extensions inthat line to include"callgraph.extension".
Seeextension example notebook for additional examples.
$ pip install callgraph
fromfunctoolsimportlru_cacheimportcallgraph.decoratorascallgraph@callgraph()@lru_cache()defnchoosek(n,k):ifk==0:return1ifn==k:return1returnnchoosek(n-1,k-1)+nchoosek(n-1,k)nchoosek(5,2)nchoosek.__callgraph__.view()
See theAPI documentation for additional documentation.
See thedecorator example notebook for additional instructions and examples.
Install dev tools, and set up a Jupyter kernel for the current pythonenviromnent:
$ pip install -r requirements-dev.txt$ python -m ipykernel install --user
Install locally:
flit install --symlink
Callgraph uses the Pythongraphviz package. Python graphviz usestheGraphviz package.
MIT
About
Magic to display dynamic call graphs of Python function calls
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.