Skip to content

Track activities and Trigger actions

In this section, we explore how to track the main application activity (on server side) and trigger actions to refresh the user interface (on client side).

What is an Event?

Taipy, particularly its scenario and management capabilities, is designed to be natively multi-user and asynchronous. This means you cannot control when an action is completed (e.g., an end-user creating a scenario, a submission failing, a data node being edited, a job completing). To handle these situations, Taipy emits events that you can consume and react to. An event is a message emitted by Taipy when something occurs in the system. It is represented by an object of class Event. An event contains attributes necessary to identify the change.

Scenario creation event

For example, when a scenario is created, an event is emitted with the following attributes:

Events are particularly useful for:

  • Updating a user interface content (e.g., refreshing a list of scenarios when a new one is created) for all users
  • Notifying end-users (e.g., sending a GUI notification when a job fails)
  • Triggering actions (e.g., automatically submitting a scenario when its input is updated)
  • And more...

For more details, see the event description page.

How to process events?

The overall principle is to register custom callbacks to event topics. The EventProcessor service listens for events matching the topics and triggers the callback execution when such an event is produced. This allows you to react to events in a Taipy application, whether you want to update the user interface or perform some server-side processing.

To process events, follow these steps:

  1. Instantiate an EventProcessor object providing the Gui instance.
  2. Register a callback to a topic using the various EventProcessor.on_event() methods. Several methods are available to register callbacks depending on your needs:

  3. Start the EventProcessor service to begin listening for events.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import taipy as tp
from taipy import Config
from taipy.event import Event, EventProcessor
from taipy.gui import Gui


# Define the functions to be executed when the event is processed.
def process_event(event: Event, gui: Gui):
    # Custom event processing logic here
    print(f"Received a {event.entity_type} {event.operation} event at : {event.creation_date}")

def broadcast_event(state, event: Event):
    # Custom event broadcast to all states
    print(f"Broadcasting event: {event.entity_type} {event.operation} at {event.creation_date}")

if __name__ == "__main__":
    # Create a scenario configuration.
    some_datanode_cfg = Config.configure_data_node("data")
    print_task_cfg = Config.configure_task("print", print, some_datanode_cfg)
    scenario_config = Config.configure_scenario("scenario", [print_task_cfg])

    # create the taipy services.
    gui = Gui()
    event_processor = EventProcessor(gui)
    event_processor.on_event(callback=process_event)
    event_processor.broadcast_on_event(callback=broadcast_event)
    event_processor.start()

    # Create a scenario, so Taipy emits an event.
    scenario = tp.create_scenario(scenario_config)
    scenario.data = "Some content."

    tp.run(gui)

This snippet shows two generic ways to process events.

In line 8, a first callback is defined to be executed for every event emitted by Taipy. The callback takes an Event object and a Gui instance as parameters. It is registered in line 25 using the EventProcessor.on_event() method.

In line 9, a second callback is defined to be broadcast to all states. It takes a state and an Event object as parameters. It is registered in line 26 using the EventProcessor.broadcast_on_event() method.

However, you might want to specify the types of events you are interested in to avoid processing all the events. For more details, see the topic description page.

For more realistic examples, see the examples page.