Browse Source

lambda in exported page (#1923)

* lambda in exported page
resolves #1920

* add tests

---------

Co-authored-by: Fred Lefévère-Laoide <Fred.Lefevere-Laoide@Taipy.io>
Fred Lefévère-Laoide 7 tháng trước cách đây
mục cha
commit
6dac9041c4

+ 4 - 7
taipy/gui/_renderers/builder.py

@@ -159,20 +159,17 @@ class _Builder:
             hash_name = hash_names.get(k)
             if hash_name is None:
                 if isinstance(v, str):
-                    looks_like_a_lambda = v.startswith("{lambda ") and v.endswith("}")
                     # need to unescape the double quotes that were escaped during preprocessing
                     (val, hash_name) = _Builder.__parse_attribute_value(gui, v.replace('\\"', '"'))
                 else:
-                    looks_like_a_lambda = False
                     val = v
-                if isroutine(val):
+                if isroutine(val) and not hash_name:
                     # if it's not a callable (and not a string), forget it
                     if val.__name__ == "<lambda>":
                         # if it is a lambda and it has already a hash_name, we're fine
-                        if looks_like_a_lambda or not hash_name:
-                            hash_name = _get_lambda_id(t.cast(LambdaType, val))
-                            gui._bind_var_val(hash_name, val)  # type: ignore[arg-type]
-                    elif not hash_name:
+                        hash_name = _get_lambda_id(t.cast(LambdaType, val))
+                        gui._bind_var_val(hash_name, val)  # type: ignore[arg-type]
+                    else:
                         hash_name = _get_expr_var_name(val.__name__)
 
                 if val is not None or hash_name:

+ 10 - 0
tests/gui/lambdas/__init__.py

@@ -0,0 +1,10 @@
+# Copyright 2021-2024 Avaiga Private Limited
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+#        http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+# 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.

+ 1 - 0
tests/gui/lambdas/another_module.py

@@ -0,0 +1 @@
+exported_page ="<|Hello|button|on_action={lambda s,i,p: None}|>"

+ 24 - 0
tests/gui/lambdas/test_lambda.py

@@ -0,0 +1,24 @@
+# Copyright 2021-2024 Avaiga Private Limited
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+#        http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+# 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.
+from taipy.gui import Gui
+
+from .another_module import exported_page
+
+
+def test_lambda_1(gui: Gui, test_client, helpers):
+    md_string = "<|Hello|button|on_action={lambda s,i,p: None}|>"
+    expected_list = ['onAction="__lambda_', "_TPMDL_1"]
+    helpers.test_control_md(gui, md_string, expected_list)
+
+def test_lambda_2(gui: Gui, test_client, helpers):
+    expected_list = ['onAction="__lambda_', "_TPMDL_1"]
+    gui.add_page("test", exported_page)
+    helpers._test_control(gui, expected_list)