Adjust Page Title

In this tutorial we are going to change the MainForm title into a breadcrumb.

Website Breadcrumbs

Breadcrumbs are a web design elements used for navigation. They typically display a horizontal list near the top of a page showing the path from the homepage to the current page, with each section separated by a symbol.

Planning

To achieve this, we want the MainForm title to display the following text for each component (at this stage we will only focus on the components we have linked):

  • HomeComponent → StudyM8

  • CalendarComponent → StudyM8 - Calendar

  • AddComponent → StudyM8 - Add

  • AccountComponent → StudyM8 - Account

The Code

Determine the stem

Open the MainForm in code mode. The first thing we need to do is store the value for the breadcrumb stem. We will do that in the __init__.

10  def __init__(self, **properties):
11    # Set Form properties and Data Bindings.
12    self.init_components(**properties)

Check the comment on line 11. The breadcrumb stem will be a Form property, so it should go into this section of the code. Add the highlighted code.

10  def __init__(self, **properties):
11    # Set Form properties and Data Bindings.
12    self.init_components(**properties)
13    self.breadcrumb_stem = self.label_title.text

Code explaination

  • line 13:

    • self.label_title.text → take the current text value of the lable_title on this form (MainForm)

    • self.breadcrumb_stem = → store it in the breadcrumb_stem variable

Change the title

We now need to change the title for each component. The event that will trigger this action will be link clicking. Luckily we already have event handlers for this, so we can simply add our code to these event handlers.

HomeComponent

Lets start with the HomeComponent. We need to return the title text back to the breadcrumb stem. This is important for when users return to the Home page from another page.

Adjust the link_home_click handler by adding the highlighted code below:

18  def link_home_click(self, **event_args):
19    self.content_panel.clear()
20    self.content_panel.add_component(HomeComponent())
21    self.label_title.text = self.breadcrumb_stem

Code explaination

  • line 21:

    • self.breadcrumb_stem → take the value in the breadcrumb_stem variable

    • self.label_title.text = → write it to the label_title

Other components

Next we will do the CalendarComponent. Go to the link_calendar_click handler and add the highlighted code:

18  def link_calendar_click(self, **event_args):
19    self.content_panel.clear()
20    self.content_panel.add_component(CalendarComponent())
21    self.label_title.text = self.breadcrumb_stem + " - Calendar"

Code explaination

  • line 21:

    • self.breadcrumb_stem → take the value in the breadcrumb_stem variable

    • + " - Calendar" → add - Calendar to the end of it

    • self.label_title.text = → write it to the label_title

The breadcrumb for both AddComponent and AccountComponent are the same process, so go ahead and make the changes.

Testing

Now launch your website and check that your breadcrumb changes when each component is loaded.

test

Final code state

By the end of this tutorial your code should be the same as below:

Final MainForm

 1from ._anvil_designer import MainFormTemplate
 2from anvil import *
 3from ..HomeComponent import HomeComponent
 4from ..CalendarComponent import CalendarComponent
 5from ..AddComponent import AddComponent
 6from ..AccountComponent import AccountComponent
 7
 8
 9class MainForm(MainFormTemplate):
10  def __init__(self, **properties):
11    # Set Form properties and Data Bindings.
12    self.init_components(**properties)
13    self.breadcrumb_stem = self.label_title.text
14
15    # Any code you write here will run before the form opens.
16    self.content_panel.add_component(HomeComponent())
17
18  def link_home_click(self, **event_args):
19    self.content_panel.clear()
20    self.content_panel.add_component(HomeComponent())
21    self.label_title.text = self.breadcrumb_stem
22
23  def link_calendar_click(self, **event_args):
24    self.content_panel.clear()
25    self.content_panel.add_component(CalendarComponent())
26    self.label_title.text = self.breadcrumb_stem + " - Calendar"
27
28  def link_add_click(self, **event_args):
29    self.content_panel.clear()
30    self.content_panel.add_component(AddComponent())
31    self.label_title.text = self.breadcrumb_stem + " - Add"
32
33  def link_account_click(self, **event_args):
34    """This method is called when the link is clicked"""
35    self.content_panel.clear()
36    self.content_panel.add_component(AccountComponent())
37    self.label_title.text = self.breadcrumb_stem + " - Account"