Skip to content

tree: add render_node arg to __print_backend - #227

Open
rix1 wants to merge 3 commits into
caesar0301:masterfrom
otovo:rix1/add-render-node
Open

tree: add render_node arg to __print_backend#227
rix1 wants to merge 3 commits into
caesar0301:masterfrom
otovo:rix1/add-render-node

Conversation

@rix1

@rix1 rix1 commented May 8, 2024

Copy link
Copy Markdown

This let us do inversion of control, passing in a callback function that gives more flexibility into how the node is rendered when using show() and save2file(). Essentially replaces the need for data_property - but we should probably keep it for backwards compatibility.

The callback function right now expect a string to be returned. I was thinking on maybe supporting None which could also replace the need for the filter argument: Return None to avoid the Node to be rendered. Let me know what you think @liamlundy or @caesar0301 :)

Example:

def render_node(node):
  if node.is_leaf():
    return node.my_property
  return node.some_other_property

tree.show(render_node=render_node)

See unit test in 626f439 for an actual example.

Checklist

  • I have formatted my code with Black I pass the local validation with scripts/flake8.sh.
  • I have updated docs: Please point me in the right direction here - I've added a docstring, but I'm not sure what/how/where to document the updated show() API.

This let us do inversion of control[1], passing in a callback function
that gives more flexibility into how the node is rendered when using
show() and save2file().

**Example:**

```py
def render_node(node):
  if node.level > 2:
    return node.my_property
  return node.some_other_property

tree.show(render_node=render_node)
```

[1]: https://en.wikipedia.org/wiki/Inversion_of_control
@rix1

rix1 commented May 8, 2024

Copy link
Copy Markdown
Author

Just added some unit tests.

@rix1
rix1 force-pushed the rix1/add-render-node branch from 626f439 to 5a3df96 Compare May 8, 2024 22:39
@BaxHugh

BaxHugh commented Feb 27, 2025

Copy link
Copy Markdown

I'd find this useful. I see this was created almost a year ago, any updates on getting this merged and released?
@caesar0301, @liamlundy @rix1

@caesar0301

Copy link
Copy Markdown
Owner

I'd find this useful. I see this was created almost a year ago, any updates on getting this merged and released? @caesar0301, @liamlundy @rix1

Same to me, I will spend time to fix the conflicts first, and add more uts.

@rix1

rix1 commented Jun 2, 2025

Copy link
Copy Markdown
Author

I'd find this useful. I see this was created almost a year ago, any updates on getting this merged and released? @caesar0301, @liamlundy @rix1

Same to me, I will spend time to fix the conflicts first, and add more uts.

Let me know if there's anything I can do to help :)

Happy to rebase and fix conflicts

@frukto

frukto commented Aug 27, 2025

Copy link
Copy Markdown

This would indeed be very helpful, @rix1 can you rebase again. Maybe it gets merged this time 🤷

@liamlundy

Copy link
Copy Markdown
Collaborator

@rix1 just rebase and have the tests pass and it can be merged

@rix1

rix1 commented Jul 13, 2026

Copy link
Copy Markdown
Author

@rix1 just rebase and have the tests pass and it can be merged

Fantastic! Thanks 🙏 What should we do about 1) the docs, and 1.5) if you agree on the deprecation of the now competing APIs, how do you want to do this?

Just let me know if you want to increase the API surface or simplify by marking filter and/or data_property as deprecated, and I can make an attempt at updating the docs.

@liamlundy

Copy link
Copy Markdown
Collaborator

This looks good! Just a few quick requests and let's get this merged.

  1. Can you rename the argument to node_renderer instead of render_node?
  2. Let's not deprecate anything yet but I will likely deprecate idhidden and data_property before the next release
  3. The API documentation is sufficient but feel free to add examples if you are inclined

@rix1 just rebase and have the tests pass and it can be merged

Fantastic! Thanks 🙏 What should we do about 1) the docs, and 1.5) if you agree on the deprecation of the now competing APIs, how do you want to do this?

Just let me know if you want to increase the API surface or simplify by marking filter and/or data_property as deprecated, and I can make an attempt at updating the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants