Indexing troubles

What I’m trying to do:
I am trying to get the question_index value corresponding to the question_column value to be saved in a data table

I have a data table (question_choices) with a column, question_index (which has a number for each question), it also has a column, question_column ( displaying each question).
the questions are shuffled as below and is called when continue_button is shown on screen.

 def form_show(self, **event_args):
    """This method is called when the Button is shown on the screen"""
    self.my_list =list(app_tables.questions_choices.search())
    random.shuffle(self.my_list)
    self.index=(0)

I have a function that calls (self.button_click) when button_1 is clicked.button _1 is visible and therefore clicked before continue_button( this sets the first question to be displayed)

  
  def hide_label(self):
    if self.label_12.visible == True:
      self.column_panel_2.visible = False
      self.label_3.visible = False  
      self.label_1.visible = False 
      self.label_9.visible = False
    else:
      self.label_3.visible = True
      self.column_panel_2.visible = True
      self.continue_button.visible = True
      self.label_1.visible = True
      self.button_click()
      self.label_9.visible = True
      

def button_click is again called but by, continue_button which displays the next question on label_2 when continue_button is clicked.

    
  def button_click(self):
    try:
     
      row = self.my_list[self.index]      
      q  = row['question_index']
      self.label_2.text = row['question_column']
      self.label_4.text = row['Column1']
      self.label_5.text = row['Column2']
      self.label_6.text = row['Column3']
      self.label_7.text = row['Column4']
      self.label_8.text = row['Column5']
      self.label_9.text = row['Column6']      
      self.label_10.text = row['Column7']
      self.index +=1

this code is also called when continue_button is clicked(in order to send values to data table)
q is the value of (question_index)

new_user = list(app_tables.users.search())         
    row = users.get_user(new_user)
    y = row['email']
   
    
    for self.radio_button in self.column_panel_2.get_components():
      if self.radio_button.selected:       
        add_select = self.radio_button.value 
        email = y
        user_place = list(app_tables.users.search())
        row = (anvil.users.get_user())
        z = row ['position']
        anvil.server.call('log_answer',add_select,email,z,q)
    self.radio_button_reset()   

My problem is that the value displayed in data table is always the next question.So the indexing for q is always corresponding to the next question and not the current question_column value.

In the continue_button_click code, can you change the order? First log the answer and then call button_click function to update the display with the next question.

I have been trying for hours but still cant get it

  
  def continue_button_click(self, **event_args):
    """This method is called when the button is clicked"""
    self.find_index()
    
    
  def find_index(self):
    try:
      self.my_list = list(app_tables.questions_choices.search())
      row = self.my_list[self.index]      
      l = row['question_index']
      print(l)                                                      # does not print the correct index
      
    except Exception as e:
      print (e)
#       
      
    self.warning.visible = False
    self.warning_on()

    for self.radio_button in self.column_panel_2.get_components():
      if self.radio_button.selected == True:
        self.button_click()
         
    
  def button_click(self):
    try:
     
      row = self.my_list[self.index]      
      q = row['question_index']                                  #This prints the correct index
      self.label_2.text = row['question_column']
      self.label_4.text = row['Column1']
      self.label_5.text = row['Column2']
      self.label_6.text = row['Column3']
      self.label_7.text = row['Column4']
      self.label_8.text = row['Column5']
      self.label_9.text = row['Column6']      
      self.label_10.text = row['Column7']
      self.index +=1
      if self.index==5:
        navigation.go_hobbies()   
    except Exception as e:
      print (e)

Am I on the correct path
any guidence will be greatly appreciated

I don’t understand the purpose of this line at the start of find_index:

self.my_list = list(app_tables.questions_choices.search())

self.my_list has already been defined in form_show. Redefining it at the start of find_index gives you the questions in a different sort order, so that the question at self.index will be different from what is displayed in `label_2’.

1 Like

I have removed the problematic line but the value printed is still not correct ie. the value does not correspond to the question

I also tried to call the self.find_index function when the first question is shown on the screen button_1_click but still get the incorrect value


def continue_button_click(self, **event_args):
“”“This method is called when the button is clicked”""

self.find_index()

def find_index(self):
try:
row = self.my_list[self.index]
q = row[‘question_index’]
print(q) #this is not the correct value ie doesn’t correspond with question

except Exception as e:
  print (e)
   
  
self.warning.visible = False
self.warning_on()

for self.radio_button in self.column_panel_2.get_components():
  if self.radio_button.selected == True:
    self.button_click()

def button_click(self):
try:

  row = self.my_list[self.index]      
  q = row['question_index']
  self.label_2.text = row['question_column']
  self.label_4.text = row['Column1']
  self.label_5.text = row['Column2']
  self.label_6.text = row['Column3']
  self.label_7.text = row['Column4']
  self.label_8.text = row['Column5']
  self.label_9.text = row['Column6']      
  self.label_10.text = row['Column7']
  self.index +=1
  if self.index==5:
    navigation.go_hobbies()   
except Exception as e:
  print (e)

find user email and return radio_button selection to data table

finds email position and returns value to button_capture_from_user, column 2

new_user = list(app_tables.users.search())         
row = users.get_user(new_user)
y = row['email']


for self.radio_button in self.column_panel_2.get_components():
  if self.radio_button.selected:       
    add_select = self.radio_button.value 
    email = y
    user_place = list(app_tables.users.search())
    row = (anvil.users.get_user())
    z = row ['position']
    anvil.server.call('log_answer',add_select,email,z,q)
self.radio_button_reset()

Now I guess it’s that you increase self.index by one after displaying it. So then when you access it from the continue button click, self.index corresponds to the next question, not the current question. So changing the first find_index line to row = self.my_list[self.index-1] should do the trick.

If not, are you able to share a clone link? (You could send it to me in a private message if you don’t want to share it publicly.)

No unfortunately it is still one out
How do I private message you

When you go to reply, like on this thread, there should be a little reply arrow icon in the upper left corner. If you click that, it gives you a couple options, one of which is to send a direct message to me.

Another way is to click my name and look for a way to “send message” that way.

edit: We continued troubleshooting via DM.

2 Likes