# jupyter: "%display latex" completely breaks @interact (and all of ipywidgets)

Reported by: Owned by: William Stein major sage-duplicate/invalid/wontfix notebook Dima Pasechnik, Kwankyu Lee, Enrique Artal Bartolo, Joshua Campbell, Samuel Lelièvre, Volker Braun Kwankyu Lee N/A #31536

In sage-9.1, this works fine with Jupyter:

%display latex

then in another cell

@interact
def f(n=10):
print(n)


However, in sage-9.2, the output of Interact is displayed as latex, i.e., somebody changed %display latex in such a way that it also breaks widgets.

𝙸𝚗𝚝𝚎𝚛𝚊𝚌𝚝𝚒𝚟𝚎𝚏𝚞𝚗𝚌𝚝𝚒𝚘𝚗<𝚏𝚞𝚗𝚌𝚝𝚒𝚘𝚗𝚏𝚊𝚝𝟶𝚡𝟽𝚏𝚏𝟹𝚊𝚊𝟸𝟿𝚋𝚏𝟽𝟶>𝚠𝚒𝚝𝚑𝟷𝚠𝚒𝚍𝚐𝚎𝚝𝚗:𝙸𝚗𝚝𝚂𝚕𝚒𝚍𝚎𝚛(𝚟𝚊𝚕𝚞𝚎=𝟷𝟶,𝚍𝚎𝚜𝚌𝚛𝚒𝚙𝚝𝚒𝚘𝚗='𝚗',𝚖𝚊𝚡=𝟹𝟶,𝚖𝚒𝚗=⎯𝟷𝟶)


This is a really serious bug, since not only is @interact broken, in fact all of ipywidgets stops working the second you do "%display latex".

### Changed 21 months ago by William Stein

Attachment: Screen Shot 2021-03-18 at 11.22.11 AM.png​ added

Screenshot illustrating this bug.

### comment:1 Changed 21 months ago by William Stein

Description: modified (diff) jupyter: "%display latex" completely breaks @interact → jupyter: "%display latex" completely breaks @interact (and all of ipywidgets)

### comment:2 Changed 21 months ago by Matthias Köppe

Cc: Dima Pasechnik added major → blocker

### comment:4 Changed 21 months ago by Kwankyu Lee

Perhaps introduced by ipython upgrade in #28197?

### comment:5 Changed 21 months ago by Matthias Köppe

Cc: Enrique Artal Bartolo Joshua Campbell Samuel Lelièvre added

### comment:6 Changed 21 months ago by Eric Gourgoulhon

FWIW, %display latex also breaks %matplotlib notebook. For instance the following cell in a Jupyter notebook

%display latex
%matplotlib notebook
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()


yields

<𝙸𝙿𝚢𝚝𝚑𝚘𝚗.𝚌𝚘𝚛𝚎.𝚍𝚒𝚜𝚙𝚕𝚊𝚢.𝙹𝚊𝚟𝚊𝚜𝚌𝚛𝚒𝚙𝚝𝚘𝚋𝚓𝚎𝚌𝚝>
<𝙸𝙿𝚢𝚝𝚑𝚘𝚗.𝚌𝚘𝚛𝚎.𝚍𝚒𝚜𝚙𝚕𝚊𝚢.𝙷𝚃𝙼𝙻𝚘𝚋𝚓𝚎𝚌𝚝>


instead of the pyplot window. If one suppresses %display latex or replaces by %display plain, then everything is fine: the interactive pyplot window appears, with the requested plot.

Contrary to the issue with @interact, this one is also there in Sage 9.1, except that the output is only

<𝙸𝙿𝚢𝚝𝚑𝚘𝚗.𝚌𝚘𝚛𝚎.𝚍𝚒𝚜𝚙𝚕𝚊𝚢.𝙷𝚃𝙼𝙻𝚘𝚋𝚓𝚎𝚌𝚝>

Version 0, edited 21 months ago by Eric Gourgoulhon (next)

Here is the bug.

• ## src/sage/repl/rich_output/backend_ipython.py

diff --git a/src/sage/repl/rich_output/backend_ipython.py b/src/sage/repl/rich_output/backend_ipython.py
index a2c17e2841..38ae60ce70 100644
 a class BackendIPythonNotebook(BackendIPython): return ({u'text/plain': rich_output.unicode_art.get_unicode()}, {}) elif isinstance(rich_output, OutputLatex): return ({u'text/html':  rich_output.mathjax(), u'text/latex': rich_output.inline_equation(), u'text/plain': plain_text.text.get_unicode(), }, {}) elif isinstance(rich_output, OutputHtml):

### comment:8 in reply to:  7 ; follow-up:  9 Changed 21 months ago by Eric Gourgoulhon

Here is the bug.

Shall we revert #23330? It looks more important to have ipywidgets working than a correct pdf export.

### comment:9 in reply to:  8 ; follow-up:  11 Changed 21 months ago by Kwankyu Lee

Here is the bug.

Shall we revert #23330? It looks more important to have ipywidgets working than a correct pdf export.

Moreover, in the spirit of #31536, tex/latex mime type is to be deprecated in favor of tex/html.

### comment:10 Changed 21 months ago by Eric Gourgoulhon

BTW, this points to the need to move to the default Jupyter rich output display for latex (i.e. use _repr_latex_ instead of %display latex), since the pdf export works out of the box with _repr_latex_ and there is no interference with ipywidgets.

### comment:11 in reply to:  9 ; follow-up:  13 Changed 21 months ago by Eric Gourgoulhon

Here is the bug.

Shall we revert #23330? It looks more important to have ipywidgets working than a correct pdf export.

Moreover, in the spirit of #31536, tex/latex mime type is to be deprecated in favor of tex/html.

But then, there cannot be pdf export of latex-typeset formulas from text/html.

### comment:12 follow-up:  14 Changed 20 months ago by Eric Gourgoulhon

Authors: → Eric Gourgoulhon → public/trac31513_display_latex_ipywidgets → 046892eee8198c4112c45bf52f96131d8be9a40f new → needs_review

Here is a possible fix, which does not revert the pdf export fix introduced in #23330. I have instead modified sage.repl.display.formatter.SageDisplayFormatter.format so that IPython widgets are tried before Sage rich output. I've performed a few tests and things seem OK, but I am not completly sure about possible side effects of this fix. So please test it on your side.

New commits:

 ​046892e Change the order of IPython widgets and Sage rich output in SageDisplayFormatter.format

### comment:13 in reply to:  11 Changed 20 months ago by Kwankyu Lee

Moreover, in the spirit of #31536, tex/latex mime type is to be deprecated in favor of tex/html.

But then, there cannot be pdf export of latex-typeset formulas from text/html.

Right. tex/latex is the right mime type for pdf output. What I don't like is the use of latex for mathjax+html. I misdirected my arrow.

### comment:14 in reply to:  12 ; follow-up:  15 Changed 20 months ago by Kwankyu Lee

Here is a possible fix, which does not revert the pdf export fix introduced in #23330. I have instead modified sage.repl.display.formatter.SageDisplayFormatter.format so that IPython widgets are tried before Sage rich output. I've performed a few tests and things seem OK, but I am not completly sure about possible side effects of this fix. So please test it on your side.

Then you bypass the test set(sage_format.keys()).issubset(self.default_mime()) checked before calling self.ipython_display_formatter(obj). I guess the author (Volker?) of the test thought the test is necessary before resorting to ipython for output.

Do you understand the logic of the test? I don't. If you do, would you explain? As it is practically impossible to check all side effects of the change, I think we need to understand the logic correctly and be sure it is safe to bypass the test.

### comment:15 in reply to:  14 Changed 20 months ago by Eric Gourgoulhon

Then you bypass the test set(sage_format.keys()).issubset(self.default_mime()) checked before calling self.ipython_display_formatter(obj). I guess the author (Volker?) of the test thought the test is necessary before resorting to ipython for output.

Do you understand the logic of the test?

No. I have to look deeper. Certainly Volker should check this.

### comment:16 follow-up:  17 Changed 20 months ago by Kwankyu Lee

Now #31536 also contains fixes both for jupyter pdf export problem and this ticket.

Hope for no unexpected side effects..

Last edited 20 months ago by Kwankyu Lee (previous) (diff)

### comment:17 in reply to:  16 Changed 20 months ago by Eric Gourgoulhon

Now #31536 also contains fixes both for jupyter pdf export problem and this ticket.

This sounds nice! I'll look at it today.

### comment:18 follow-up:  19 Changed 20 months ago by Volker Braun

Morally thats wrong, we should prefer our own formatter which gives e.g. the emacs backend a chance of handling things correctly. The problem is that ipython/jupyter doesn't have a blessed way of recognizing interact output, and moreover keeps adding more magic methods over time. The proposed patch is reasonable to me, hopefully the jupyter guys can keep ipython_display_formatter sufficently specific that only output is captured that is 100% ipython specific (like the current @interacts which cannot work anywhere else)

Note that I didn't actually check this ticket for regressions.

### comment:19 in reply to:  18 Changed 20 months ago by Eric Gourgoulhon

Morally thats wrong, we should prefer our own formatter which gives e.g. the emacs backend a chance of handling things correctly. The problem is that ipython/jupyter doesn't have a blessed way of recognizing interact output, and moreover keeps adding more magic methods over time. The proposed patch is reasonable to me, hopefully the jupyter guys can keep ipython_display_formatter sufficently specific that only output is captured that is 100% ipython specific (like the current @interacts which cannot work anywhere else)

### comment:20 Changed 20 months ago by Eric Gourgoulhon

Status: needs_review → needs_info

#31536 seems in a good shape and would be a better fix for the issue reported here.

### comment:21 Changed 20 months ago by Eric Gourgoulhon

Authors: Eric Gourgoulhon public/trac31513_display_latex_ipywidgets 046892eee8198c4112c45bf52f96131d8be9a40f sage-9.3 → sage-duplicate/invalid/wontfix needs_info → needs_review

Now that #31536 is ready, we should use it to fix the issue reported here. In particular, #31536 complies with the moral behaviour expressed in comment:18.

### comment:22 Changed 20 months ago by Matthias Köppe

Dependencies: → #31536 blocker → major

### comment:23 Changed 20 months ago by Kwankyu Lee

Status: needs_review → positive_review

fixed in Sage 9.3.rc2

### comment:24 Changed 19 months ago by Samuel Lelièvre

Resolution: → fixed → Kwankyu Lee positive_review → closed
Note: See TracTickets for help on using tickets.