Trying to retrieve a specific item in a specific row and column from data table

I am trying to return a specific item found at say column 1 row 2 of a data table

this is the data binding I am using to return the entire column

self.item[‘question_column’]

I can retrieve all the data from a column but how do I narrow that down to a specific row in that column

Thanks in advance
Wayne Stokes

You can search or get a row.

Something like:

row = app_tables.questions.search()[1]
row = app_tables.questions.get(question_id='hello')

The first line will return the second row, as you mentioned “row 2”. But relying on the order of the row in the database is not a good idea, unless you specify an sorting order.

The second line will get the row specified by an id.

thanks a million Stefano

Hi Stefano

Am I correct in saying this input is not for data bindings.Or am I missing the point completely.I am trying to get a single entry in the table eg.column1 row2
I am trying to use data bindings

I’ll try to paint the whole picture, starting from the bottom:

  • Databinding allows you to show the value of something on an interface component, like a text box or a label. The databinding can be bidirectional, that is if the text box changes the value, the change is reflected in that something.
  • In Anvil you do the databinding by setting the item property of a component. For components containing lists of components, like a repeating panel, you set their items property, and they will take care of setting the item property of their children components.
  • You usually (not always, but most of the times) get something from the server, using a server call like something = anvil.server.call('get_something', key)
  • The server processes the request with a function like this:
    @anvil.server.callable
    def get_something(key):
      return app_tables.some_table.get(key_column=key)

For example, on the client side:

self.item = anvil.server.call('get_question', question_id)

And on the server side:

@anvil.server.callable
def get_question(question_id):
  return app_tables.questions.get(question_id=question_id)

This is a simplified picture. Here is a few things that would change in the real world:

  • The server checks whether the current user has the permission to access to that row
  • The server function often returns a modified version of the row, maybe converting it to a dic
  • Or, if returning the row itself, it can give write permission to the form, so the write back part of the databinding works
  • Otherwise (if returning a dict rather than the row tiself) there will be an explicit function on the client side that calls another server function when it’s time to write back to the database

Thanks for that info Stefano
I am able to get a column in the table to be returned in a repeating panel but I am not looking for the entire column,only the first entry in the column.
Sorry I’m new at this,so if I’m sounding dumb it’s probably because I am

what do you mean by the first entry in the column?

The table at the moment has 4 columns first name ,surname,age, date.
lets say the first name is peter and the second name tim.
I want the repeating panel to be populated with only the name tim for example or perhaps only tim’s age
I hope this makes sense

self.repeating_panel.items contains a list of dictionaries or table rows.

The repeating panel creates an instance of its template form for each item of its items, so each instance’s item is a dictionary or a row.

Any component in the template form can use self.item['first name'] or self.item['age'] to bind the value of its text property to the member of the dictionary or the column of the row stored in the form’s item property.

So, if you need only one or two columns, just add only one or two labels / textboxes / whatever component you want to use.

Am I missing something?

Thank you Stefano
I appreciate your help.

Yes I understand that first name will give me all the names in the first column but what if I only want one of the names in that column.

I don’t understand what you mean.

The table has rows.
You get one with row = app_tables.some_table.get(key_column=key), then you get the value of one column of that row with print(row['column_name]).
Or you get a list of rows with rows = app_tables.some_table.search(), then you get the value of one column of one specific row, for example the second one, with print(list(rows)[1]['column_name]).

Why don’t you follow the template of the forum for new questions and say what you want to do, what happens instead, what you have tried and why it doesn’t work? It’s easier to understand that way than by describing it.