@@ -73,6 +73,7 @@ def print_tree(
73
73
attr_omit_null : bool = False ,
74
74
attr_bracket : List [str ] = ["[" , "]" ],
75
75
style : Union [str , Iterable [str ], BasePrintStyle ] = "const" ,
76
+ ** print_kwargs ,
76
77
) -> None :
77
78
"""Print tree to console, starting from `tree`.
78
79
@@ -90,6 +91,8 @@ def print_tree(
90
91
- (BasePrintStyle): `ANSIPrintStyle`, `ASCIIPrintStyle`, `ConstPrintStyle`, `ConstBoldPrintStyle`, `RoundedPrintStyle`,
91
92
`DoublePrintStyle` style or inherit from `BasePrintStyle`
92
93
94
+ Remaining kwargs are passed without modification to python's `print` function.
95
+
93
96
Examples:
94
97
**Printing tree**
95
98
@@ -188,6 +191,19 @@ def print_tree(
188
191
| `-- e
189
192
`-- c
190
193
194
+ **Printing to a file**
195
+
196
+ >>> import io
197
+ >>> output = io.StringIO()
198
+ >>> print_tree(root, file=output)
199
+ >>> string = output.getvalue()
200
+ >>> print(string)
201
+ a
202
+ ├── b
203
+ │ ├── d
204
+ │ └── e
205
+ └── c
206
+
191
207
Args:
192
208
tree (Node): tree to print
193
209
node_name_or_path (str): node to print from, becomes the root node of printing
@@ -232,7 +248,7 @@ def print_tree(
232
248
if attr_str :
233
249
attr_str = f" { attr_bracket_open } { attr_str } { attr_bracket_close } "
234
250
node_str = f"{ _node .node_name } { attr_str } "
235
- print (f"{ pre_str } { fill_str } { node_str } " )
251
+ print (f"{ pre_str } { fill_str } { node_str } " , ** print_kwargs )
236
252
237
253
238
254
def yield_tree (
@@ -416,6 +432,7 @@ def hprint_tree(
416
432
max_depth : int = 0 ,
417
433
intermediate_node_name : bool = True ,
418
434
style : Union [str , Iterable [str ], BaseHPrintStyle ] = "const" ,
435
+ ** print_kwargs ,
419
436
) -> None :
420
437
"""Print tree in horizontal orientation to console, starting from `tree`.
421
438
@@ -430,6 +447,8 @@ def hprint_tree(
430
447
- (BaseHPrintStyle): `ANSIHPrintStyle`, `ASCIIHPrintStyle`, `ConstHPrintStyle`, `ConstBoldHPrintStyle`,
431
448
`RoundedHPrintStyle`, `DoubleHPrintStyle` style or inherit from BaseHPrintStyle
432
449
450
+ Remaining kwargs are passed without modification to python's `print` function.
451
+
433
452
Examples:
434
453
**Printing tree**
435
454
@@ -504,6 +523,17 @@ def hprint_tree(
504
523
- a -+ \\ - e
505
524
\\ - c
506
525
526
+ **Printing to a file**
527
+ >>> import io
528
+ >>> output = io.StringIO()
529
+ >>> hprint_tree(root, file=output)
530
+ >>> string = output.getvalue()
531
+ >>> print(string)
532
+ ┌─ d
533
+ ┌─ b ─┤
534
+ ─ a ─┤ └─ e
535
+ └─ c
536
+
507
537
Args:
508
538
tree (Node): tree to print
509
539
node_name_or_path (str): node to print from, becomes the root node of printing
@@ -518,7 +548,7 @@ def hprint_tree(
518
548
max_depth = max_depth ,
519
549
style = style ,
520
550
)
521
- print ("\n " .join (result ))
551
+ print ("\n " .join (result ), ** print_kwargs )
522
552
523
553
524
554
def hyield_tree (
0 commit comments