Search code examples
pythonjupyter-notebookipythonjupyter-lab

How can I debug IPython display methods?


I'm writing _repr_latex_, _repr_html_, __repr__ methods.

The problem that I'm facing is that I want to debug the call chain, because I bumped in to a situation like this.

class A:
    def _get_text(self):
        return self.__class__.__name__

    def _repr_html_(self):
        text = self._get_text()
        return f"<b>{text}</b>"

class B(A):
    def __repr__(self):
        return f"<{self.__class__.__name__} >"

class C(B):
    def _get_text(self):
        return "I'm C, an equivalent data structure with different meaning."

And whenever I try to display by stating an object c = C at the end of a code block, it displays the __repr__ not the _repr_html_, corresponding to the class C.

If anyone knows what is a common debugging process for this situation?


Solution

  • After examining all the methods, mentioned, I realized that there was an exception occurring in one of the methods, forcing a fallback to __repr__.

    The conclusion is to make sure that the _repr_{type}_ doesn't raise exceptions.