caesar0301/treelib

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

mariomuellergermany opened this issue · 3 comments

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

I had the same problem, it is since this change: 8cd2940 . 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:

treelib/treelib/tree.py

Lines 931 to 934 in 7e1daf1

if stdout:
print(self._reader.encode("utf-8"))
else:
return self._reader

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.

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