One-to-one Data Table server code

I am brand new to Python, and Anvil, so please excuse me if this is a very dumb question, but Google and the documentation here hasn’t helped me.

I have an application where there are Projects, and inside each Project there are Interviews. I used the To-do list example to build a page where I can add new projects and display existing projects in a repeating panel. If I click on the Project_name I then open a new Form, and I have set up a new repeating panel where I want to display any interviews that belong to this Project.

Reading the notes I see I can do this to find a row:

# Get Jane Smith's row from the table
jane_smith = app_tables.people.get(Name="Jane Smith")

Which is great, but I can’t hard-code the Project_name into the server code - so how do I pass the current project name from the client side code to the server, look up the interviews that are linked to that project, and return those to the client to display in the repeating panel?

I’ve got this code at the moment:

class Project_Detail_Page(Project_Detail_PageTemplate):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    self.project = properties['Project']
    self.project_name.text = self.project['project_name']
    interviews = anvil.server.call('get_interviews', 'project_name')
    self.repeating_panel_1.items = interviews

That’s on the client side, and I’m hoping to find the right rows on the server using this method:

@anvil.server.callable
def get_interviews(my_project):
  this_project = app_tables.projects.search(project_name='my_project')
  return app_tables.interviews.search(Project="this_project")

I have hacked around with the above get_interviews method until it’s ended up as this ugly mess, but I have no idea how I’m supposed to search the interviews table to find only rows that belong to the right project, nor how I can send the right details about the project from the client side to the server in the first place.

Can anyone help? This strikes me as something that’s incredibly basic functionality, but whatever I’m searching for is not throwing up useful answers.

FYI, if it helps, the error I get from this current code is:

anvil.tables.TableError: Column 'Project' can only be searched with a Row from 'Projects' table (not a string) in table 'Interviews'

Welcome to Anvil!

I’m sorry to say though, that it is pretty basic and I’m not sure you’re ready to do it quite yet. Understanding basic Python coding is really a prerequisite for using Anvil. Working through a basic free course is going to be a much better use of your time than Googling and hacking around in the dark. (I used the mobile app Sololearn, myself.) Check out https://anvil.works/docs/overview/help#python or other suggestions you can find on the forum.

I can tell you, for starters, that you are confusing variable names with strings when you pass 'project_name' to the server function and "this_project" to the interviews.search method. But getting you all the way to working code without doing it for you seems difficult.

Maybe someone else will have a more helpful response. Otherwise, my unsolicited advice is to take a couple days to learn a bit more basic Python/coding, and then come back to this.

Thanks hugetim - one of the challenges I’ve got is that I’m following the tutorials here but not really learning anything; I’m just copy/pasting code in without understanding how it works.

I have some basic (old) programming experience in other languages, so this strikes me as something that should be really basic, yet in Anvil I’m struggling to work out how to connect the client side code (e.g. my Page that is displaying content relevant to a single Project) with server side code (using the single Project attributes to search a linked table for the linked interviews).

I was looking at some YouTube Python lessons, but don’t really know which lesson I need to watch - some is really basic (introducing Objects, for example) and others too complex (building whole apps before I know what’s what).

My issue is that I lack the vocabulary to search for the right tutorial, as you point out variable names and strings etc are things I’m aware of, but can’t use properly when searching for help.

1 Like

I seem to have found the issue:

Server code:

@anvil.server.callable
def get_interviews(my_project):
  return app_tables.interviews.search(Project=my_project)

client code:

 self.init_components(**properties)
    self.project = properties['Project']
    self.project_name.text = self.project['project_name']
    interviews = anvil.server.call('get_interviews', self.project)
    self.repeating_panel_1.items = interviews

This is now letting me find the linked table rows and display them on the client side.

1 Like

Nice! You were closer than I thought. I may have just been a little tired last night (in my time zone).

I remember when I was getting started I found it very puzzling to pass the name of the server function to anvil.server.call() as a string. I can imagine that could have contributed to your initial confusion.

This is actually a good thing, you are pretty much the target audience for learning python faster than you would probably think possible.
Once you get to the ah-ha of how the language self-referentially follows similar (what is called pythonic) patterns of its own construction, you will be able to apply all the concepts of mostly any other language that you have used in the past. (objects, classes, functions, loops, arrays, linked-lists, logical expressions and operators, etc.)

Check out this thread where @david.wylie asked for suggestions for starting from a similar point in technical ability:

1 Like

Yes, I think it’s me not knowing when to use the parentheses and when to use the object itself - it seems odd to use parentheses to call the function to me - thanks for helping.

1 Like

Nice, thanks. It does seem like a relatively intuitive language, so hopefully I can grasp the basics quickly.

I think you mean double quotes instead of parentheses, but we’re on the same page. :slightly_smiling_face: You’re welcome!

1 Like