Shapour
Shapour

Reputation: 29

python notebook Callgraph

I am trying to creat a callgraph of my python code in Notebook. I tried the %callgraph and it seems to work only for one level (i.e., great for recursive functions). So, it only shows the first level of the tree.

Am I doing something wrong? is there another way to create call graphs for python/notebook?

Upvotes: 0

Views: 339

Answers (2)

Rub
Rub

Reputation: 2708

Q: Am I doing something wrong?

A: Unless you show us what you have done, we cannot answer that.

Q: Is there another way to create call graphs for python/notebook?

A: Yes, most certainly.

There are many 'callgraph' out there. Which one have you used?

I tried the %callgraph and it seems to work only for one level.the tree.

I tried this one https://callgraph.readthedocs.io

I just started reading the documentation trying to figure it out, but I got for you an example proving it can.

recorder = CallGraphRecorder()

@callgraph(recorder=recorder)
@lru_cache()
def f1(a):
  if a>5:
    return f2(a)
  else:
    return a

@callgraph(recorder=recorder)
@lru_cache()
def f2(a):
  if a>10:
    return f3(a-5)
  else:
    return a-5

@callgraph(recorder=recorder)
@lru_cache()
def f3(a):
  if a>15:
    return a
  else:
    return a-5


for i in range(9,14):
  f1(i)

recorder.graph

enter image description here

Upvotes: 0

Justin Lin
Justin Lin

Reputation: 761

try use -w flag. for example, %callgraph -w10 lev("big", "dog"); lev("dig", "dog")

source: https://callgraph.readthedocs.io/en/latest/

Upvotes: -1

Related Questions