pyiron / pyiron_base

Core components of the pyiron integrated development environment (IDE) for computational materials science
https://pyiron-base.readthedocs.io
BSD 3-Clause "New" or "Revised" License
19 stars 13 forks source link

Rendering DataContainers on an ipython shell #1031

Open jan-janssen opened 1 year ago

jan-janssen commented 1 year ago

I just noticed that only jupyter lab renders the DataContainer class as foldable structure, while the same rendering does not work in jupyter notebooks or the ipython shell. This makes it difficult to work with hierarchical input files, for example the Sphinx DFT code. Example Code:

from pyiron_base import DataContainer
d = DataContainer({"a": 1, "b": 2})
d

For the GenericParameters class this was not an issue:

from pyiron_base import GenericParameters
d = GenericParameters()
d.set_dict({"a": 1, "b": 2})
d

It renders fine in jupyter lab, jupyter notebooks and on an ipython shell.

@samwaseda, @pmrv Any suggestions? Are you using jupyter lab exclusively? Or is there something I am missing, which enables the rendering of the Datacontainer objects?

pmrv commented 1 year ago

Iirc, this is due to the extension that does the json rendering only being available for jupyter lab. Not sure if there's a similar extension for the notebook, since I'm not using it. @niklassiemer made a special GUI for it once, so maybe we should just replace the json hack by it.

samwaseda commented 1 year ago

Same problem posted more than 2 years ago. To be honest I would love to have a simple string representation if the extension that @pmrv is mentioning is not available.

niklassiemer commented 1 year ago

It really heavily depends on how the different systems try to represent the objetcs: JupyterLab used the _repr_json_ method if this is defined, JupyterNotebooks only use the _repr_html_ method (if I recall correctly), while the shell only uses the __repr__ method. You did not post the output for the calls, but I added an _repr_html_ method to the DataContainer (without fancy GUI features) to be able to have a nicer display (of course static and not foldable...). For me the question actually is: What static representation do we want for a hierarchical data structure? This was not a problem for the GenericParameters which only have a flat hierarchy. The current html solution used in the Notebooks should render

dc = DataContainer({"a": 1, "b": 2, 'c': {'A': Any(), 'B': {'b1':  None, 'b2': {'n': 0, 'm': 1, 'o': 2}}}})
dc['d'] = {'some':'dict'}
dc
DataContainer({
  "a": "1",
  "b": "2",
  "c": {
    "A": "<__main__.Any object at 0x7f174ae167c0>",
    "B": {
      "b1": "None",
      "b2": {
        "n": "0",
        "m": "1",
        "o": "2"
      }
    }
  },
  "d": "{'some': 'dict'}"
})

while the plain repr looks like


"DataContainer({'a': 1, 'b': 2, 'c': DataContainer({'A': <__main__.Any object at 0x7f174ae167c0>, 'B': DataContainer({'b1': None, 'b2': DataContainer({'n': 0, 'm': 1, 'o': 2})})}), 'd': {'some': 'dict'}})"```