XanaduAI / MrMustard

A differentiable bridge between phase space and Fock space
https://mrmustard.readthedocs.io/
Apache License 2.0
78 stars 27 forks source link

Define multiple output methods for lab_dev #509

Open aplund opened 1 month ago

aplund commented 1 month ago

Before posting a feature request

Feature details

Currently in ipython, lab_dev objects do not display any useful information in the output. I need to manually call repr to see something useful.

Python 3.11.9 (main, Jul 24 2024, 15:03:44) [GCC 14.1.1 20240522]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.28.0 -- An enhanced Interactive Python. Type '?' for help.

[ins] In [1]: %load playground/display.py

[ins] In [2]: # %load playground/display.py
         ...: import mrmustard as mm
         ...: import mrmustard.lab_dev
         ...:
         ...: c = mm.lab_dev.Coherent([0], 1)
         ...:

[ins] In [3]: c
Out[3]: HBox(children=(VBox(children=(HTML(value='\n<style>\ntd {\n  border: 1px solid;\n}\nth {\n  border: 1px solid;…

Implementation

It would appear that this occurs because there is the _ipython_display_ method defined on the objects. This is described in the ipython code as an "escape-hatch" formatter. After IPython 6.1 the _repr_*_ methods are described as the way to get formatted representations of objects. Perhaps this is the way to do this.

How important would you say this feature is?

1: Not important. Would be nice to have.

Additional information

No response

timmysilv commented 1 month ago

To add a bit of context here: I chose to use _ipython_display_ because _repr_*_ requires that you return the required data (eg. raw javascript/html) as opposed to using your own side effect (eg. IPython.display like we use) as described in the ipython docs. I remember that plotly was having trouble when I tried using _repr_html_ and returning the HTML from the widget, likely because it needed some javascript stuff that wasn't installed/supported. I didn't dig too deep, so it might be doable, but Plotly definitely added pain.

aplund commented 1 month ago

Hmm... this would seem to be more complex than it seems due to the way plotly works. Does plotly give equally weird results when used interactively in the terminal interface?

timmysilv commented 3 weeks ago

i can't use much from the terminal because i do all my work on a remote machine so i'm not sure. but probably