Use the root attribute in codebase.visualize(G, root=root_symbol) to visualize trees

1

Prompt

Generate an inheritance tree for the 'Symbol' class, showing all
subclasses up to 3 levels deep

You can adjust the depth and add specific conditions to refine your visualization.

2

Review

The assistant will provide a codemod using NetworkX to create the inheritance tree. Here’s an example of what the code might look like:

from app.graph_sitter.core import symbol

# Create a directed graph to represent the inheritance tree
G = nx.DiGraph()

symbol_class = codebase.get_symbol("Symbol")

# Add the Symbol class as the root node with red color
G.add_node(symbol_class, color="red")

def recursive_function(symbol_class):
    if isinstance(symbol_class, ExternalModule):
        return

    for subclass in symbol_class.get_superclasses(max_depth=1):
        # Create an edge from the current class to its subclass
        G.add_edge(symbol_class, subclass)
        recursive_function(subclass)

recursive_function(symbol_class)

# Visualize the graph using the codebase's visualization tool, with Symbol as the root
codebase.visualize(G, root=symbol_class)
3

Interact

Hit Run and view the graph in the Output Tab. For more advanced customization options, check out our customize page.