Update Repeating Panel with new data

I am making an app that has 2 basic functionalities:
(1) create event

  • I have a form that captures the event information then add an entry to my database

(2) list event

  • Pulls data from the database and display it on a Datagrid

I also have a home page with navigation bar, using the instruction here: Anvil Docs | Navigation

The issue I am facing is that when I add another event, and then go to the list event page, the latest event was not shown.

I have solved it by re-initialising the pages on my home page’s click function, seen below.
However this cause the page load to be slower. Is there a better way to do this?

class Home(HomeTemplate):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    
    self.create_event_link.tag.form_to_open=CreateEvent()
    self.list_events_link.tag.form_to_open=ListEvents()


  def nav_link_click(self, **event_args):
    """A generalised click handler that you can bind to any nav link."""
    # Refresh the pages
    self.create_event_link.tag.form_to_open=CreateEvent()
    self.list_events_link.tag.form_to_open=ListEvents()

    
    # Find out which Form this Link wants to open
    form_to_open = event_args['sender'].tag.form_to_open

    self.content_panel.clear()
    self.content_panel.add_component(form_to_open, full_width_row=True)

The definition of the form is CreateEvent while the instance of the form with its content is CreateEvent().

Similar to how you can refer to the definition of a function or to whatever is generated by executing the function:

def double(x):
    return x * 2

print(double)        # prints the definition of the function
print(double(x))     # executes the function and prints the value returned by the function

print(ListEvents)    # prints the definition of the form, a reference to the class
print(ListEvents())  # print the form itself (which is not really printable, so it actually prints something representing the form instance)

One solution would be to do:

# remove the "()", just store the definition, not the actual form
self.create_event_link.tag.form_to_open=ListEvent

# delay the creation of the form here
self.content_panel.add_component(form_to_open(), full_width_row=True)

This will ensure that the form is always regenerated from scratch. The same effect that you have by adding those two lines that you have added, but you only generate the form you actually use, not both of them.

If the form is still slow, then you may need to work on optimizing the form update. What happens when the form is created that makes it slow?

Thanks for your help :slight_smile:

It was slow because my actual app has several more pages. I only shared a subset of an app to articulate the problem.

1 Like