Skip to content

contract.utils

print_data_stream_tree

print_data_stream_tree(
    node: DataStream,
    prefix: str = "",
    is_last: bool = True,
    parents: list[bool] = [],
    show_params: bool = False,
    show_type: bool = False,
    show_missing_indicator: bool = True,
) -> str

Generates a tree representation of a data stream hierarchy.

Creates a formatted string displaying the hierarchical structure of a data stream and its children as a tree with branch indicators and icons.

Parameters:

Name Type Description Default
node DataStream

The data stream node to start printing from.

required
prefix str

Prefix string to prepend to each line, used for indentation.

''
is_last bool

Whether this node is the last child of its parent.

True
parents list[bool]

List tracking whether each ancestor was a last child, used for drawing branches.

[]
show_params bool

Whether to render parameters of the datastream.

False
show_type bool

Whether to render the class name of the datastream.

False
show_missing_indicator bool

Whether to render the missing data indicator.

True

Returns:

Name Type Description
str str

A formatted string representing the data stream tree.

Examples:

from contraqctor.contract import Dataset, csv, json
from contraqctor.contract.utils import print_data_stream_tree

csv_stream = csv.Csv("data", reader_params=csv.CsvParams(path="data.csv"))
json_stream = json.Json("config", reader_params=json.JsonParams(path="config.json"))
dataset = Dataset("experiment", [csv_stream, json_stream], version="1.0.0")

tree = print_data_stream_tree(dataset)
print(tree)
Source code in src/contraqctor/contract/utils.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def print_data_stream_tree(
    node: DataStream,
    prefix: str = "",
    is_last: bool = True,
    parents: list[bool] = [],
    show_params: bool = False,
    show_type: bool = False,
    show_missing_indicator: bool = True,
) -> str:
    """Generates a tree representation of a data stream hierarchy.

    Creates a formatted string displaying the hierarchical structure of a data stream
    and its children as a tree with branch indicators and icons.

    Args:
        node: The data stream node to start printing from.
        prefix: Prefix string to prepend to each line, used for indentation.
        is_last: Whether this node is the last child of its parent.
        parents: List tracking whether each ancestor was a last child, used for drawing branches.
        show_params: Whether to render parameters of the datastream.
        show_type: Whether to render the class name of the datastream.
        show_missing_indicator: Whether to render the missing data indicator.

    Returns:
        str: A formatted string representing the data stream tree.

    Examples:
        ```python
        from contraqctor.contract import Dataset, csv, json
        from contraqctor.contract.utils import print_data_stream_tree

        csv_stream = csv.Csv("data", reader_params=csv.CsvParams(path="data.csv"))
        json_stream = json.Json("config", reader_params=json.JsonParams(path="config.json"))
        dataset = Dataset("experiment", [csv_stream, json_stream], version="1.0.0")

        tree = print_data_stream_tree(dataset)
        print(tree)
        ```
    """
    node_icon = _get_node_icon(node, show_missing_indicator)
    line_prefix = _build_line_prefix(parents, is_last)
    node_label = _build_node_label(node, show_type, show_params)

    tree_representation = f"{line_prefix}{node_icon} {node_label}\n"

    if node.is_collection and node.has_data:
        for i, child in enumerate(node.data):
            child_is_last = i == len(node.data) - 1
            tree_representation += print_data_stream_tree(
                child,
                prefix="",
                is_last=child_is_last,
                parents=parents + [is_last],
                show_params=show_params,
                show_type=show_type,
                show_missing_indicator=show_missing_indicator,
            )

    return tree_representation

print_data_stream_tree_html

print_data_stream_tree_html(
    node: DataStream,
    is_last: bool = True,
    parents: list[bool] | None = None,
    show_params: bool = False,
    show_type: bool = False,
    show_missing_indicator: bool = True,
) -> str

Generates an HTML tree representation of a data stream hierarchy with tooltips.

Creates a formatted HTML string displaying the hierarchical structure of a data stream and its children as a tree with branch indicators, icons, and tooltips showing descriptions.

Parameters:

Name Type Description Default
node DataStream

The data stream node to start printing from.

required
is_last bool

Whether this node is the last child of its parent.

True
parents list[bool] | None

List tracking whether each ancestor was a last child, used for drawing branches.

None
show_params bool

Whether to render parameters of the datastream.

False
show_type bool

Whether to render the class name of the datastream.

False
show_missing_indicator bool

Whether to render the missing data indicator.

True

Returns:

Name Type Description
str str

A formatted HTML string representing the data stream tree with tooltips.

Examples:

from contraqctor.contract import Dataset, csv, json
from contraqctor.contract.utils import print_data_stream_tree_html

csv_stream = csv.Csv("data", reader_params=csv.CsvParams(path="data.csv"))
json_stream = json.Json("config", reader_params=json.JsonParams(path="config.json"))
dataset = Dataset("experiment", [csv_stream, json_stream], version="1.0.0")

html = print_data_stream_tree_html(dataset)
with open("tree.html", "w") as f:
    f.write(html)
Source code in src/contraqctor/contract/utils.py
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
def print_data_stream_tree_html(
    node: DataStream,
    is_last: bool = True,
    parents: list[bool] | None = None,
    show_params: bool = False,
    show_type: bool = False,
    show_missing_indicator: bool = True,
) -> str:
    """Generates an HTML tree representation of a data stream hierarchy with tooltips.

    Creates a formatted HTML string displaying the hierarchical structure of a data stream
    and its children as a tree with branch indicators, icons, and tooltips showing descriptions.

    Args:
        node: The data stream node to start printing from.
        is_last: Whether this node is the last child of its parent.
        parents: List tracking whether each ancestor was a last child, used for drawing branches.
        show_params: Whether to render parameters of the datastream.
        show_type: Whether to render the class name of the datastream.
        show_missing_indicator: Whether to render the missing data indicator.

    Returns:
        str: A formatted HTML string representing the data stream tree with tooltips.

    Examples:
        ```python
        from contraqctor.contract import Dataset, csv, json
        from contraqctor.contract.utils import print_data_stream_tree_html

        csv_stream = csv.Csv("data", reader_params=csv.CsvParams(path="data.csv"))
        json_stream = json.Json("config", reader_params=json.JsonParams(path="config.json"))
        dataset = Dataset("experiment", [csv_stream, json_stream], version="1.0.0")

        html = print_data_stream_tree_html(dataset)
        with open("tree.html", "w") as f:
            f.write(html)
        ```
    """
    import html as html_module

    if parents is None:
        parents = []

    html_header = _get_html_header() if not parents else ""
    node_icon = _get_node_icon(node, show_missing_indicator)
    line_prefix = _build_line_prefix(parents, is_last)
    node_label = _build_node_label(node, show_type, show_params)
    tooltip_text = _get_tooltip_text(node)

    node_label_escaped = html_module.escape(node_label)
    line_prefix_escaped = html_module.escape(line_prefix)

    tooltip_span = f'<span class="tooltiptext">{tooltip_text}</span>'
    node_content = f"{node_icon} {node_label_escaped}{tooltip_span}"
    tree_representation = f'{html_header}{line_prefix_escaped}<span class="tooltip">{node_content}</span>\n'

    if node.is_collection and node.has_data:
        for i, child in enumerate(node.data):
            child_is_last = i == len(node.data) - 1
            tree_representation += print_data_stream_tree_html(
                child,
                is_last=child_is_last,
                parents=parents + [is_last],
                show_params=show_params,
                show_type=show_type,
                show_missing_indicator=show_missing_indicator,
            )

    if not parents:
        tree_representation += """    </div>
</body>
</html>
"""

    return tree_representation