Skip to content

Commit

Permalink
Fallback to the string printer if the latex printer raises an exception
Browse files Browse the repository at this point in the history
Currently it just prints an error and gives an empty png. However, there is a
lot of LaTeX that matplotlib cannot parse, e.g., matrices. This gives a better
user experience for SymPy, which has a text printer that it can fallback
to. SymPy cannot just disable the LaTeX printer because it is desired for the
notebook, and there is no way for it to enable LaTeX just in the notebook, or
change the printer precedence order.

I have left in the debug logging of the error (to the terminal).

Fixes jupyter#63. This supersedes jupyter#57, although I don't know if it fixes the issue
there.
  • Loading branch information
asmeurer committed Oct 23, 2015
1 parent 19a8fb5 commit 047f8cd
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions qtconsole/rich_jupyter_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ def _handle_execute_result(self, msg):
self._append_html(self.output_sep2, True)
elif 'text/latex' in data:
self._pre_image_append(msg, prompt_number)
self._append_latex(data['text/latex'], True)
try:
self._append_latex(data['text/latex'], True)
except Exception as e:
self.log.error("Failed to render latex: '%s'", data['text/latex'], exc_info=True)
self.log.error("Failed to render latex: %s" % e)
return super(RichJupyterWidget, self)._handle_execute_result(msg)
self._append_html(self.output_sep2, True)
else:
# Default back to the plain text representation.
Expand Down Expand Up @@ -166,7 +171,12 @@ def _handle_display_data(self, msg):
jpg = decodestring(data['image/jpeg'].encode('ascii'))
self._append_jpg(jpg, True, metadata=metadata.get('image/jpeg', None))
elif 'text/latex' in data and latex_to_png:
self._append_latex(data['text/latex'], True)
try:
self._append_latex(data['text/latex'], True)
self.log.error("Failed to render latex: '%s'", data['text/latex'], exc_info=True)
self.log.error("Failed to render latex: %s" % e)
except Exception as e:
return super(RichJupyterWidget, self)._handle_display_data(msg)
else:
# Default back to the plain text representation.
return super(RichJupyterWidget, self)._handle_display_data(msg)
Expand All @@ -177,13 +187,7 @@ def _handle_display_data(self, msg):

def _append_latex(self, latex, before_prompt=False, metadata=None):
""" Append latex data to the widget."""
try:
png = latex_to_png(latex, wrap=False)
except Exception as e:
self.log.error("Failed to render latex: '%s'", latex, exc_info=True)
self._append_plain_text("Failed to render latex: %s" % e, before_prompt)
else:
self._append_png(png, before_prompt, metadata)
self._append_png(latex_to_png(latex, wrap=False), before_prompt, metadata)

def _append_jpg(self, jpg, before_prompt=False, metadata=None):
""" Append raw JPG data to the widget."""
Expand Down Expand Up @@ -365,4 +369,3 @@ class RichIPythonWidget(RichJupyterWidget):
def __init__(self, *a, **kw):
warn("RichIPythonWidget is deprecated, use RichJupyterWidget")
super(RichIPythonWidget, self).__init__(*a, **kw)

0 comments on commit 047f8cd

Please sign in to comment.