Skip to main content

call_tree_dir()

Build the call tree of the functions defined in a folder of .py files.

Usage

Source

call_tree_dir(
    path="src",
    root="",
    modules_only=False,
    exclude=None,
    max_depth=None,
    labels="def"
)

call_tree_dir() parses every .py file under path (subfolders included) without importing or running any of it. It collects each top level function (def name(...) or name = lambda ...) and each class method (as Class.method), then links a function to every other collected function its body calls or names. The result is the tree reachable from root.

Names are resolved through each file’s imports, so mdl.fit() after import pkg.model as mdl, plots.draw() after from . import plots, and g() after from .m import f as g all link to the function they name. A module level instance (model = Model()) links model.fit() to Model.fit. A class call links to Class.__init__ when the class defines one, Class().fit() links to Class.fit, and self.m() or cls.m() inside a method links to m on the same class (base classes are not searched). Nodes are labeled with the name of the definition, not the alias used at the call site. Folders named great-docs or _freeze (docs build output) and hidden folders are skipped.

Because nothing is imported, it works on a package’s src/ folder or a Shiny for Python app before it is installed.

A launch function often names the app as a string, as in shiny.run_app("myapp.app:app"). Such a "module:attr" string is followed when module is one of the scanned files. When attr is a module level assignment (app = create_app()), the string becomes a node whose children are the functions that assignment calls or names. When attr is a function (an app factory), it links straight to it.

Parameters

path: str | Path = "src"

Folder to scan. Default "src".

root: str = ""

Name of the function to root the tree at: a top level function found under path, or a method written as "Class.method".

modules_only: bool = False

When True, keep only root and functions decorated with Shiny’s @module.ui or @module.server. Every other function is spliced out and its children move up, so modules reached through a plain wrapper stay visible.

exclude: str | Iterable[str] | None = None

Function names to drop from the tree; their children move up into their place. The root is never dropped.

max_depth: int | None = None

How many levels to expand. None (the default) expands everything.

labels: str = "def"
"def" (the default) labels each node by its definition name (Model.fit). "call" uses the text at the call site (model.fit) for the edge into each node. Nested functions are always labeled outer.inner.

Returns

CallTree
The root node. Print it to draw the tree.

Warns

UserWarning
When two files define a top level function with the same name. The file parsed last (in sorted path order) wins.

Examples

>>> import tempfile
>>> from pathlib import Path
>>> app_dir = Path(tempfile.mkdtemp())
>>> _ = (app_dir / "app.py").write_text('''
... from shiny import App, module, ui

…

... @module.ui
... def counter_ui():
...     return ui.input_action_button("go", "Go")

…

... @module.server
... def counter_server(input, output, session):
...     pass

…

... def app_ui():
...     return ui.page_fluid(counter_ui("a"))

…

... def app_server(input, output, session):
...     counter_server("a")

…

... def launch():
...     return App(app_ui(), app_server)
... ''')
>>> call_tree_dir(app_dir, root="launch")

█─launch ├─█─app_ui │ └─counter_ui └─█─app_server └─counter_server

>>> call_tree_dir(app_dir, root="launch", modules_only=True)

█─launch ├─counter_ui └─counter_server