admin管理员组文章数量:1400134
IPython/Jupyter has built-in functionality to visualise JSON structures as an interactive tree where you can expand and collapse nodes. I like using it for lists and dictionaries, but now I want to use it for visualising the content of dataclasses also:
import IPython
from dataclasses import dataclass
import dataclasses
@dataclass
class TestDC:
id: str
val: int
_ipy = IPython.get_ipython()
_json_formatter = _ipy.display_formatter.formatters["application/json"]
_json_formatter.for_type(dict, lambda obj: obj)
_json_formatter.for_type(list, lambda obj: obj)
_json_formatter.for_type(TestDC, lambda obj: dataclasses.asdict(obj))
dc1 = TestDC('a', 1)
dc2 = TestDC('b', 2)
In Jupyter-lab, this works fine when visualising the content of a single TestDC
object,
but when I try to visualise the content of a list of TestDC
objects, like [dc1, dc2]
, an error occurs: ValueError: Can't clean for JSON: TestDC(id='a', val=1)
.
I understand that there's no automatic JSON serialisation of dataclasses, but I don't understand why the built-in visualisation functionality is able to process/visualise the content of a single TestDC
object and also nested list and dictionary objects, but runs into troubles when processing a list of objects, for which it knows how to visualise them individually...
What am I missing here?
本文标签: pythonJupyterlab How to use builtin JSON visualization functionality for a dataclassStack Overflow
版权声明:本文标题:pythonJupyter-lab: How to use built-in JSON visualization functionality for a dataclass - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744196258a2594754.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论