We’ll use a Server Module to retrieve a list of articles from the Data Table.

We’re using a Server Module because code in Server Modules can be trusted, and we might want to add authentication or other logic to this retrieval code. We recommend doing it this way for all database-backed apps.

Add this function to your Server Module:

# In your Server Module
@anvil.server.callable
def get_articles():
  # Get a list of articles from the Data Table, sorted by 'created' column, in descending order
  return app_tables.articles.search(
    tables.order_by("created", ascending=False)
  )

We want to add this list to our Repeating Panel in order to display the articles on the Homepage.

We’ll define a function called refresh_articles() to do this. We can re-use this function in a number of places - we’ll also call it after we update or delete an article, to refresh the content on the Homepage, so we call it refresh_articles(). (You can read more about suggested Anvil naming conventions here.)

Go back to your Homepage Form and add this function:

  def refresh_articles(self):
    # Load existing articles from the Data Table, and display them in the RepeatingPanel
    self.articles_panel.items = anvil.server.call('get_articles')

We want to load our news articles from the Data Table when the Homepage is opened, so let’s call our refresh_articles function when the Homepage loads.

Add this line to the bottom of the __init__ method of your Homepage Form:

  # Any code you write here will run when the form opens.
  self.refresh_articles()

We also want to call refresh_articles() after we Create a new article, so the new article shows up on our Homepage. Modify your add_article_button_click to look like this:

  def add_article_button_click(self, **event_args):
    # Initialise an empty dictionary to store the user inputs
    new_article = {}
    # Open an alert displaying the 'ArticleEdit' Form
    save_clicked = alert(
      content=ArticleEdit(item=new_article),
      title="Add Article",
      large=True,
      buttons=[("Save", True), ("Cancel", False)]
    )
    # If the alert returned 'True', the save button was clicked.
    if save_clicked:
      anvil.server.call('add_article', new_article)

    self.refresh_articles()

Run your app. You should now see any articles you’ve added listed on the homepage!