Best Practice to use Form_Show vs Init methods

some thoughts:
key principle: your form won’t be displayed until __init__ has executed.

__init__

everything in __init__ wil run and then the form will be displayed/loaded to the screen.
(assuming that it is either the main form open_form('Form1') or it has a visible parent self.content_panel.add_component(Form1())

  • if your form needs data with data bindings when it is visible put these in your __init__ method
  • calling open_form inside an __init__ method will fail
    • the other form’s init method will execute, but the current form will be displayed afterwards.
  • calling self.call_js will fail
    • the form has yet to load (but anvil.js.call_js will work for any js function that is already loaded)
  • timer events only start after __init__ and only when the form is on the screen
  • if you have a lot of data to load consider moving this out of __init__
    • instead get some data that is quick to load,
    • start a background task
    • check the background task state with a timer event.

form_show

this event is triggered after the __init__ method has executed, and when the form has been loaded onto the screen.
It is also triggered whenever a form reappears, or whenever you do self.visible = True

  • put self.call_js here
  • if you cache your forms (like HashRouting does) use the form_show event when a form reappears
3 Likes