Skip to content

Update control.py #5300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions sdk/python/packages/flet/src/flet/core/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,24 @@ def _get_attr(
if name not in self.__attrs:
return def_value
s_val = self.__attrs[name][0]
if data_type == "bool" and s_val is not None and isinstance(s_val, str):

if not isinstance(s_val, str):
return s_val

if data_type == "bool":
return s_val.lower() == "true"
elif data_type == "bool?" and isinstance(s_val, str):
elif data_type == "bool?":
if s_val.lower() == "true":
return True
elif s_val.lower() == "false":
return False
else:
return def_value
elif data_type == "float" and s_val is not None and isinstance(s_val, str):
elif data_type == "float":
return float(s_val)
elif data_type == "int" and s_val is not None and isinstance(s_val, str):
elif data_type == "int":
return int(s_val)
else:
return s_val
return s_val

def _set_attr(self, name: str, value: V, dirty: bool = True) -> None:
self._set_attr_internal(name, value, dirty)
Expand Down Expand Up @@ -340,34 +343,33 @@ def invoke_method(
wait_for_result: bool = False,
wait_timeout: Optional[float] = 5,
) -> Optional[str]:
assert (
self.__page
), f"{self.__class__.__qualname__} Control must be added to the page first"
if arguments:
# remove items with None values and convert other values to string
arguments = {k: str(v) for k, v in arguments.items() if v is not None}
return self.__page._invoke_method(
control_id=self.uid,
method_name=method_name,
arguments=arguments,
wait_for_result=wait_for_result,
wait_timeout=wait_timeout,
)
return self.__invoke_method(method_name,arguments,wait_for_result,wait_timeout)

def invoke_method_async(
self,
method_name: str,
arguments: Optional[Dict[str, str]] = None,
wait_for_result: bool = False,
wait_timeout: Optional[float] = 5,
):
return self.__invoke_method(method_name,arguments,wait_for_result,wait_timeout, is_async=True)

def __invoke_method(
self,
method_name: str,
arguments: Optional[Dict[str, str]] = None,
wait_for_result: bool = False,
wait_timeout: Optional[float] = 5,
*,
is_async: bool = False
):
assert (
self.__page
), f"{self.__class__.__qualname__} Control must be added to the page first"
if arguments:
# remove items with None values and convert other values to string
arguments = {k: str(v) for k, v in arguments.items() if v is not None}
return self.__page._invoke_method_async(
return (self.__page._invoke_method_async if is_async else self.__page._invoke_method)(
control_id=self.uid,
method_name=method_name,
arguments=arguments,
Expand Down Expand Up @@ -594,9 +596,7 @@ def _dispose(self) -> None:

# Magic methods
def __str__(self) -> str:
attrs = {}
for k, v in self.__attrs.items():
attrs[k] = v[0]
attrs = {k: v[0] for k, v in self.__attrs.items()}
return f"{self._get_control_name()} {attrs}"

def __repr__(self) -> str:
Expand Down