Custom components don’t change their look in the designer
That may change in the future, but for now that’s how things are.
Here’s your component edited so that the one line above
visible = HtmlTemplate.visible
just works
https://anvil.works/build#clone:6RSGRONZEV2XXRYV=NVBNLABL6QZGNEOBB5TEOAH5
You may want to read this from the python documentation to go deeper
It describes descriptors which are pretty fundamental to understand what’s really going on here.
https://docs.python.org/3/howto/descriptor.html
CustomComponent class inheritance looks like this:
object
-> Component
-> Container
-> HtmlTemplate (probably or ColumnPanel)
-> MyCustomComponentTemplate
-> MyCustomComponent
If we give MyCustomComponent
an is_visible
property in the custom component dialogue
then we will inherit an is_visible
descriptor from the MyCustomComponentTemplate
class.
print(MyCustomComponentTemplate.is_visible)
# <anvil.CustomComponentProperty object>
This is a bit like python’s property
class.
It’s your basic descriptor with a __get__
and __set__
and doesn’t do much apart from store the value.
(python’s property
class is also a descriptor)
If you didn’t create your own @property
for is_visible
,
then setting is_visible
on a custom component will fallback to the MyCustomComponentTemplate.is_visible
descriptor.
This can be very useful if you don’t have any logic that needs to run in the getter/setter.
i.e. you get a basic descriptor for free.
If you need extra logic then you can create your own @property
Your current code looks like this
@property
def is_visible(self):
return self.visible
@is_visible.setter
def is_visible(self, value):
self.visible = value
You have overridden the is_visible
CustomComponentProperty descriptor (inherited from the Template class) with your own descriptor.
When you access self.visible
above where are you getting it from?
print(HtmlTemplate.visible)
# <anvil.ComponentProperty object>
HtmlTemlate has its own property descriptor that handles the visible
property.
This descriptor knows how to actually make a component visible or not.
Now let’s say we want a visible
property instead of an is_visible
property.
If we add visible
in the custom component dialogue what happens?
The Template class will now have a visible
descriptor (instead of the is_visible
descriptor).
Which by default doesn’t really do anything except store the value we set.
i.e. self.visible = True
will do nothing.
The hack for this is to get our HtmlTemplate.visible
descriptor back by adding it directly to our class.
class MyCustomComponent(MyCustomComponentTemplate):
visible = HtmlTemplate.visible # give me the real visible descriptor
The anvil extras code needs to do some business logic so instead it creates its own @property descriptor, and then directly calls the __get__
and __set__
methods for the HtmlTemplate.visible
descriptor.
(doing self.visible = True would just cause an infinite loop)
Hope that helps 
Should it be easier to do this without needing to understand about descriptors?
Almost certainly 
(the is_visible
approach is a good one)