kayjan / bigtree

Tree Implementation and Methods for Python, integrated with list, dictionary, pandas and polars DataFrame.

Home Page:https://bigtree.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to build a html tree

jet10000 opened this issue · comments

commented

How to build "ul li" or "table" html tree ?

Hi, this is available in yield_tree function where it performs the tree iteration and you can specify any <ul> or <li> to print before/after the nodes. I have included a working example below to print the results to the console.

By using yield_tree, you have a lot of control over what to do with the nodes, such as

  • Printing the node attributes in addition to the node name
  • Print the results with proper HTML indentation
  • Redirecting the output to a text file (instead of printing to the console)

The working example below is just a barebones example of printing the HTML "ul li" component with the node name.

from bigtree import yield_tree

last_depth = 0
for branch, stem, node in yield_tree(tree):
    if node.depth > last_depth:
        last_depth += 1
        print("<ul>")
        print(f"<li>{node.node_name}</li>")
    else:
        while node.depth < last_depth:
            last_depth -= 1
            print("</ul>")
        print(f"<li>{node.node_name}</li>")
while last_depth:
    last_depth -= 1
    print("</ul>")
commented

Thanks, is it possible to convert the <ul> and <li> back to bigtree?

It is a little tricky to read in the HTML raw code, but you can display the HTML structure and use str_to_tree similar to this issue.

commented

I temporarily use html2markdown to convert html tree into markdown tree, and then into bigtree.

Expect to have a built-in method in the future.

Do open a new issue and describe your use case and provide sample input if possible.

However, there are a lot of HTML tags and not all of the tags are related to tree hierarchical structure. If your workaround works, to make use of html2markdown, then duplicated efforts would be redundant in bigtree.