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 = "
{{ 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):