Chapter 3: Responding to events

Now that our Extension is connected to our dashboard, let’s use an event handler to respond to when user selects a mark on the dashboard.

https://extension-documentation.s3.amazonaws.com/tutorials/chat/eventhandler.gif

Tip

An event handler is a method that is called whenever an event occurs, for example, when the user makes a selection on the dashboard. You can create an event handler using the register_event_handler method from Anvil X.

In the code pane for Main, create and register the event handler using the following code (changes highlighted).

 1from ._anvil_designer import MainTemplate
 2from anvil import *
 3from anvil.tables import app_tables
 4from anvil import tableau
 5
 6from trexjacket.api import get_dashboard
 7dashboard = get_dashboard()
 8
 9class Main(MainTemplate):
10  def __init__(self, **properties):
11    self.init_components(**properties)
12    dashboard.register_event_handler('selection_changed', self.selection_changed_event_handler)
13
14  def selection_changed_event_handler(self, event):
15    user_selection = event.worksheet.get_selected_marks()
16    print(f"Got a selected record: {user_selection}, with length: ({len(user_selection)})")

register_event_handler takes 2 arguments:

  1. The kind of event we want to respond to ('selection_changed' in this case)

  2. The method that will execute when the event occurs (self.selection_changed_event_handler)

Now that we’ve created and registered our event handler, let’s test it out! Reload your extension in Tableau and click on a state. Return to the Anvil IDE and see the output in the “Tableau Output 1” pane (see gif).