joowani / binarytree

Python Library for Studying Binary Trees
http://binarytree.readthedocs.io
MIT License
1.81k stars 171 forks source link

graphviz's `_repr_svg_()` deprecation causes error on jupyter notebook #40

Closed scarf005 closed 2 years ago

scarf005 commented 2 years ago

Summary

while trying to repr tree as image following the example provided on readme, got following error:

AttributeError: 'Digraph' object has no attribute '_repr_svg_'

looks like it's because Digraph._repr_svg_() is deprecated: https://github.com/joowani/binarytree/blob/397e65fa08f463f23983e67ac8db406b8e6671be/binarytree/__init__.py#L512

and is solved by replacing the line with:

return self.graphviz()._repr_image_svg_xml()

(_repr_image_svg_xml() returns str)

Log

text:

from binarytree import tree
t = tree()
t
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/.asdf/installs/python/3.10.1/lib/python3.10/site-packages/IPython/core/formatters.py:343, in BaseFormatter.__call__(self, obj)
    [341](file:///home/scarf/.asdf/installs/python/3.10.1/lib/python3.10/site-packages/IPython/core/formatters.py?line=340)     method = get_real_method(obj, self.print_method)
    [342](file:///home/scarf/.asdf/installs/python/3.10.1/lib/python3.10/site-packages/IPython/core/formatters.py?line=341)     if method is not None:
--> [343](file:///home/scarf/.asdf/installs/python/3.10.1/lib/python3.10/site-packages/IPython/core/formatters.py?line=342)         return method()
    [344](file:///home/scarf/.asdf/installs/python/3.10.1/lib/python3.10/site-packages/IPython/core/formatters.py?line=343)     return None
    [345](file:///home/scarf/.asdf/installs/python/3.10.1/lib/python3.10/site-packages/IPython/core/formatters.py?line=344) else:

File ~/.asdf/installs/python/3.10.1/lib/python3.10/site-packages/binarytree/__init__.py:512, in Node._repr_svg_(self)
    [506](file:///home/scarf/.asdf/installs/python/3.10.1/lib/python3.10/site-packages/binarytree/__init__.py?line=505) """Display the binary tree using Graphviz (used for `Jupyter notebooks`_).
    [507](file:///home/scarf/.asdf/installs/python/3.10.1/lib/python3.10/site-packages/binarytree/__init__.py?line=506) 
    [508](file:///home/scarf/.asdf/installs/python/3.10.1/lib/python3.10/site-packages/binarytree/__init__.py?line=507) .. _Jupyter notebooks: https://jupyter.org
    [509](file:///home/scarf/.asdf/installs/python/3.10.1/lib/python3.10/site-packages/binarytree/__init__.py?line=508) """
    [510](file:///home/scarf/.asdf/installs/python/3.10.1/lib/python3.10/site-packages/binarytree/__init__.py?line=509) try:
    [511](file:///home/scarf/.asdf/installs/python/3.10.1/lib/python3.10/site-packages/binarytree/__init__.py?line=510)     # noinspection PyProtectedMember
--> [512](file:///home/scarf/.asdf/installs/python/3.10.1/lib/python3.10/site-packages/binarytree/__init__.py?line=511)     return str(self.graphviz()._repr_svg_())
    [514](file:///home/scarf/.asdf/installs/python/3.10.1/lib/python3.10/site-packages/binarytree/__init__.py?line=513) except (SubprocessError, ExecutableNotFound, FileNotFoundError):
    [515](file:///home/scarf/.asdf/installs/python/3.10.1/lib/python3.10/site-packages/binarytree/__init__.py?line=514)     return self.svg()

AttributeError: 'Digraph' object has no attribute '_repr_svg_'
Node(3)

screenshot: image

Versions

ubuntu 21.10 python 3.10.1 ipython 8.1.1 binarytree 6.5.1 graphviz 0.20

joowani commented 2 years ago

thank you for the PR @scarf005! Sorry it took so long to merge.