From a168c00161100b394db37cfd81d76427f5a26ed5 Mon Sep 17 00:00:00 2001 From: Zack Cornelius Date: Fri, 26 Jul 2019 13:53:25 -0500 Subject: [PATCH] Check for existing module in get_module before importing Significantly speeds up graph rendering when using large (10K entry) pandas dataframe columns. Using 50000 point Pandas series, this speeds up scatter plot generation from 2 seconds to ~280ms --- packages/python/plotly/_plotly_utils/optional_imports.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/python/plotly/_plotly_utils/optional_imports.py b/packages/python/plotly/_plotly_utils/optional_imports.py index 7f49d1fe26f..c36e0197b69 100644 --- a/packages/python/plotly/_plotly_utils/optional_imports.py +++ b/packages/python/plotly/_plotly_utils/optional_imports.py @@ -6,6 +6,7 @@ from importlib import import_module import logging +import sys logger = logging.getLogger(__name__) _not_importable = set() @@ -20,6 +21,8 @@ def get_module(name): :return: (module|None) If import succeeds, the module will be returned. """ + if name in sys.modules: + return sys.modules[name] if name not in _not_importable: try: return import_module(name)