Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tree: add render_node arg to __print_backend #227

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

rix1
Copy link

@rix1 rix1 commented May 8, 2024

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
Copy link
Author

rix1 commented May 8, 2024

Just added some unit tests.

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.

1 participant