瀏覽代碼

Accept Pandas Series in lov property (#1447)

* Add conditions to convert pd.Series to list object

* Add a test case for panda series

* Moved new lines to _get_adapted_lov

Removed unnecessary lines

* Updated builder.py and added ignore line to avoid linter error

* Updated adapter.py without using pandas

* Changed the name of the test function
Satoshi S. 10 月之前
父節點
當前提交
a0ddd41fb3
共有 3 個文件被更改,包括 29 次插入1 次删除
  1. 6 1
      taipy/gui/_renderers/builder.py
  2. 5 0
      taipy/gui/utils/_adapter.py
  3. 18 0
      tests/gui/builder/control/test_selector.py

+ 6 - 1
taipy/gui/_renderers/builder.py

@@ -191,6 +191,11 @@ class _Builder:
         lof = self.__attributes.get(name)
         if isinstance(lof, str):
             lof = list(lof.split(";"))
+        if not isinstance(lof, list) and hasattr(lof, "tolist"):
+            try:
+                return lof.tolist()  # type: ignore[union-attr]
+            except Exception as e:
+                _warn("Error accessing List of values", e)
         return lof
 
     def get_name_indexed_property(self, name: str) -> t.Dict[str, t.Any]:
@@ -955,7 +960,7 @@ class _Builder:
 
             attributes (list(tuple)): The list of attributes as (property name, property type, default value).
         """
-        attributes.append(("id",)) # Every element should have an id attribute
+        attributes.append(("id",))  # Every element should have an id attribute
         for attr in attributes:
             if not isinstance(attr, tuple):
                 attr = (attr,)

+ 5 - 0
taipy/gui/utils/_adapter.py

@@ -40,6 +40,11 @@ class _Adapter:
         self.__warning_by_type: t.Set[str] = set()
 
     def _get_adapted_lov(self, lov: t.Any, var_type: str) -> _AdaptedLov:
+        if not isinstance(lov, list) and hasattr(lov, "tolist"):
+            try:
+                lov = lov.tolist()  # type: ignore[union-attr]
+            except Exception as e:
+                _warn("Error accessing List of values", e)
         return _AdaptedLov(lov, var_type)
 
     def _add_for_type(self, type_name: str, adapter: t.Callable) -> None:

+ 18 - 0
tests/gui/builder/control/test_selector.py

@@ -9,6 +9,8 @@
 # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 # specific language governing permissions and limitations under the License.
 
+import pandas as pd
+
 import taipy.gui.builder as tgb
 from taipy.gui import Gui
 
@@ -69,3 +71,19 @@ def test_selector_builder_3(gui: Gui, test_client, helpers):
         "value={_TpLv_tpec_TpExPr_selected_obj_TPMDL_0}",
     ]
     helpers.test_control_builder(gui, page, expected_list)
+
+
+def test_selector_pandas_series(gui: Gui, test_client, helpers):
+    pd_series = pd.Series(["l1", "l2", "l3"])
+    gui._bind_var_val("selected_val", "l1")
+    gui._bind_var_val("selector_properties", pd_series)
+    with tgb.Page(frame=None) as page:
+        tgb.selector(value="{selected_val}", lov="{selector_properties}")  # type: ignore[attr-defined]
+    expected_list = [
+        "<Selector",
+        'defaultLov="[&quot;l1&quot;, &quot;l2&quot;, &quot;l3&quot;]"',
+        'defaultValue="[&quot;l1&quot;]"',
+        'updateVarName="_TpLv_tpec_TpExPr_selected_val_TPMDL_0"',
+        "value={_TpLv_tpec_TpExPr_selected_val_TPMDL_0}",
+    ]
+    helpers.test_control_builder(gui, page, expected_list)