Skip to content

Commit

Permalink
fix(debug): Add helper for finding invalid args
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Dec 12, 2023
1 parent 6a63a8a commit d1d5c8f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
19 changes: 19 additions & 0 deletions examples/test/attr_type.py
Original file line number Diff line number Diff line change
@@ -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",
],
)
24 changes: 18 additions & 6 deletions trame_client/widgets/core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import logging
from ..utils.defaults import TrameDefault
from ..utils.formatter import to_pretty_html
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}" ... />'
)

Expand All @@ -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}"'
Expand Down

0 comments on commit d1d5c8f

Please sign in to comment.