Skip to content

Commit

Permalink
Checking if a chart is closed before rendering html/json (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
myrmarachne authored Jun 27, 2024
1 parent 7439fd7 commit b6eba1e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
20 changes: 16 additions & 4 deletions qf_lib/documents_utils/document_exporting/element/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = "<h2 class='chart-render-failure'>Failed to render chart</h1>"
result += self._create_html_comment()
return result

try:
base64 = self._chart.render_as_base64_image(self.figsize, self.dpi, self.optimise,
**self.savefig_settings)
Expand All @@ -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 = "<h2 class='chart-render-failure'>Failed to render chart</h1>"
# Close the chart's figure as we are no longer going to be using it.
if self._chart is not None:
Expand All @@ -138,7 +150,7 @@ def generate_html(self, document: Optional[Document] = None) -> str:

def _create_html_comment(self):
template = Template("""
<p class="comment">{{ comment }}</p>
""")
<p class="comment">{{ comment }}</p>
""")

return template.render(comment=self.comment)
2 changes: 2 additions & 0 deletions qf_lib/plotting/charts/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit b6eba1e

Please sign in to comment.