Skip to content

Commit

Permalink
Improve core selector loading (#1594)
Browse files Browse the repository at this point in the history
* Improve core selector loading
resolves #1543 ?

* line legth

* Nam's comment

---------

Co-authored-by: Fred Lefévère-Laoide <Fred.Lefevere-Laoide@Taipy.io>
  • Loading branch information
FredLL-Avaiga and Fred Lefévère-Laoide authored Jul 27, 2024
1 parent e7908d6 commit 853dc12
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
29 changes: 20 additions & 9 deletions taipy/gui/_renderers/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,13 @@ def __set_string_or_number_attribute(self, name: str, default_value: t.Optional[
def __set_react_attribute(self, name: str, value: t.Any):
return self.set_attribute(name, "{!" + (str(value).lower() if isinstance(value, bool) else str(value)) + "!}")

def _get_lov_adapter(self, var_name: str, property_name: t.Optional[str] = None, multi_selection=True): # noqa: C901
def _get_lov_adapter( # noqa: C901
self, var_name: str, property_name: t.Optional[str] = None, multi_selection=True, with_default=True
):
property_name = var_name if property_name is None else property_name
lov_name = self.__hashes.get(var_name)
lov = self.__get_list_of_(var_name)
default_lov = []
default_lov: t.Optional[t.List[t.Any]] = [] if with_default or not lov_name else None

adapter = self.__attributes.get("adapter")
if adapter and isinstance(adapter, str):
Expand All @@ -396,15 +398,15 @@ def _get_lov_adapter(self, var_name: str, property_name: t.Optional[str] = None,
if isinstance(lov, list):
if not isinstance(var_type, str):
elt = None
if len(lov) == 0:
if lov:
elt = lov[0]
else:
value = self.__attributes.get("value")
if isinstance(value, list):
if len(value) > 0:
elt = value[0]
else:
elt = value
else:
elt = lov[0]
var_type = self.__gui._get_unique_type_adapter(type(elt).__name__)
if adapter is None:
adapter = self.__gui._get_adapter_for_type(var_type)
Expand All @@ -427,7 +429,7 @@ def _get_lov_adapter(self, var_name: str, property_name: t.Optional[str] = None,
if adapter is not None:
self.__gui._add_adapter_for_type(var_type, adapter) # type: ignore

if len(lov) > 0:
if default_lov is not None and lov:
for elt in lov:
ret = self.__gui._run_adapter(
t.cast(t.Callable, adapter), elt, adapter.__name__ if callable(adapter) else "adapter"
Expand All @@ -453,7 +455,8 @@ def _get_lov_adapter(self, var_name: str, property_name: t.Optional[str] = None,
self.__set_default_value("value", ret_val)

# LoV default value
self.__set_json_attribute(_to_camel_case(f"default_{property_name}"), default_lov)
if default_lov is not None:
self.__set_json_attribute(_to_camel_case(f"default_{property_name}"), default_lov)

# LoV expression binding
if lov_name:
Expand Down Expand Up @@ -1031,8 +1034,16 @@ def set_attributes(self, attributes: t.List[tuple]): # noqa: C901
self.__set_dynamic_date_attribute(attr[0], _get_tuple_val(attr, 2, None))
elif var_type == PropertyType.data:
self.__set_dynamic_property_without_default(attr[0], var_type)
elif var_type == PropertyType.lov or var_type == PropertyType.single_lov:
self._get_lov_adapter(attr[0], multi_selection=var_type != PropertyType.single_lov)
elif (
var_type == PropertyType.lov
or var_type == PropertyType.single_lov
or var_type == PropertyType.lov_no_default
):
self._get_lov_adapter(
attr[0],
multi_selection=var_type != PropertyType.single_lov,
with_default=var_type != PropertyType.lov_no_default,
)
elif var_type == PropertyType.lov_value:
self.__set_dynamic_property_without_default(
attr[0], var_type, _get_tuple_val(attr, 2, None) == "optional"
Expand Down
1 change: 1 addition & 0 deletions taipy/gui/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class PropertyType(Enum):
json = "json"
single_lov = "singlelov"
lov = _TaipyLov
lov_no_default = "lovnodefault"
"""
The property holds a LoV (list of values).
"""
Expand Down
4 changes: 2 additions & 2 deletions taipy/gui_core/_GuiCoreLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class _GuiCore(ElementLibrary):
},
inner_properties={
"inner_scenarios": ElementProperty(
PropertyType.lov,
PropertyType.lov_no_default,
f"{{{__CTX_VAR_NAME}.get_scenarios(<tp:prop:{__SEL_SCENARIOS_PROP}>, "
+ f"{__SCENARIO_SELECTOR_FILTER_VAR}<tp:uniq:sc>, "
+ f"{__SCENARIO_SELECTOR_SORT_VAR}<tp:uniq:sc>)}}",
Expand Down Expand Up @@ -182,7 +182,7 @@ class _GuiCore(ElementLibrary):
},
inner_properties={
"inner_datanodes": ElementProperty(
PropertyType.lov,
PropertyType.lov_no_default,
f"{{{__CTX_VAR_NAME}.get_datanodes_tree(<tp:prop:{__DATANODE_SEL_SCENARIO_PROP}>, "
+ f"<tp:prop:{__SEL_DATANODES_PROP}>, "
+ f"{__DATANODE_SELECTOR_FILTER_VAR}<tp:uniq:dns>, "
Expand Down

0 comments on commit 853dc12

Please sign in to comment.