Hash routing change role of content_panel on load

I have started using the hashrouting addon maintained by @stucork. It’s awesome. There is one issue that I’m trying to get around however.

The empty content panel on the startup form changes roles depending on what form is put into the content_panel. The main routing form has one role, and most other forms have a different role.

This isn’t an issue when you rout to the startup page and then click links. I can use

anvil.get_open_form().content_panel.role = …

inside of a button click method.

However, if I put

anvil.get_open_form().content_panel.role = …

inside the initializer and then link directly to that page’s hash then I get an error that tells me that anvil.get_open_form() is returning None.

Any ideas?

That’s built into how Anvil forms work. In the __init__ function the form you’re about to show doesn’t exist yet as a form, so get_open_form() will return the previous form (which is still open until after the __init__ of the new form finishes). For routing the open form is your routing template, so it won’t actually change, but the concept is the same.

When you link directly to that page’s hash, there’s no open form setup yet when the form’s __init__ runs.

In general you’d do that sort of code in a form show event handler, but that would cause the form to appear with the old role, and then get changed, which probably isn’t what you want.

You might be able to set a property on the form and then fetch that in your main routing template’s on navigate handler? Not the sort of thing I’ve done before, but by the time navigation happens all the forms should be created.

This motivated me to use

routing.get_url_hash()

on the startup form. If it’s an empty string (the main routing form) then set the role to be one value, otherwise it’s something else.

1 Like