Should I use databinding to make this more CRUD?

I have a set of radio buttons in the some group generated like this

# in __init__
self.item['selected']=get_db_answer()

...

for value in {'option1': 'option 1 text', 'option2': 'option 2 text'}:
      radio_button = RadioButton(text=radios[value],
                                 group_name=radio_group_name,
                                 value=value,
                                 tag=value,
                                 selected=(value==self.item['selected']))
      self.column_panel_1.add_component(radio_button)

I use get_group_value to later see which of the option is selected.

The way I select the radio button doesn’t feel in keeping with best practices with CRUD.

Is there a way to use Anvil’s databinding to have one of the options selected based on what is in a database without using the visual editor? Alt, is there a better way to programmatically work with radio buttons?

Should I give up on generating the questions in Python and use the visual editor?

Hi @lclu! The approach that you’ve shown looks fine to me.
Which CRUD best practices do you feel like it’s violating?

Here’s a post describing data bindings via the visual editor and code: Adding data bindings via code - #2

The end result is that data bindings are an IDE shortcut for generating the same sort of Python code you’re already using.

1 Like

It would be nice if I could somehow do two way data binding to what is selected back to item['selected'] so that all the setting and getting is done through the same binding object.

I could add a on_select event for each radio button, but it would be cleaner if there’s a way to bind group_value to the binding object.

Is that possible?

You might try this RadioGroup component: Anvil Docs | m3

Documentation here: Material 3

Announcement here:

Thanks for pointing that out! After playing around with the new theme, I quite like it. Do you know if it’s possible to switch the theme of an existing app from Material Design 3 to New M3 (Beta)?

I found out that I can import these new components into my existing app with from m3._Components.RadioGroupPanel import RadioGroupPanel