Passing app_table.notes through a search to then link to a new form

I am trying to build a search where the results of my search populate a column panel below the textbox and the elements of the column panel are links to the student’s individual notes page which I have a form for as well. I am modifying the CRM app template. Somehow I am not passing the notes properly and after a couple of hours of fiddling I am putting it to the group.

Here is my TeacherHome form code:
from anvil import *
import anvil.server
import google.auth, google.drive
from google.drive import app_files
import tables
from tables import app_tables
from re import *

from StudentSearch import StudentSearch

class TeacherHome (TeacherHomeTemplate):
def init(self, tables, **kwargs):
self.init_components(**kwargs)

# Any code you write here will run when the form opens.

def search_btn_click (self, **event_args):
# This method is called when the button is clicked
text = self.search_box.text
students_row = anvil.server.call(‘search_students’, text)
self.notes = anvil.server.call(‘search_notes’, text)

self.student_pnl.add_component(StudentSearch(students_row, self.notes), index=0)

Here is my StudentSearch form code:
from anvil import *
import anvil.server
import google.auth, google.drive
from google.drive import app_files
import tables
from tables import app_tables

class StudentSearch (StudentSearchTemplate):

def init(self, row, notes, **kwargs):
# You must call self.init_components() before doing anything else in this function
self.init_components(**kwargs)

self.row = row
#self.notes = notes
self.notes = notes
# Any code you write here will run when the form opens.
self.searched_name_lbl.text = row['name'] 
self.searched_points_lbl.text = str(row['total_points']) + " points"

def open_link_click (self, **event_args):
# This method is called when the link is clicked

open_form("StudentNotes", self.row, self.notes)

and here are my server module functions:
@anvil.server.callable
def search_students(text):
if text is not None:
for row in app_tables.students.search():
if re.search(text, row[‘name’], re.IGNORECASE) is not None:
return row

@anvil.server.callable
def search_notes(text):
if text is not None:
return app_tables.notes

It seems to me that when you set notes in your CustomerList it was as easy as setting it to the table.

Thanks in advance

Could you do us a favour, please? Could you format that code to make it more readable. Just surround all the code with triple backticks and it will syntax highlight, indent and everything. Makes it easier to help.

Cheers,

TeacherHome

from StudentSearch import StudentSearch 

class TeacherHome (TeacherHomeTemplate):
  def __init__(self, tables, **kwargs):
    self.init_components(**kwargs)
    
    # Any code you write here will run when the form opens.

  def search_btn_click (self, **event_args):
    # This method is called when the button is clicked
    text = self.search_box.text
    students_row = anvil.server.call('search_students', text)
    self.notes = anvil.server.call('search_notes', text)
    
    self.student_pnl.add_component(StudentSearch(students_row, self.notes), index=0)

Here is my StudentSearch:

class StudentSearch (StudentSearchTemplate):
  
  def __init__(self, row, notes, **kwargs):
    # You must call self.init_components() before doing anything else in this function
    self.init_components(**kwargs)

    self.row = row
    #self.notes = notes
    self.notes = notes
    # Any code you write here will run when the form opens.
    self.searched_name_lbl.text = row['name'] 
    self.searched_points_lbl.text = str(row['total_points']) + " points"
    
  def open_link_click (self, **event_args):
    # This method is called when the link is clicked
    
    open_form("StudentNotes", self.row, self.notes)

And here is the servermodule:

@anvil.server.callable
def search_students(text):
  if text is not None:
    for row in app_tables.students.search():
      if re.search(text, row['name'], re.IGNORECASE) is not None:
        return row
      
@anvil.server.callable
def search_notes(text):
  if text is not None:
    return app_tables.notes

Thank you for the short reply I hope this format helps. Already learned something

Actually, having just asked you to do that could you publish the app and allow cloning (posting the clone link either here or PMing me)? Might be better if I can run it against your data.

Initial observation is that you don’t appear to be searching for notes, just returning the whole table. Surely you want to be tying the notes to the student?

Also (and not really relevant to your question) is there any reason you are using regex to search the table rows instead of using a search parameter? is it for the caseless search?

Broadly though, your data tables should contain a student table and a notes table, with one of the student table fields being a link to the notes table (lets call this column “notes_link”).

Then, when you search the students table, student_row['notes_link'] will contain the notes for that student, and it is this you need to pass to the form (I think), noting that it could be list.

(working through it in PM)

Still having problems getting this link to pass through the data tables. I have tried making it both client side and server side. I am getting this error now. If anyone is willing to look at this I would greatly appreciate it.

ExternalError: TypeError: Cannot read property 'component' of undefined

Fixed. Was an issue with a none being return from my regular expression search. When that was moved to server side it worked

1 Like