From c7e190dab47875a958ca022ea953e6bed84b238d Mon Sep 17 00:00:00 2001 From: Ian Murray Date: Tue, 28 Feb 2012 20:37:09 +0000 Subject: [PATCH 1/5] Fixed #735. More useful error message in html output --- IPython/frontend/qt/console/rich_ipython_widget.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IPython/frontend/qt/console/rich_ipython_widget.py b/IPython/frontend/qt/console/rich_ipython_widget.py index 9570f0180c4..18edd496fad 100644 --- a/IPython/frontend/qt/console/rich_ipython_widget.py +++ b/IPython/frontend/qt/console/rich_ipython_widget.py @@ -231,7 +231,7 @@ def _get_image_tag(self, match, path = None, format = "png"): try: svg = str(self._name_to_svg_map[match.group("name")]) except KeyError: - return "Couldn't find image %s" % match.group("name") + return "Cannot convert a PNG to SVG. To fix this, add this to your config: c.InlineBackendConfig.figure_format = 'svg'" # Not currently checking path, because it's tricky to find a # cross-browser way to embed external SVG images (e.g., via From d8bbd71f0d143d1a3ca0a0bbdee7864df8a10a40 Mon Sep 17 00:00:00 2001 From: Ian Murray Date: Tue, 28 Feb 2012 20:44:19 +0000 Subject: [PATCH 2/5] Added a warning dialog box when attempting to convert a PNG to SVG (#735) --- IPython/frontend/qt/console/rich_ipython_widget.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/IPython/frontend/qt/console/rich_ipython_widget.py b/IPython/frontend/qt/console/rich_ipython_widget.py index 18edd496fad..1aec78ceb2d 100644 --- a/IPython/frontend/qt/console/rich_ipython_widget.py +++ b/IPython/frontend/qt/console/rich_ipython_widget.py @@ -231,6 +231,10 @@ def _get_image_tag(self, match, path = None, format = "png"): try: svg = str(self._name_to_svg_map[match.group("name")]) except KeyError: + QtGui.QMessageBox.warning(self, 'Error converting PNG to SVG.', + 'Cannot convert a PNG to SVG. To fix this, add this to your ipython config:\n' + 'c.InlineBackendConfig.figure_format = \'svg\'', + QtGui.QMessageBox.Ok) return "Cannot convert a PNG to SVG. To fix this, add this to your config: c.InlineBackendConfig.figure_format = 'svg'" # Not currently checking path, because it's tricky to find a From 984ff3680c2166490f29c33ae4611d4888cf5c22 Mon Sep 17 00:00:00 2001 From: Ian Murray Date: Wed, 14 Mar 2012 22:24:13 +0000 Subject: [PATCH 3/5] Improve warning about exporting SVG - Improved error message by instructing to regenerate the figure. - Only display warning message once per figure per export attempt. --- .../qt/console/rich_ipython_widget.py | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/IPython/frontend/qt/console/rich_ipython_widget.py b/IPython/frontend/qt/console/rich_ipython_widget.py index 1aec78ceb2d..b6fbebdb8c4 100644 --- a/IPython/frontend/qt/console/rich_ipython_widget.py +++ b/IPython/frontend/qt/console/rich_ipython_widget.py @@ -231,11 +231,25 @@ def _get_image_tag(self, match, path = None, format = "png"): try: svg = str(self._name_to_svg_map[match.group("name")]) except KeyError: - QtGui.QMessageBox.warning(self, 'Error converting PNG to SVG.', - 'Cannot convert a PNG to SVG. To fix this, add this to your ipython config:\n' - 'c.InlineBackendConfig.figure_format = \'svg\'', - QtGui.QMessageBox.Ok) - return "Cannot convert a PNG to SVG. To fix this, add this to your config: c.InlineBackendConfig.figure_format = 'svg'" + + # Determine whether this is a new export by checking whether + # we've attempted to export this svg previously. If so, then + # reset the flag that determines whether the warning has been + # displayed or not. + if match.group("name") in getattr(self, '_svgs_processed', set()): + self._svg_warning_displayed = False + + # Display this warning exactly once per export attempt. + if not getattr(self, '_svg_warning_displayed', False): + QtGui.QMessageBox.warning(self, 'Error converting PNG to SVG.', + 'Cannot convert a PNG to SVG. To fix this, add this to your ipython config:\n\n' + '\tc.InlineBackendConfig.figure_format = \'svg\'\n\n' + 'And regenerate the figures.', + QtGui.QMessageBox.Ok) + self._svg_warning_displayed = True + self._svgs_processed = set() + self._svgs_processed.add(match.group("name")) + return "Cannot convert a PNG to SVG. To fix this, add this to your config: c.InlineBackendConfig.figure_format = 'svg' and regenerate the figures." # Not currently checking path, because it's tricky to find a # cross-browser way to embed external SVG images (e.g., via From 335f6b126534b8ae3e89f41432d9d0ebf3040b4b Mon Sep 17 00:00:00 2001 From: Ian Murray Date: Mon, 23 Apr 2012 23:51:13 +0100 Subject: [PATCH 4/5] Simplified the reset of the _svg_warning_displayed flag in RichIPythonWidget By overriding export_html(), and reseting the flag prior to the html export. This avoids repeated messages for a single document save, but repeats the message for subsequent saves, including in new tabs and across kernel restarts. --- .../qt/console/rich_ipython_widget.py | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/IPython/frontend/qt/console/rich_ipython_widget.py b/IPython/frontend/qt/console/rich_ipython_widget.py index b6fbebdb8c4..ad95d74895f 100644 --- a/IPython/frontend/qt/console/rich_ipython_widget.py +++ b/IPython/frontend/qt/console/rich_ipython_widget.py @@ -29,6 +29,8 @@ class RichIPythonWidget(IPythonWidget): # RichIPythonWidget protected class variables. _payload_source_plot = 'IPython.zmq.pylab.backend_payload.add_plot_payload' _jpg_supported = Bool(False) + _svg_warning_displayed = False + #--------------------------------------------------------------------------- # 'object' interface #--------------------------------------------------------------------------- @@ -52,6 +54,19 @@ def __init__(self, *args, **kw): self._jpg_supported = 'jpeg' in _supported_format + #--------------------------------------------------------------------------- + # 'ConsoleWidget' public interface overides + #--------------------------------------------------------------------------- + + def export_html(self): + """ Shows a dialog to export HTML/XML in various formats. + + Overridden in order to reset the _svg_warning_displayed flag. + """ + self._svg_warning_displayed = False + super(RichIPythonWidget, self).export_html() + + #--------------------------------------------------------------------------- # 'ConsoleWidget' protected interface #--------------------------------------------------------------------------- @@ -231,24 +246,13 @@ def _get_image_tag(self, match, path = None, format = "png"): try: svg = str(self._name_to_svg_map[match.group("name")]) except KeyError: - - # Determine whether this is a new export by checking whether - # we've attempted to export this svg previously. If so, then - # reset the flag that determines whether the warning has been - # displayed or not. - if match.group("name") in getattr(self, '_svgs_processed', set()): - self._svg_warning_displayed = False - - # Display this warning exactly once per export attempt. - if not getattr(self, '_svg_warning_displayed', False): + if not self._svg_warning_displayed: QtGui.QMessageBox.warning(self, 'Error converting PNG to SVG.', 'Cannot convert a PNG to SVG. To fix this, add this to your ipython config:\n\n' '\tc.InlineBackendConfig.figure_format = \'svg\'\n\n' 'And regenerate the figures.', QtGui.QMessageBox.Ok) self._svg_warning_displayed = True - self._svgs_processed = set() - self._svgs_processed.add(match.group("name")) return "Cannot convert a PNG to SVG. To fix this, add this to your config: c.InlineBackendConfig.figure_format = 'svg' and regenerate the figures." # Not currently checking path, because it's tricky to find a From 78c047ee1c56bd5a51023717d0644eddd07f8d13 Mon Sep 17 00:00:00 2001 From: Ian Murray Date: Tue, 24 Apr 2012 00:07:06 +0100 Subject: [PATCH 5/5] Tidying of warning dialog strings ... and small expansion of related comments --- IPython/frontend/qt/console/rich_ipython_widget.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/IPython/frontend/qt/console/rich_ipython_widget.py b/IPython/frontend/qt/console/rich_ipython_widget.py index ad95d74895f..06172d956cc 100644 --- a/IPython/frontend/qt/console/rich_ipython_widget.py +++ b/IPython/frontend/qt/console/rich_ipython_widget.py @@ -29,6 +29,9 @@ class RichIPythonWidget(IPythonWidget): # RichIPythonWidget protected class variables. _payload_source_plot = 'IPython.zmq.pylab.backend_payload.add_plot_payload' _jpg_supported = Bool(False) + + # Used to determine whether a given html export attempt has already + # displayed a warning about being unable to convert a png to svg. _svg_warning_displayed = False #--------------------------------------------------------------------------- @@ -61,7 +64,8 @@ def __init__(self, *args, **kw): def export_html(self): """ Shows a dialog to export HTML/XML in various formats. - Overridden in order to reset the _svg_warning_displayed flag. + Overridden in order to reset the _svg_warning_displayed flag prior + to the export running. """ self._svg_warning_displayed = False super(RichIPythonWidget, self).export_html() @@ -248,12 +252,16 @@ def _get_image_tag(self, match, path = None, format = "png"): except KeyError: if not self._svg_warning_displayed: QtGui.QMessageBox.warning(self, 'Error converting PNG to SVG.', - 'Cannot convert a PNG to SVG. To fix this, add this to your ipython config:\n\n' + 'Cannot convert a PNG to SVG. To fix this, add this ' + 'to your ipython config:\n\n' '\tc.InlineBackendConfig.figure_format = \'svg\'\n\n' 'And regenerate the figures.', QtGui.QMessageBox.Ok) self._svg_warning_displayed = True - return "Cannot convert a PNG to SVG. To fix this, add this to your config: c.InlineBackendConfig.figure_format = 'svg' and regenerate the figures." + return ("Cannot convert a PNG to SVG. " + "To fix this, add this to your config: " + "c.InlineBackendConfig.figure_format = 'svg' " + "and regenerate the figures.") # Not currently checking path, because it's tricky to find a # cross-browser way to embed external SVG images (e.g., via