hgrecco / pint

Operate and manipulate physical quantities in Python
http://pint.readthedocs.org/
Other
2.4k stars 473 forks source link

Extremely slow display of array-quantities with large shapes in Jupyter #1583

Open SimonHeybrock opened 2 years ago

SimonHeybrock commented 2 years ago

Try this in Jupyter (jupyterlab 3.4.6)

import numpy as np
import pint
q = pint.Quantity(np.ones(shape=(10000,1000)), 'm')
q

This takes very long, and the time scales with the array size. Then I tried:

from IPython.display import HTML
display(HTML(q._repr_html_()))

and this is fast (instant)! So what is going on? I noticed that Quantity also defines _repr_latex_ and indeed calling it takes a very long time. Is JupyterLab calling _repr_latex_ even if it is going to display HTML?

Edit:

Confirmed that it does:

class Obj:
    def _repr_html_(self):
        print('html')
        return 'abc'
    def _repr_latex_(self):
        print('latex')
        return 'abc'

o = Obj()
o

I could not figure out if this is a Jupyter/IPython feature or bug (no luck finding a related issue yet), but according to https://github.com/ipython/ipython/issues/11202#issuecomment-399698470 it is a feature?

hgrecco commented 2 years ago

This was discussed here: https://github.com/hgrecco/pint/issues/765 and https://nbviewer.org/urls/dl.dropbox.com/s/jlgqm6s92kzvagi/pint_html_repr_demo.ipynb

I was under the impression that this was not an issue any more.