Enabling/disabling all components in a form

I have a form that opens with all components disabled to prevent accidental editing. To edit the form, the user has to hit an ‘edit’ button.

Since the form is quite large, I haven’t listed all the components, but have so far iterated recursively over them, but the performance is terrible. To somewhat alleviate this, I abandoned try/except statements and instead tested the classes of subcomponents, but the performance is still not good. So my question is: is there a way to access all components (including subcomponents of containers) without recursion, or is there any other way I could improve performance of this code?

  def enable_edit(self, component):
    self.item = anvil.server.call('get_bond_writeable', self.item)
    comp_type = str(type(component))[14:-2]
    if comp_type in ('FlowPanel','ColumnPanel', 'Link', 'DataGrid', 'RepeatingPanel', 'base.Bond.Bond'):
      for comp in component.get_components():
        self.enable_edit(comp)
    if comp_type in ('Button', 'TextBox', 'CheckBox', 'DropDown', 'TextArea', 'RadioButton'):
      component.enabled = True

Problem solved. It turned out that since all components are attributes of the form itself, I can iterate over dir(self) and test the components. All components are now enabled instantly:

  def enable_edit(self):
    for item in dir(self):
      comp = getattr(self, item)
      if str(type(comp))[14:-2] in ('Button', 'TextBox', 'CheckBox', 'DropDown', 'TextArea', 'RadioButton'):
        comp.enabled = True
3 Likes

Nice solution and super fast.

Other suggestions that do the same might be:

 def enable_edit(self):
  disablable_components = (Button, TextBox, CheckBox, DropDown, TextArea, RadioButton)
  for item in dir(self):
    comp = getattr(self, item)
    if type(comp) in disablable_components:
      comp.enabled = True
 def enable_edit(self):
  disablable_components = (Button, TextBox, CheckBox, DropDown, TextArea, RadioButton)
  for item in dir(self):
    comp = getattr(self, item)
    if any(isinstance(comp, anvil_comp) for anvil_comp in disablable_components):
      comp.enabled = True
 def enable_edit(self):
  for item in dir(self):
    comp = getattr(self, item)
    enabled = getattr(comp, 'enabled', None)
    if enabled is not None:
      comp.enabled = True
1 Like