qf-lib icon indicating copy to clipboard operation
qf-lib copied to clipboard

[Bug]: Adding a Chart twice through `ChartElement` to a Document results in `_tkinter.TclError: invalid command name ".!canvas"`

Open myrmarachne opened this issue 1 year ago • 0 comments

Environment

- QF-LIB: 3.0.0
- Python: 3.11.8

What happened?

Adding a Chart to an AbstracDocument more than once, while providing a non-None figsize, results in the following error while building and saving the document:

Traceback (most recent call last):
  File "...\qf-lib\qf_lib\documents_utils\document_exporting\element\chart.py", line 110, in generate_html
    base64 = self._chart.render_as_base64_image(self.figsize, self.dpi, self.optimise,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\qf-lib\qf_lib\plotting\charts\chart.py", line 139, in render_as_base64_image
    self.plot(figsize)
  File "...\qf-lib\qf_lib\plotting\charts\line_chart.py", line 52, in plot
    self._setup_axes_if_necessary(figsize)
  File "...\qf-lib\qf_lib\plotting\charts\chart.py", line 302, in _setup_axes_if_necessary
    self.axes.figure.set_size_inches(figsize[0], figsize[1])
  File "...\quant-fin\venv311\Lib\site-packages\matplotlib\figure.py", line 2959, in set_size_inches
    manager.resize(*(size * self.dpi).astype(int))
  File "...\quant-fin\venv311\Lib\site-packages\matplotlib\backends\_backend_tk.py", line 508, in resize
    self.canvas._tkcanvas.configure(width=width, height=height)
  File "C:\Program Files\Python311\Lib\tkinter\__init__.py", line 1721, in configure
    return self._configure('configure', cnf, kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\tkinter\__init__.py", line 1711, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!canvas"

Screenshots

No response

Reproduction steps

from os import getcwd

from demo_scripts.demo_configuration.demo_settings import get_demo_settings
from qf_lib.analysis.common.abstract_document import AbstractDocument
from qf_lib.documents_utils.document_exporting.element.chart import ChartElement
from qf_lib.documents_utils.document_exporting.pdf_exporter import PDFExporter
from qf_lib.plotting.charts.line_chart import LineChart


class TestDocument(AbstractDocument):
    def build_document(self):
        chart = LineChart()
        self.document.add_element(ChartElement(chart, figsize=(6.5, 3.6)))
        self.document.add_element(ChartElement(chart))  # Does not raise Exception
        self.document.add_element(ChartElement(chart, figsize=(6.5, 3.6)))  # Raises Exception

    def save(self, report_dir: str = ""):
        return self.pdf_exporter.generate([self.document], getcwd(), "Test File.pdf")


if __name__ == '__main__':
    settings = get_demo_settings()
    doc = TestDocument(settings, PDFExporter(settings))
    doc.build_document()
    doc.save()

Running the code above produces an error, when the chart is added again to the document with figsize parameter provided.



### Relevant log output

_No response_

myrmarachne avatar Mar 21 '24 11:03 myrmarachne