Using radio buttons with data tables

Hi,
I am building an app that will act as a control panel for a contact centre. The config data will be stored in an Anvil table and the contact centre will retrieve the data as a JSON dictionary - I have figured that bit out but need some help with the user front end.

When the app is opened and the form init code runs the app needs to pull various bits of data from an Anvil data table row and put the values into controls on the form from where the user can edit them then save them.

I have got this working for Labels, TextBoxes and CheckBoxes ok (I think!). I have also got it working for a group of RadioButtons but I am not convinced that I am doing it as simply as is possible.

There are three Radio Buttons which are used to control the system operating mode.

  1. Calendar
  2. Force Open
  3. Force Closed

These buttons are assigned to a group.

My form code currently uses if/elif statements to work out which of the Radip Buttons should be selected according to the operatingmode value read from the data table. This seems very clunky but I cannot work out how to simplify it. Can I use the group_name and value properties to do this?

Note I have worked out how to save the value for the selected radio button by using the get_group_value() method.

Server Code

import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
import anvil.server

@anvil.server.callable
def get_row():
  initialize_row = app_tables.services.get(servicename="Highways")
  return initialize_row

@anvil.server.callable
def save_values(trigger_update,operatingmode_update,announcepositioninqueue_update):
  update_row = app_tables.services.get(servicename="Highways")
  update_row['trigger'] = trigger_update
  update_row['operatingmode'] = operatingmode_update
  update_row['announcepositioninqueue'] = announcepositioninqueue_update

Form1 Code

from ._anvil_designer import Form1Template
from anvil import *
import anvil.server
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables

triggernumber='1000'
servicename = 'Highways'
operatingmode = 'Calendar'

class Form1(Form1Template):

  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    #self.service_name_label.text = servicename
    #self.trigger_number_box.text = triggernumber
    # Any code you write here will run when the form opens.
     
    row = anvil.server.call('get_row')
    self.service_name_label.text = row["servicename"]
    self.trigger_number_box.text = row["trigger"]
    operatingmode = row["operatingmode"]
      
    if operatingmode == 'Calendar':
      self.calendar_radio_button.selected = 'True'
    elif operatingmode == 'Force Closed':
      self.force_closed_radio_button.selected = 'True'
    elif operatingmode == 'Force Open':
      self.force_open_radio_button.selected = 'True'   
    
    self.position_in_queue_check_box.checked = row['announcepositioninqueue']

  def save_button_click(self, **event_args):
    """This method is called when the button is clicked"""
    trigger_update = self.trigger_number_box.text
    announcepositioninqueue_update = self.position_in_queue_check_box.checked
    operatingmode_update = self.calendar_radio_button.get_group_value()  
    announcepositioninqueue_update = self.position_in_queue_check_box.checked

    anvil.server.call('save_values',trigger_update,operatingmode_update,announcepositioninqueue_update)
    Notification("Values saved!").show()

Thanks for reading.

James

What you’re doing works fine. The only thing that would make it simpler (though simpler is in the mind of the observer):

button_dict={'Calendar':self.calendar_radio_button,
'Force Closed':self.forced_close_radio_button,
'Force Open':self.force_open_radiobutton}

button_dict[operatingmode].selected='True'

That makes it two lines instead of six

1 Like

Jonathan,
Thanks for responding - your code is a lot more elegant.