🔍 Code Extractor

function get_nested_dmaps

Maturity: 43

Recursively traverses a DynamicMap object to find and collect all nested DynamicMap instances that serve as inputs to the callback function.

File:
/tf/active/vicechatdev/patches/spaces.py
Lines:
631 - 645
Complexity:
simple

Purpose

This function is used to discover the dependency graph of DynamicMap objects by recursively examining the inputs of each DynamicMap's callback. It's useful for understanding the structure of complex dynamic visualizations, managing dependencies, or performing operations on all DynamicMaps in a hierarchy. The function returns a deduplicated list of all DynamicMap instances found in the input tree.

Source Code

def get_nested_dmaps(dmap):
    """Recurses DynamicMap to find DynamicMaps inputs

    Args:
        dmap: DynamicMap to recurse to look for DynamicMap inputs

    Returns:
        List of DynamicMap instances that were found
    """
    if not isinstance(dmap, DynamicMap):
        return []
    dmaps = [dmap]
    for o in dmap.callback.inputs:
        dmaps.extend(get_nested_dmaps(o))
    return list(set(dmaps))

Parameters

Name Type Default Kind
dmap - - positional_or_keyword

Parameter Details

dmap: A DynamicMap object to analyze. If the input is not a DynamicMap instance, the function returns an empty list. The function will recursively examine this DynamicMap's callback.inputs attribute to find nested DynamicMap instances.

Return Value

Returns a list of unique DynamicMap instances found through recursive traversal. The list includes the input dmap itself (if it's a DynamicMap) plus all nested DynamicMaps discovered in the callback inputs. Duplicates are removed using set() conversion. Returns an empty list if the input is not a DynamicMap.

Dependencies

  • holoviews

Required Imports

from holoviews.core.spaces import DynamicMap

Usage Example

from holoviews.core.spaces import DynamicMap
import holoviews as hv
import numpy as np

# Create a simple DynamicMap
def plot_func(x):
    return hv.Curve(np.sin(np.linspace(0, x, 100)))

dmap1 = DynamicMap(plot_func, kdims=['x']).redim.range(x=(0, 10))

# Create a nested DynamicMap that depends on dmap1
def transform_func(curve):
    return curve * 2

dmap2 = DynamicMap(transform_func, streams=[dmap1])

# Get all nested DynamicMaps
nested = get_nested_dmaps(dmap2)
print(f"Found {len(nested)} DynamicMap(s)")
# Output: Found 2 DynamicMap(s) (includes dmap2 and dmap1)

# Test with non-DynamicMap input
result = get_nested_dmaps("not a dmap")
print(result)  # Output: []

Best Practices

  • Ensure the input is a DynamicMap object to get meaningful results; non-DynamicMap inputs will return an empty list
  • Be aware that the function deduplicates results, so each unique DynamicMap appears only once in the output regardless of how many times it appears in the dependency tree
  • The function includes the input dmap itself in the results if it's a valid DynamicMap
  • This function assumes that DynamicMap objects have a 'callback' attribute with an 'inputs' property
  • Use this function to understand complex DynamicMap dependency chains before performing operations that might affect multiple related visualizations

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_nested_streams 79.6% similar

    Recursively traverses a DynamicMap object to extract and return all unique Stream objects found within it and its nested DynamicMaps.

    From: /tf/active/vicechatdev/patches/spaces.py
  • class DynamicMap 57.1% similar

    A DynamicMap is a type of HoloMap where the elements are dynamically generated by a callable. The callable is invoked with values associated with the key dimensions or with values supplied by stream parameters.

    From: /tf/active/vicechatdev/patches/spaces.py
  • function dimensioned_streams 53.3% similar

    Filters and returns streams from a DynamicMap that have parameters matching the DynamicMap's key dimensions.

    From: /tf/active/vicechatdev/patches/util.py
  • function dynamicmap_memoization 49.9% similar

    A context manager that temporarily controls memoization behavior of a callable object based on the state of associated streams, disabling memoization when transient streams are triggering.

    From: /tf/active/vicechatdev/patches/spaces.py
  • function get_unique_keys 47.8% similar

    Extracts unique key values from an ndmapping object for specified dimensions, returning an iterator of unique tuples.

    From: /tf/active/vicechatdev/patches/util.py
← Back to Browse