#31513 closed defect (fixed)

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

Reported by: William Stein Owned by:
Priority: major Milestone: sage-duplicate/invalid/wontfix
Component: notebook Keywords:
Cc: Dima Pasechnik, Kwankyu Lee, Enrique Artal Bartolo, Joshua Campbell, Samuel Lelièvre, Volker Braun Merged in:
Authors: Reviewers: Kwankyu Lee
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: #31536 Stopgaps:

Status badges

Description (last modified by William Stein)

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".

Attachments (1)

Screen Shot 2021-03-18 at 11.22.11 AM.png​ (143.6 KB) - added by William Stein 21 months ago.
Screenshot illustrating this bug.

Download all attachments as: .zip

Change History (25)

Changed 21 months ago by William Stein

Screenshot illustrating this bug.

comment:1 Changed 21 months ago by William Stein

Description: modified (diff)
Summary: 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
Priority: major β†’ blocker

comment:3 Changed 21 months ago by Matthias KΓΆppe

Cc: Kwankyu Lee added

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)

comment:7 Changed 21 months ago by Kwankyu Lee

Here is the bug.

​https://trac.sagemath.org/ticket/23330

  • 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 b class BackendIPythonNotebook(BackendIPython):Β 
    544544            return ({u'text/plain': rich_output.unicode_art.get_unicode()}, {})
    545545        elif isinstance(rich_output, OutputLatex):
    546546            return ({u'text/html':  rich_output.mathjax(),
    Β 547                     u'text/latex': rich_output.inline_equation(),
    547548                     u'text/plain': plain_text.text.get_unicode(),
    548549            }, {})
    549550        elif isinstance(rich_output, OutputHtml):

comment:8 in reply to: Β 7 ; Changed 21 months ago by Eric Gourgoulhon

Replying to klee:

Here is the bug.

​https://trac.sagemath.org/ticket/23330

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

comment:9 in reply to: Β 8 ; Changed 21 months ago by Kwankyu Lee

Replying to egourgoulhon:

Replying to klee:

Here is the bug.

​https://trac.sagemath.org/ticket/23330

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 ; Changed 21 months ago by Eric Gourgoulhon

Replying to klee:

Replying to egourgoulhon:

Replying to klee:

Here is the bug.

​https://trac.sagemath.org/ticket/23330

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 Changed 20 months ago by Eric Gourgoulhon

Authors: β†’ Eric Gourgoulhon
Branch: β†’ public/trac31513_display_latex_ipywidgets
Commit: β†’ 046892eee8198c4112c45bf52f96131d8be9a40f
Status: 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:

​046892eChange 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

Replying to egourgoulhon:

Replying to klee:

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 ; Changed 20 months ago by Kwankyu Lee

Replying to egourgoulhon:

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

Cc: Volker Braun added

Replying to klee:

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 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

Replying to klee:

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 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

Replying to vbraun:

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)

Thanks for your feedback.

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
Branch: public/trac31513_display_latex_ipywidgets
Commit: 046892eee8198c4112c45bf52f96131d8be9a40f
Milestone: sage-9.3 β†’ sage-duplicate/invalid/wontfix
Status: 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
Priority: 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
Reviewers: β†’ Kwankyu Lee
Status: positive_review β†’ closed
Note: See TracTickets for help on using tickets.