Skip to content
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

Trigger attribute on dropdowns fire unconditionally across a page #44

Closed
lo5 opened this issue Aug 21, 2020 · 6 comments
Closed

Trigger attribute on dropdowns fire unconditionally across a page #44

lo5 opened this issue Aug 21, 2020 · 6 comments
Assignees
Labels
question Question ui Related to UI
Milestone

Comments

@lo5
Copy link
Member

lo5 commented Aug 21, 2020

Via @FelixArokia

async def main_page(q: Q):
    q.page['main_title'] = ui.form_card(box = '4 1 5 1', items = [
                                    ui.text_xl("This is the main page"),
    ])
    store_choices = [ui.choice('Store_1', 'Store_1'), ui.choice('Store_2', 'Store_2')]
    selected_store = 'Store_1'
    q.page['store_dropdown_card'] = ui.form_card(box = '2 2 2 1', items = [
        ui.dropdown(name='arg_store_dropdown', label='Select Store', value=selected_store,
                    required=True, choices=store_choices, trigger=True),
    ])
    item_choices = [ui.choice('Item_1', 'Item_1'), ui.choice('Item_2', 'Item_2')]
    selected_item = 'Item_1'
    q.page['item_dropdown_card'] = ui.form_card(box='8 2 2 1', items = [
        ui.dropdown(name='arg_item_dropdown', label='Select Item', value=selected_item,
                    required=True, choices=item_choices, trigger=True),
    ])
    if q.args.arg_store_dropdown:
        q.page['store_trigger'] = ui.form_card(box = '1 4 10 1', items = [
        ui.text_xl("Store drop down was selected. SUCCESS STORE"),])
    if q.args.arg_item_dropdown:
        q.page['item_trigger'] = ui.form_card(box = '1 6 10 1', items = [
        ui.text_xl("Item drop down was selected. SUCCESS ITEM"),])
    await q.page.save()
listen("/trigger_working", main_page)
@lo5 lo5 added ui Related to UI bug Bug in code labels Aug 21, 2020
@lo5 lo5 self-assigned this Aug 21, 2020
@lo5 lo5 added this to the 0.2.0 milestone Aug 21, 2020
@lo5
Copy link
Member Author

lo5 commented Sep 11, 2020

@FelixArokia I verified this. This is not a bug.

Since the value= is set on both dropdowns, q.args.arg_store_dropdown and q.args.arg_item_dropdown are initialized to those values.

When either dropdown is changed, a form submission is triggered, and hence you get back both q.args.arg_store_dropdown and q.args.arg_item_dropdown exactly as you set them. This is normal behavior.

I'm guessing that you were expecting exactly one of q.args.arg_store_dropdown or q.args.arg_item_dropdown to be set when changed, but that is not how the model works. The SDK's behavior mimics that of HTML forms: regardless of which element is changed, the values of all elements are submitted to your program.

Regardless, given that this is the behavior, and given that this cannot be changed, I'd like to know if this behavior is a show stopper for you, so that we can accommodate your case properly.

cc @mturoci

@lo5 lo5 added question Question and removed bug Bug in code labels Sep 11, 2020
@vopani
Copy link
Contributor

vopani commented Sep 11, 2020

Q-MB has similar use-case.

This is what I do (tuned for your example):

async def main_page(q: Q):
   if not q.client.initialized:
      q.user.arg_store_dropdown = 'Store 1'
      q.user.arg_item_dropdown = 'Item 1'
      q.client.initialized = True

   if q.user.arg_store_dropdown != q.args.arg_store_dropdown:
      q.user.arg_store_dropdown = q.args.arg_store_dropdown
      q.page['store_trigger'] = ui.form_card(box = '1 4 10 1', items = [
         ui.text_xl("Store drop down was selected. SUCCESS STORE"),])
   elif q.user.arg_item_dropdown != q.args.arg_item_dropdown:
      q.user.arg_item_dropdown = q.args.arg_item_dropdown
      q.page['store_trigger'] = ui.form_card(box = '1 4 10 1', items = [
         ui.text_xl("Item drop down was selected. SUCCESS ITEM"),])

Is there a better way?

@lo5
Copy link
Member Author

lo5 commented Sep 11, 2020

@vopani - thanks for the example. Assuming your example demonstrates "how to identify which dropdown was changed", is this something you commonly need, and why? Why wouldn't you blindly refer to q.args (or q.args copied over to q.user) instead of trying to identify which dropdown changed?

@vopani
Copy link
Contributor

vopani commented Sep 11, 2020

I actually refactored it to use args blindly: https://github.com/h2oai/q-marketbasket/blob/master/run.py#L634

But I can see why knowing which dropdown changed can be useful too. eg: I have a bar chart. I change a dropdown. The bar chart shows the original values (dotted) and the new values (comparing the state of the chart before the dropdown change and after). The chart would only make sense if I know what dropdown was changed and see the values before/after the change.
How would you do this without knowing which dropdown changed?

Whether it's common or not, I'm not sure but I'd say not so common. In most cases, can just use args blindly.
Maybe @FelixArokia can share his exact requirement to determine how best to solve for it.

@FelixArokia
Copy link

Not a showstopper @lo5 , I am following a very similar approach as given by @vopani here. Thanks for the clarification

@lo5
Copy link
Member Author

lo5 commented Sep 16, 2020

Thanks @FelixArokia @vopani. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question ui Related to UI
Projects
None yet
Development

No branches or pull requests

3 participants