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

You can make m3 a dependency to have the components in the drag and drop designer

1 Like

I’m not able to import the module.

I get the error ModuleNotFoundError: No module named 'm3', I think because the app is Material Design 3 instead of New M3 (Beta).

I don’t think there’s a way to migrate over to the new theme, so I believe I’d need to write a new app. Is that true?

Is this how you have it setup in the Dependencies tab in Settings?

image

That makes it work for me.

1 Like

Anvil Docs | Material 3.

Have a look at this link.

Like @duncan_richards12 said you have to add it as a dependency THEN import it too.

3 Likes

Thank you, I’ve imported the components successfully.

Hi @lclu, looks like your issue has been solved. You can click the check button under the comment that solved your issue to mark it as the solution. This helps other users to see that this question has been answered :slightly_smiling_face:.

1 Like