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.
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 .
In the code pane for Main
, create and register the event handler using the following code (changes highlighted).
from ._anvil_designer import MainTemplate
from anvil import *
from anvil.tables import app_tables
from anvil import tableau
from trexjacket.api import get_dashboard
dashboard = get_dashboard()
class Main(MainTemplate):
def __init__(self, **properties):
self.init_components(**properties)
dashboard.register_event_handler('selection_changed', self.selection_changed_event_handler)
def selection_changed_event_handler(self, event):
user_selection = event.worksheet.get_selected_marks()
print(f"Got a selected record: {user_selection}, with length: ({len(user_selection)})")
register_event_handler
takes 2 arguments:
- The kind of event we want to respond to (
'selection_changed'
in this case) - 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).