Chapter 4: Getting comment details

In Chapter 4 we used get_selected_marks to get the user’s selection from the Tableau Dashboard. In this chapter, we’ll save the key fields we need to allow users to make a comment.

We want to get the name of the state and the profit from the dashboard, which are both contained in user_selection. Let’s add some code to our form that saves this information to form attributes:

 1from ._anvil_designer import Form1Template
 2from anvil import *
 3from anvil import tableau
 4
 5from trexjacket.api import get_dashboard
 6dashboard = get_dashboard()
 7
 8class Form1(Form1Template):
 9  def __init__(self, **properties):
10    self.state_name = None
11    self.profit = None
12    self.logged_in_user = None
13    self.init_components(**properties)
14    dashboard.register_event_handler('selection_changed', self.selection_changed_event_handler)
15
16  def selection_changed_event_handler(self, event):
17    user_selection = event.worksheet.get_selected_marks()
18
19    if len(user_selection) == 0:
20        self.state_name = None
21        self.profit = None
22        self.logged_in_user = None
23    else:
24        record = user_selection[0]
25        self.state_name = record['State']
26        self.profit = record['AGG(Profit Ratio)']
27        self.logged_in_user = record['logged_in_user']
28
29    msg = f"User: {self.logged_in_user}\nState: {self.state_name}\nProfit Ratio: {self.profit}"
30    Notification(msg).show()

Once you’ve added this code in the code pane of Form1, reload the extension inside Tableau and click on a few states. You should see the selection appearing as a popup in your extension!

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

In the next chapter we’ll add the functionality for a user to add their comment.