How to get an element in a nested repeating panel

my_list = ['anvil', 42, [3, 1, 4]]
for value in my_list:
    print(value)

If you look at this code and need to think about it more than 1 second to guess what’s the output, then you first need to get familiar with python, then you can tackle Anvil. Otherwise it will take you months to create an Anvil app that would otherwise take a few days.

2 Likes

You are right, it did take me too long to build the app. I knew nothing! And I mean nothing! about Python or Anvil. I started in Nov, got Covid and was out of it then, until I picked it up in March.

I just checked and in that time, I read 1.8K posts, and did 64 posts myself! Thats just the forum, let alone the hours I spent on the Docs pages.

Anyway, I solved it after a good night sleep. I checked 2 rows and did a print stmt to confirm that I have 2 entries on the dict.

    dict_stuff_to_attach = []
    for mystuff in self.repeating_panel_mystuff.get_components():
      if mystuff.checkbox_assigned.checked== True:
       	dict_stuff_to_attach.append ({
       "UniqueID": mystuff.mystuff_uid,
        })
      else:
        return
1 Like

Now you’ve got it working, I’ll show you how I might have done it in a declarative (as opposed to imperative) style using a list comprehension:

components = self.repeating_panel_mystuff.get_components()
dict_stuff_to_attach = [
    {"UniqueId": component.mystuff_uid}
    for component in components
    if component.checkbox_assigned
]

(but I do wonder why the dict is needed at all given that it has only a single key).

1 Like

I havent done the server query yet, but I would use the dict to query the mystuff table and get the rows that correspond to the keys. It has to go in a link-multiple on the trips table.
The user gets a list of their stuff and can add it to trips, they can check all they are taking on their trip.

Thanks for the alternative suggestion, its much easier to read the code the way you have done it.

I’m going to guess that you need a list of ids, not a dictionary:

components = self.repeating_panel_mystuff.get_components()
stuff_to_attach = [
  form.mystuff_uid
  for form in components
  if form.checkbox_assigned.checked
]
1 Like

Thanks for this, what if I only want to get the checkbox if it has been checked by the user?

That’s what the last line is for.
I haven’t tested it, but if I understand what’s going on by looking at your snippets, this should work.

https://www.google.com/search?q=list+comprehension+in+python

2 Likes