Outputting tables.Row data to a Repeating Panel

I am trying to display my tables data in a repeating panel. From reading the documents it seems pretty straightforward, pass an iterable to panel.items and then use self.item[key] to access each element.

In my example I have a list of tables.Row objects - [row obj, row obj, row obj]
If i print panel.items, I can see the list of objects in the console. But now if i try to access self.item[‘name’] I get “Key Error ‘name’”.
If i simply try to output self.item i get {} (empty dict)

I also tried converting the row objects to a list of list, for example i have [ [‘aaron’, 21], [‘nicole’, 20] ].
If i print panel.items i can clearly see the list of lists. Now if i try to access it using self.item[0] I also get a key error. And same as before if i try to access self.item i just get {}

Am i trying to do this inherently wrong? Seems like the right usage from the docs, but maybe it’s not possible to use row objects or nested lists?

Thanks in advance for any help.

Welcome to the Anvil Forum!

Your aims sound feasible. The problem is likely in how your code refers to these data items. (That includes references that appear in your Data Bindings, too, if you have them.)

But we can’t see that code until you post it.

Hint: to make a snippet of code readable in a forum post, put a line consisting of three back ticks (```) immediately before and after your lines of code. Among other things, this will preserve the indentation that means so much in Python code.

1 Like

Hey thanks for the reply, sorry i thought the code was overkill, i assumed since i could see the objects in self.panel.items that there was no problem with the incoming data!

So i have a method get_players() in the server that simply returns all the rows from my table

@anvil.server.callable
  def get_players():
    players = [player for player in app_tables.scores.search()]
    return players

Then in the client code, I’m displaying it in a form. The forms init method is as follows:

def __init__(self, **properties):
    self.init_components(**properties)
    players = anvil.server.call('get_players')
    self.scores_panel.items = players
    self.name_label.text = self.item['name']

So if I print(self.scores_panel.items) I can see it contains my anvil.tables.Row objects but when i try to assign it to the labels text it’s giving me Key error: name

I doubled checked and name is the correct key from the database, even in the server code just to double checked myself, after I generate the players I added:

for player in players:
    print(player['name'])

And this outputs perfectly, so I’m unsure where the issue is. I didn’t use any data bindings in the Design section, did everything with code so far. Thanks for any advice

EDIT: Btw I know i could assign them by saying self.scores_panel.items = app_tables.scores.search(), but down the line I will be wanting to manipulate that data before passing it on, just in case you were going to mention that as a possible solution

self.item is not the same object as self.scores_panel.items. In fact, self.item is not defined in your main form (nor should it be).

I suspect that the line containing self.item belongs in a different form altogether, i.e., the Data Row (template) that you use in your Repeating Panel.

Yes, there are three layers of objects:

  1. The Form containing the Repeating Panel (self, in the code above)
  2. The Repeating Panel (self.scores_panel, in the code above). Its items member is rigged to accept a list of dictionary-like items, such as database table rows.
  3. The row template (another Form you create),used by the Repeating Panel. For each list entry, the Repeating Panel creates a new instance of this template, and sets that instance’s item member to the list entry. So, self.name_label.text = self.item['name'] makes sense only in the template’s __init__ function.

Hope this helps.

1 Like

Yeah this makes absolutely perfect sense, I just totally misinterpreted the documentation. The whole time I was wondering how the form with the repeating panel was iterating over items in the background, but yeah obviously that’s happening in the Template form.

Thank you for the great explanation!

You’re very welcome. For what it’s worth, I didn’t get it at first, either. Nor Data Binding. They make a lot more sense once the context is pieced together.