Replies: 3 comments 6 replies
-
Example for when that might come in handy: @app("/")
async def serve(q: Q):
if not q.client.initialized:
q.page['meta'] = ui.meta_card(box='')
q.client.initialized = True
q.client.textbox1 = q.client.textbox2 = ""
await q.page.save()
await handle_on(q)
q.page['main'] = ui.form_card(
box='4 2 3 3',
title='',
items=[
ui.textbox(name="textbox1", label="textbox 1", trigger=True, value=q.client.textbox1 or ""),
ui.textbox(name="textbox2", label="textbox 2", trigger=True, value=q.client.textbox2 or ""),
ui.text(content=q.client.changed_text or ""),
],
)
await q.page.save()
#@on("textbox1") # won't trigger when textbox is cleared, because "" is not truthy
@on("textbox1", lambda x: x is not None)
async def ontextbox1(q: Q):
print("textbox1 changed")
if q.client.textbox1 != q.args.textbox1:
q.client.changed_text = f"textbox1 value changed from {q.client.textbox1} to {q.args.textbox1}"
q.client.textbox1 = q.args.textbox1
@on("textbox2", lambda x: x is not None)
async def ontextbox2(q: Q):
print("textbox2 changed")
if q.client.textbox2 != q.args.textbox2:
q.client.changed_text = f"textbox2 value changed from {q.client.textbox2} to {q.args.textbox2}"
q.client.textbox2 = q.args.textbox2 87YwOOUUWa.mp4So we need to rewrite this app in order to get the desired behavior (and capture text changes of def on_change(arg: str = None, predicate: Optional[Callable] = None, compared_to="client", propagate=False):
def decorator(on_decorated):
@on(arg, predicate)
async def wrapper(q: Q, *args, **kwargs):
res = None
async def _propagate():
val = q.args[arg]
q.args[arg] = None
await handle_on(q)
q.args[arg] = val
if q.args[arg] == getattr(q, compared_to)[arg]:
await _propagate()
else:
res = await on_decorated(q)
if propagate:
await _propagate()
return res
return wrapper
return decorator
@app("/")
async def serve(q: Q):
if not q.client.initialized:
q.page['meta'] = ui.meta_card(box='')
q.client.initialized = True
q.client.textbox1 = q.client.textbox2 = ""
await q.page.save()
await handle_on(q)
q.page['main'] = ui.form_card(
box='4 2 3 3',
title='',
items=[
ui.textbox(name="textbox1", label="textbox 1", trigger=True, value=q.client.textbox1 or ""),
ui.textbox(name="textbox2", label="textbox 2", trigger=True, value=q.client.textbox2 or ""),
ui.text(content=q.client.changed_text or ""),
],
)
await q.page.save()
@on_change("textbox1", lambda x: x is not None, compared_to="client")
async def ontextbox1_change(q: Q):
print("textbox1 changed")
q.client.changed_text = f"textbox1 value changed from {q.client.textbox1} to {q.args.textbox1}"
q.client.textbox1 = q.args.textbox1
@on_change("textbox2", lambda x: x is not None, compared_to="client")
async def ontextbox2_change(q: Q):
print("textbox2 changed")
q.client.changed_text = f"textbox2 value changed from {q.client.textbox2} to {q.args.textbox2}"
q.client.textbox2 = q.args.textbox2 aQdzgrURAY.mp4or alternatively with a async def handle_on_changes_only(q, compared_to="client"):
restore_args = {}
for k in q.args.__dict__['__kv'].keys():
if q.args[k] == getattr(q, compared_to)[k]:
restore_args[k] = q.args[k]
q.args[k] = None
await handle_on(q)
q.args.__dict__['__kv'].update(restore_args)
@app("/")
async def serve(q: Q):
if not q.client.initialized:
q.page['meta'] = ui.meta_card(box='')
q.client.initialized = True
q.client.textbox1 = q.client.textbox2 = ""
await q.page.save()
await handle_on_changes_only(q)
q.page['main'] = ui.form_card(
box='4 2 3 3',
title='',
items=[
ui.textbox(name="textbox1", label="textbox 1", trigger=True, value=q.client.textbox1 or ""),
ui.textbox(name="textbox2", label="textbox 2", trigger=True, value=q.client.textbox2 or ""),
ui.text(content=q.client.changed_text or ""),
],
)
await q.page.save()
@on("textbox1", lambda x: x is not None)
async def ontextbox1_change(q: Q):
print("textbox1 changed")
q.client.changed_text = f"textbox1 value changed from {q.client.textbox1} to {q.args.textbox1}"
q.client.textbox1 = q.args.textbox1
@on("textbox2", lambda x: x is not None)
async def ontextbox2_change(q: Q):
print("textbox2 changed")
q.client.changed_text = f"textbox2 value changed from {q.client.textbox2} to {q.args.textbox2}"
q.client.textbox2 = q.args.textbox2 |
Beta Was this translation helpful? Give feedback.
-
I am also facing a similar issue due to the same reason. Event.handling.order.issue.Wave.movIf the users click on the @lo5 Can't we have a solution for this from the SDK side? Can't we include some meta information like the event source of the last event in q state other than setting |
Beta Was this translation helpful? Give feedback.
-
@dulajra Can I get a minimal repro of what's not working? How are the handlers configured? Also, how is it related to |
Beta Was this translation helpful? Give feedback.
-
I think it would be useful to have some information about
trigger=True
events inq.events
. Reason being that it is currently a bit cumbersome to find the source of a "triggered" submit viaq.args
... or some way to handle "on_change" between
q.args.k
andq.[client|app|user].k
(see comment below)Beta Was this translation helpful? Give feedback.
All reactions