From d1d5c8f103ab359db2e002c4c68f7865cfcbc80e Mon Sep 17 00:00:00 2001 From: Sebastien Jourdain Date: Mon, 11 Dec 2023 17:21:52 -0700 Subject: [PATCH] fix(debug): Add helper for finding invalid args --- examples/test/attr_type.py | 19 +++++++++++++++++++ trame_client/widgets/core.py | 24 ++++++++++++++++++------ 2 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 examples/test/attr_type.py diff --git a/examples/test/attr_type.py b/examples/test/attr_type.py new file mode 100644 index 0000000..6e5cc89 --- /dev/null +++ b/examples/test/attr_type.py @@ -0,0 +1,19 @@ +# Run with -d or --debug +from trame.app import get_server +from trame.ui.html import DivLayout +from trame.widgets import html + +server = get_server(client_type="vue3") + +with DivLayout(server) as layout: + html.Div( + v_show=True, + test=({},), + test_1=([],), + test_2=(5,), + __properties=[ + "test", + "test_1", + "test_2", + ], + ) diff --git a/trame_client/widgets/core.py b/trame_client/widgets/core.py index e4680a3..47b862d 100644 --- a/trame_client/widgets/core.py +++ b/trame_client/widgets/core.py @@ -1,3 +1,4 @@ +import sys import logging from ..utils.defaults import TrameDefault from ..utils.formatter import to_pretty_html @@ -262,7 +263,7 @@ class AbstractElement: """ _next_id = 1 - _debug = False + _debug = "--debug" in sys.argv or "-d" in sys.argv def __init__(self, _elem_name, children=None, **kwargs): AbstractElement._next_id += 1 @@ -421,7 +422,7 @@ def attrs(self, *names): and js_key.startswith("v-") and not isinstance(value, (tuple, list)) ): - print( + logger.warn( f'Warning: A Vue directive is evaluating your expression and trame would expect a tuple instead of a plain type. <{self._elem_name} {js_key}="{value}" ... />' ) @@ -433,11 +434,22 @@ def attrs(self, *names): self.server.state.setdefault(value[0], value[1]) logger.info("before: %s = %s", js_key, value[0]) - translated_value = ( - self.server.state.translator.translate_js_expression( - self.server.state, value[0] + if isinstance(value[0], str): + translated_value = ( + self.server.state.translator.translate_js_expression( + self.server.state, value[0] + ) ) - ) + else: + translated_value = str(value[0]) + if AbstractElement._debug: + logger.warn( + 'Warning: <%s %s="..." /> is set with an (%s)', + self._elem_name, + js_key, + type(value[0]), + ) + logger.info("after: %s = %s", js_key, translated_value) if js_key.startswith("v-"): self._attributes[name] = f'{js_key}="{translated_value}"'