瀏覽代碼

avoid importing pandas if not needed (#3548)

Falko Schindler 9 月之前
父節點
當前提交
c922b46f42
共有 2 個文件被更改,包括 14 次插入10 次删除
  1. 7 5
      nicegui/elements/aggrid.py
  2. 7 5
      nicegui/elements/table.py

+ 7 - 5
nicegui/elements/aggrid.py

@@ -1,4 +1,5 @@
-from typing import Dict, List, Optional, cast
+import importlib.util
+from typing import TYPE_CHECKING, Dict, List, Optional, cast
 
 from typing_extensions import Self
 
@@ -6,11 +7,10 @@ from .. import optional_features
 from ..awaitable_response import AwaitableResponse
 from ..element import Element
 
-try:
-    import pandas as pd
+if importlib.util.find_spec('pandas'):
     optional_features.register('pandas')
-except ImportError:
-    pass
+    if TYPE_CHECKING:
+        import pandas as pd
 
 
 class AgGrid(Element, component='aggrid.js', libraries=['lib/aggrid/ag-grid-community.min.js']):
@@ -59,6 +59,8 @@ class AgGrid(Element, component='aggrid.js', libraries=['lib/aggrid/ag-grid-comm
         :param options: dictionary of additional AG Grid options
         :return: AG Grid element
         """
+        import pandas as pd  # pylint: disable=import-outside-toplevel
+
         def is_special_dtype(dtype):
             return (pd.api.types.is_datetime64_any_dtype(dtype) or
                     pd.api.types.is_timedelta64_dtype(dtype) or

+ 7 - 5
nicegui/elements/table.py

@@ -1,4 +1,5 @@
-from typing import Any, Callable, Dict, List, Literal, Optional, Union
+import importlib.util
+from typing import TYPE_CHECKING, Any, Callable, Dict, List, Literal, Optional, Union
 
 from typing_extensions import Self
 
@@ -7,11 +8,10 @@ from ..element import Element
 from ..events import GenericEventArguments, TableSelectionEventArguments, ValueChangeEventArguments, handle_event
 from .mixins.filter_element import FilterElement
 
-try:
-    import pandas as pd
+if importlib.util.find_spec('pandas'):
     optional_features.register('pandas')
-except ImportError:
-    pass
+    if TYPE_CHECKING:
+        import pandas as pd
 
 
 class Table(FilterElement, component='table.js'):
@@ -110,6 +110,8 @@ class Table(FilterElement, component='table.js'):
         :param on_select: callback which is invoked when the selection changes
         :return: table element
         """
+        import pandas as pd  # pylint: disable=import-outside-toplevel
+
         def is_special_dtype(dtype):
             return (pd.api.types.is_datetime64_any_dtype(dtype) or
                     pd.api.types.is_timedelta64_dtype(dtype) or