From 227ccc65d4cb8586a9699abbe0bdf1d920b1d536 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Wed, 8 Feb 2023 23:37:11 +0000 Subject: [PATCH] perf: load matplotlib on-demand it takes ~1.7 seconds to import electrum.plot, slowing down app-startup considerably --- electrum/gui/qt/history_list.py | 15 +++++++-------- electrum/plot.py | 2 ++ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/electrum/gui/qt/history_list.py b/electrum/gui/qt/history_list.py index 374bb84c4..38e3dbd3e 100644 --- a/electrum/gui/qt/history_list.py +++ b/electrum/gui/qt/history_list.py @@ -61,12 +61,6 @@ if TYPE_CHECKING: _logger = get_logger(__name__) -try: - from electrum.plot import plot_history, NothingToPlotException -except: - _logger.info("could not import electrum.plot. This feature needs matplotlib to be installed.") - plot_history = None - # note: this list needs to be kept in sync with another in kivy TX_ICONS = [ "unconfirmed.png", @@ -631,10 +625,15 @@ class HistoryList(MyTreeView, AcceptFileDragDrop): d.exec_() def plot_history_dialog(self): - if plot_history is None: + try: + from electrum.plot import plot_history, NothingToPlotException + except Exception as e: + _logger.error(f"could not import electrum.plot. This feature needs matplotlib to be installed. exc={e!r}") self.parent.show_message( _("Can't plot history.") + '\n' + - _("Perhaps some dependencies are missing...") + " (matplotlib?)") + _("Perhaps some dependencies are missing...") + " (matplotlib?)" + '\n' + + f"Error: {e!r}" + ) return try: plt = plot_history(list(self.hm.transactions.values())) diff --git a/electrum/plot.py b/electrum/plot.py index a22a1d9f8..1b27bf5d9 100644 --- a/electrum/plot.py +++ b/electrum/plot.py @@ -1,3 +1,5 @@ +# note: This module takes 1-2 seconds to import. It should be imported *on-demand*. + import datetime from collections import defaultdict