--- title: Executable code snippets in docs and HTML object representations layout: post excerpt: "It just looks so nice." assets: /assets/blog/executable-code-snippets-in-docs-and-html-object-representations thumbnail: /assets/blog/executable-code-snippets-in-docs-and-html-object-representations/thumbnail.png social_image: /assets/blog/executable-code-snippets-in-docs-and-html-object-representations/thumbnail.png alt: | A screenshot of a tree diagram created using unicode bar characters. It starts at 'root' and then fans out to 'class=od' and 'class=rd' which themselves fan out to 'expver=0001' and 'expver=0002' and finally 'param=1' and 'param=2'. image_class: invertable --- Just a quick one. Lately I've started writing the documention for [a new software project involving trees](https://qubed.readthedocs.io). While debugging that in a Jupyter notebook I made a small HTML representation of the tree that mimis the output of `tree` but using the HTML details tag so you can open and close the subtrees. This works using by giving the object a "_repr_html_" method that returns a HTML string. If it's present, Jupyter notebooks will use the output of that instead of `repr` to display a rich version of the object in a notebook. ```python class Title(): def __init__(self, x): self.x = x def _repr_html_(self): return f"
root
├── class=od
│ ├── expver=0001
│ │ ├── param=1│ │ └── param=2│ └── expver=0002
│ ├── param=1│ └── param=2└── class=rd
├── expver=0001
│ ├── param=1 │ ├── param=2 │ └── param=3└── expver=0002
├── param=1 └── param=2