From c304c3563b68235206745ba1fd0c17463c674a04 Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Wed, 17 Jan 2024 20:16:26 -0600 Subject: [PATCH] Use dynamic imports to delay package downloading until needed --- src/hooks/usePyodide.tsx | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/hooks/usePyodide.tsx b/src/hooks/usePyodide.tsx index ae23705a..809738e4 100644 --- a/src/hooks/usePyodide.tsx +++ b/src/hooks/usePyodide.tsx @@ -212,8 +212,8 @@ def _shim_seaborn_load_dataset(): import importlib import importlib.abc import io + import os import sys - import pyodide.code # ========================================================================== @@ -257,10 +257,12 @@ def _shim_seaborn_load_dataset(): return list(filter(None, dataset_names)) def load_dataset(name, cache=True, data_home=None, **kws): - import os - - import pandas as pd - import seaborn + # Load these dynamically with importlib instead of with 'import pandas' + # because if we do a normal import, then Pyodide will detect and + # automatically download the files for these packages and all their + # dependencies when it starts, which is slow. + pd = importlib.import_module("pandas") + seaborn = importlib.import_module("seaborn") if isinstance(name, pd.DataFrame): err = (