Whoops…Nothing found

Try other keywords in your search

Creating Custom Events

 1 Minute

 0 Likes

 186 Views

A CustomEvent is an Element that gives Flow users the ability to execute a process that is independent of the Step it’s used in. This is useful for situations in which we want to provide users with the option to:

  • Open Flow data in Explore
  • Take action on insights in a Flow (e.g. order new parts for maintenance)
  • Kick-off any kind of action that isn’t automatically executed in a given Step

 

How-To

A CustomEvent is created similarly to a Step. You must create a class for your event that is a child of the CustomEvent class and then create a callback function that utilizes flow_metadata and defines what is to be executed.

from predict_backend.page.elements import CustomEvent

class MyEvent(CustomEvent):
   def callback(self, flow_metadata):
       print(f"Running MyEvent {flow_metadata}")
       
       # Place what you want to execute here
       
       return 'Executed MyEvent Custom Event'

 

Once your CustomEvent class is created, it can be instantiated in a step and used the same as any other Element.

my_event = MyEvent("Event Title", "Event Description")
page_section.add_card_w_content([my_event])

 

What to Expect (Validation)

To provide some more context, we will go through an example in which we use a CustomEvent to give users the option to open Flow data in Explore. We start by creating our new class that inherits from CustomEvent. Its callback function defines what will be run if a user decides to execute our event.

class ExploreNow(CustomEvent):
   def callback(self, flow_metadata):
       # You can put whatever python code you want here. The following is just an example
       print(f"Running ExploreNow {flow_metadata}")
       
       # Get store_interface and pyvip client
       # Within a custom event we need to create a new pyvip client object
       store_interface = StoreInterface(**flow_metadata)
       
       pyvip_client = PredictpyVIP(user=flow_metadata['user'],
                                   flow_id=flow_metadata['flow_id'],
                                   step_name=flow_metadata['step_name'])
                                   
       # Create plot in Expore using queried data from former step
       data = store_interface.get_input("Preprocessed Data")
       
       stock_dropdown_title = "Dropdown Title (Optional): Which stock do you want to do an individual analysis on?"
       stock_analyzing = store_interface.get_element_value(data_query_step.name, stock_dropdown_title)
       
       explore_data = data[data["ticker"] == stock_analyzing][["open", "close", "volume", "pct_change"]]
       
       pyvip_client.load_data(explore_data, 'SP500 Data')
       pyvip_client.plot(x='open', y='volume', z='pct_change', path=True)
       
       # Shutdown pyVIP to properly release resource
       pyvip_client.shutdown()
       
       return 'Executed ExploreNow Custom Event'

 

Now we use our CustomEvent in a Step. In this example, we’ll create an Infographic and then place the event within it.

class MyStep(Step):
   def run(self, flow_metadata, pyvip_client=None):
   
       # Get store_interface and current page
       store_interface = StoreInterface(**flow_metadata)
       page = store_interface.get_page()
       
       # Instantiate custom event
       explore_now_description = """If you're connected to Explore, hitting "Submit" will open a visualization of 
       your queried data in Explore."""
       explore_now_event = ExploreNow("Explore Now", explore_now_description)
       
       # Create recommendation infographic with custom event and add to section
       explore_configuration_link = "https://docs.virtualitics.com/get-started-with-flows/connection-to-explore"
       recommendation = InfographData(
           label="Recommended Action",
           main_text=f"""Insight Can Go Here: Load your data into Virtualitics Explore! 
           To learn more about configuring your Explore instance, go here: {explore_configuration_link}""",
           supporting_text="Additional supporting text can be added here",
           icon="info",
           _type=InfographDataType.POSITIVE
       )
       
       recommendation_infograph = Infographic(
           title="", 
           description="",
           recommendation=[recommendation],
           event=explore_now_event
       )
       
       section = page.get_section_by_title("")
       section.add_card_w_content(recommendation_infograph, "Key Insights")
       
       store_interface.update_page(page)

 

This creates the following page content:

 

When a user clicks on the blue “Explore Now” button, a window giving them an option to execute the event will appear:

 

In this specific example, when “Submit” is selected the following plot is opened in Explore:

Was this article helpful?