caesar0301 / treelib

An efficient implementation of tree data structure in python 2/3.
http://treelib.readthedocs.io/en/latest/
Other
800 stars 185 forks source link

Treelib not working any longer on Google colab or https://jupyter.org/ #218

Open mariomuellergermany opened 10 months ago

mariomuellergermany commented 10 months ago

Hi,

I was already using the library and really enjoyed. But now it is not working any longer.

Im am using the code:

`%pip install treelib from treelib import Node, Tree tree = Tree() tree.create_node("Harry", "harry") # root node tree.create_node("Jane", "jane", parent="harry") tree.create_node("Bill", "bill", parent="harry") tree.create_node("Diane", "diane", parent="jane") tree.create_node("Mary", "mary", parent="diane") tree.create_node("Mark", "mark", parent="jane") tree.show()

The result is: b'Harry\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Bill\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Jane\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Diane\n \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Mary\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Mark\n'`

Was there an update? Why does it not work any longer?

thanks regards, Mario

realjanpaulus commented 10 months ago

I had the same problem, it is since this change: https://github.com/caesar0301/treelib/commit/8cd2940ebe5749afd4b9e0ebfe2ff2b358c2e42f . Something with encoding is weird here.

My current workaround:

# old, both giving the weird encoding
tree.show()
print(tree.show())

# new
print(tree.show(stdout=False))

This bypasses the forced encoding of UTF-8:

https://github.com/caesar0301/treelib/blob/7e1daf126bceeded2698d583d847bd7fae0880d8/treelib/tree.py#L931-L934

tomol012 commented 9 months ago

Same problem here, although not related Colab or Jupyter in any way. For me it doesn't work in VS Code, Python 3.9.13. I downgraded to 1.6.4 and all is good again.

The workaround from @realjanpaulus is not acceptable to me, because newlines are not displayed in output which means the whole tree is printed as one line, defeating the purpose of the function.

shaongitbd commented 8 months ago

First save tree using tree.save2file("tree.txt") then open it using text editor