From b6eba1e0f4a99459f422f909a96c87a1b0cacbab Mon Sep 17 00:00:00 2001 From: Karolina Cynk Date: Thu, 27 Jun 2024 15:02:22 +0200 Subject: [PATCH] Checking if a chart is closed before rendering html/json (#163) --- .../document_exporting/element/chart.py | 20 +++++++++++++++---- qf_lib/plotting/charts/chart.py | 2 ++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/qf_lib/documents_utils/document_exporting/element/chart.py b/qf_lib/documents_utils/document_exporting/element/chart.py index cb4089b8..ab7a6996 100644 --- a/qf_lib/documents_utils/document_exporting/element/chart.py +++ b/qf_lib/documents_utils/document_exporting/element/chart.py @@ -89,6 +89,12 @@ def generate_json(self) -> str: ------- A string with the base64 image (with encoding prefix) of the chart. """ + if self._chart.closed: + error_message = 'Chart generation error: The chart you are trying to generate has been already closed. ' \ + 'Check if you are not trying to regenerate the json for an already processed chart.' + self.logger.warning(error_message) + return error_message + try: result = "data:image/png;base64," + self._chart.render_as_base64_image( self.figsize, self.dpi, self.optimise) @@ -106,6 +112,13 @@ def generate_html(self, document: Optional[Document] = None) -> str: Generates the HTML necessary to display the underlying chart in a PDF document. The chart is rendered in memory, then encoded to base64 and embedded in the HTML """ + if self._chart.closed: + self.logger.warning('Chart generation error: The chart you are trying to generate has been already closed. ' + 'Check if you are not trying to regenerate the html for an already processed chart.') + result = "

Failed to render chart

" + result += self._create_html_comment() + return result + try: base64 = self._chart.render_as_base64_image(self.figsize, self.dpi, self.optimise, **self.savefig_settings) @@ -125,8 +138,7 @@ def generate_html(self, document: Optional[Document] = None) -> str: except Exception as ex: error_message = "{}\n{}".format(ex.__class__.__name__, traceback.format_exc()) - self.logger.exception('Chart generation error:') - self.logger.exception(error_message) + self.logger.exception(f'Chart generation error: {error_message}') result = "

Failed to render chart

" # Close the chart's figure as we are no longer going to be using it. if self._chart is not None: @@ -138,7 +150,7 @@ def generate_html(self, document: Optional[Document] = None) -> str: def _create_html_comment(self): template = Template(""" -

{{ comment }}

- """) +

{{ comment }}

+ """) return template.render(comment=self.comment) diff --git a/qf_lib/plotting/charts/chart.py b/qf_lib/plotting/charts/chart.py index 11c8ebbe..5deb01ff 100644 --- a/qf_lib/plotting/charts/chart.py +++ b/qf_lib/plotting/charts/chart.py @@ -95,6 +95,7 @@ def __init__(self, start_x: Any = None, end_x: Any = None, upper_y: Any = None, Determines where secondary axes should be created. When Vertical, a vertical axes is created using ``twinx``, otherwise a horizontal axes is created using ``twiny``. """ + self.closed = False @property def axes(self): @@ -179,6 +180,7 @@ def close(self): """ Closes the window containing the figure. """ + self.closed = True plt.close(self.figure) def set_x_range(self, start_x=None, end_x=None):