BAMWelDX / weldx

The welding data exchange format
https://www.bam.de/weldx
BSD 3-Clause "New" or "Revised" License
19 stars 9 forks source link

[docs] WeldxFile output in notebooks renders to IPython.display.JSON in jupyterhub environment #846

Closed marscher closed 1 year ago

marscher commented 1 year ago

This can only be shown in a Jupyter lab environment. So the selection of the output should be made aware, that it is contained in a Sphinx/non-interactive session in order to avoid this.

image

CagtayFabry commented 1 year ago

I think this is twofold:

first, this https://github.com/BAMWelDX/weldx/blob/ac3eda4a4ef015e4d34767a0b7db2f3c9e22182a/weldx/asdf/file.py#L918-L922 should really be

   def _ipython_display_(self): 
       # this will be called in Jupyter Lab, but not in a plain notebook. 
       from IPython.core.display import display 

       display(self.header(use_widgets=True, _interactive=_is_jupyterlab_session())) 

unfortunately, the current _is_jupyterlab_session() doesn't really work (reliably), at least not on the JupyterHub install

I haven't really found a nice way to implement _is_jupyterlab_session() correctly

marscher commented 1 year ago

I think there is no correct way to implement this jlab session detection reliably, because in theory there can be arbitrary frontends (historically). Of course right now there is only jlab or jupyter notebook (the later has multiple guis).

We could monkey patch the method for Sphinx to force _interactive to be false. Or set an environment variable within the Sphinx build which can the be checked in the implementation. Both solutions are not elegant, but they'd do the job.

CagtayFabry commented 1 year ago

I like the monkey patching approach you mentioned, that shouldn't be to difficult I hope?

marscher commented 1 year ago

It is not possible to patch this via the Sphinx config, because the kernel manager involved in executing the notebooks is a separate process which does not ever import this config.

CagtayFabry commented 1 year ago

The current implementation also renders the YAML html output instead of the JupyterLab JSON implementation on the JupyterHub for me @marscher

But I think it is fine to leave it for now

marscher commented 1 year ago

On Windows? On Linux I gave it a shot and it used the widgets correctly.

CagtayFabry commented 1 year ago

On Windows? On Linux I gave it a shot and it used the widgets correctly.

Interesting, I was logging into the Lab-JupyterHub from Windows for testing

marscher commented 1 year ago

The heuristic checking if a jupyter lab server is involved just inspects the names of the parent processes. If the littlest jupyter hub server has a different naming pattern than the "normal" console based jupyter lab startup, this won't work.