How do I access the name of a component?

Hi, I’m just getting started with Anvil, so bear with me.

I have a panel with some links (all added through the IDE). I want to loop over them and based on their name do something with them, eg set their role.
I can find the links allright, but accessing their links produces an error. Example:

links = panel.get_components()
print([l.name for l in links])

gives:

AttributeError: 'Link' object has no attribute 'name_$rn$'

which is a bit mysterious to me.

Then I looked into tag, however that cannot be set through the IDE.
I’d rather not test for the link’s text, which would be very brittle.

Any suggestions? TIA

Marcel

Hello and welcome!

You are correct that tags are the way to go here.

You can set tags in the IDE manually (although I rarely do that), but you can also set the tags by setting data bindings accordingly. By “data binding” I mean that you can connect variables or data from DataTables to your tag property. Please see the introductory tutorials or search the docs for more information.

All this said, I usually set tags in code. So, if I have to give some identification to a set of components when the app starts up, I’ll set all their tags in the init function. You can see some discussion and tips about using tags here. It has been discussed a few times on the forum so you could give that a search as well.

Anvil relies in part on the Skulpt project and from what I have read, not being able to access the actual name of a component has to do with current limitations in Skulpt.

3 Likes

Wow, that was unexpected. Since the IDE says that the tag “set at runtime”, I would never have expected that with data bindings I could just set it to whatever code snippet (including just a string) I liked. Part of the learning process I guess…
I prefer to set as many properties through the IDE for items I add with the IDE.

Anyway your swift answer pointed me to a solution, for which I am grateful!

Marcel

My pleasure.

Good luck with your further development!