Scenario class
Bases: _Entity
, Submittable
, _Labeled
Instance of a Business case to solve.
A scenario holds a set of tasks (instances of Task
class) to submit for execution in order
to solve the Business case. Each task can be connected to some data nodes as input or output
forming an execution graph. The scenario can be submitted for execution, and the tasks will
be orchestrated to solve the Business case.
A scenario also holds a set of additional data nodes (instances of DataNode
class) for
extra data related to the scenario.
Note
It is not recommended to instantiate a Scenario
directly. Instead, it should be
created with the create_scenario()
function.
Example
import taipy as tp
from taipy import Config
def by_two(x: int):
return x * 2
if __name__ == "__main__":
# Configure scenarios
input_cfg = Config.configure_data_node("my_input")
result_cfg = Config.configure_data_node("my_result")
task_cfg = Config.configure_task("my_double", function=by_two, input=input_cfg, output=result_cfg)
scenario_cfg = Config.configure_scenario("my_scenario", task_configs=[task_cfg])
# Create a new scenario from the configuration
scenario = tp.create_scenario(scenario_cfg)
# Write the input data and submit the scenario
scenario.my_input.write(3)
scenario.submit()
# Read the result
print(scenario.my_result.read()) # Output: 6
# Retrieve all scenarios
all_scenarios = tp.get_scenarios()
Attributes¶
additional_data_nodes
property
writable
¶
additional_data_nodes: Dict[str, DataNode]
The dictionary of the scenario's additional data nodes.
Additional data nodes are data nodes that are not part of the scenario's graph but are used to store extra data. They are not connected to the scenario's tasks.
creation_date
property
writable
¶
creation_date: datetime
The date and time of the scenario's creation.
id
instance-attribute
¶
id: ScenarioId = scenario_id or _new_id(config_id)
The unique identifier of this scenario.
is_primary
property
writable
¶
is_primary: bool
True if the scenario is the primary of its cycle. False otherwise.
sequences
property
writable
¶
sequences: Dict[str, Sequence]
The dictionary of the scenario's sequences.
The sequences are subsets of tasks that can be submitted together independently of the rest of the scenario's tasks.
subscribers
property
writable
¶
subscribers: _ListAttributes
The list of callbacks to be called on Job
's status change.
version
property
¶
version: str
The application version of the scenario.
The string indicates the application version of the scenario. If not provided, the latest version is used.
Methods¶
add_sequence() ¶
add_sequence(
name: str,
tasks: Union[List[Task], List[TaskId]],
properties: Optional[Dict] = None,
subscribers: Optional[List[_Subscriber]] = None,
) -> None
Add a sequence to the scenario.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name of the sequence. |
required |
tasks |
Union[List[Task], List[TaskId]]
|
The list of scenario's tasks to add to the sequence. |
required |
properties |
Optional[Dict]
|
The optional properties of the sequence. |
None
|
subscribers |
Optional[List[_Subscriber]]
|
The optional list of callbacks to be called on
|
None
|
Raises:
Type | Description |
---|---|
SequenceTaskDoesNotExistInScenario
|
If a task in the sequence does not exist in the scenario. |
SequenceAlreadyExists
|
If a sequence with the same name already exists in the scenario. |
add_sequences() ¶
add_sequences(
sequences: Dict[str, Union[List[Task], List[TaskId]]]
) -> None
Add multiple sequences to the scenario.
Note
To provide properties and subscribers for the sequences, use Scenario.add_sequence
instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sequences |
Dict[str, Union[List[Task], List[TaskId]]]
|
A dictionary containing sequences to add. Each key is a sequence name, and the value must be a list of the scenario tasks. |
required |
Raises:
Type | Description |
---|---|
SequenceTaskDoesNotExistInScenario
|
If a task in the sequence does not exist in the scenario. |
add_tag() ¶
add_tag(tag: str) -> None
Add a tag to this scenario.
If the scenario's cycle already have another scenario tagged with tag the other scenario will be untagged.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tag |
str
|
The tag to add to this scenario. |
required |
data_nodes_being_edited() ¶
data_nodes_being_edited() -> Set[DataNode]
Return the set of data nodes that are being edited.
Returns:
Type | Description |
---|---|
Set[DataNode]
|
The set of data nodes that are being edited. |
get_inputs() ¶
get_inputs() -> Set[DataNode]
Return the set of input data nodes of this submittable.
Returns:
Type | Description |
---|---|
Set[DataNode]
|
The set of input data nodes. |
get_intermediate() ¶
get_intermediate() -> Set[DataNode]
Return the set of intermediate data nodes of the submittable entity.
Returns:
Type | Description |
---|---|
Set[DataNode]
|
The set of intermediate data nodes. |
get_label() ¶
get_label() -> str
Returns the scenario simple label prefixed by its owner label.
Returns:
Type | Description |
---|---|
str
|
The label of the scenario as a string. |
get_outputs() ¶
get_outputs() -> Set[DataNode]
Return the set of output data nodes of the submittable entity.
Returns:
Type | Description |
---|---|
Set[DataNode]
|
The set of output data nodes. |
get_simple_label() ¶
get_simple_label() -> str
Returns the scenario simple label.
Returns:
Type | Description |
---|---|
str
|
The simple label of the scenario as a string. |
has_tag() ¶
has_tag(tag: str) -> bool
Indicate if the scenario has a given tag.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tag |
str
|
The tag to search among the set of scenario's tags. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the scenario has the tag given as parameter. False otherwise. |
is_deletable() ¶
is_deletable() -> ReasonCollection
Indicate if the scenario can be deleted.
Returns:
Type | Description |
---|---|
ReasonCollection
|
True if the scenario can be deleted. False otherwise. |
is_ready_to_run() ¶
is_ready_to_run() -> ReasonCollection
Indicate if the entity is ready to be run.
Returns:
Type | Description |
---|---|
ReasonCollection
|
A ReasonCollection object that can function as a Boolean value, which is True if the given entity is ready to be run or there is no reason to be blocked, False otherwise. |
remove_sequence() ¶
remove_sequence(name: str) -> None
Remove a sequence from the scenario.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name of the sequence to remove. |
required |
remove_sequences() ¶
remove_sequences(sequence_names: List[str]) -> None
Remove multiple sequences from the scenario.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sequence_names |
List[str]
|
A list of sequence names to remove. |
required |
remove_tag() ¶
remove_tag(tag: str) -> None
Remove a tag from this scenario.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tag |
str
|
The tag to remove from the set of the scenario's tags. |
required |
rename_sequence() ¶
rename_sequence(old_name, new_name) -> None
Rename a scenario sequence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
old_name |
str
|
The current name of the sequence to rename. |
required |
new_name |
str
|
The new name of the sequence. |
required |
Raises:
Type | Description |
---|---|
SequenceAlreadyExists
|
If a sequence with the same name already exists in the scenario. |
set_primary() ¶
set_primary() -> None
Promote the scenario as the primary scenario of its cycle.
If the cycle already has a primary scenario, it will be demoted, and it will no longer be primary for the cycle.
submit() ¶
submit(
callbacks: Optional[List[Callable]] = None,
force: bool = False,
wait: bool = False,
timeout: Optional[Union[float, int]] = None,
**properties
) -> Submission
Submit this scenario for execution.
All the Task
s of the scenario will be submitted for execution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callbacks |
List[Callable]
|
The list of callable functions to be called on status change. |
None
|
force |
bool
|
Force execution even if the data nodes are in cache. |
False
|
wait |
bool
|
Wait for the orchestrated jobs created from the scenario submission to be finished in asynchronous mode. |
False
|
timeout |
Union[float, int]
|
The optional maximum number of seconds to wait for the jobs to be finished
before returning. |
None
|
**properties |
dict[str, any]
|
A keyworded variable length list of additional arguments. |
{}
|
Returns:
Type | Description |
---|---|
Submission
|
A |
subscribe() ¶
subscribe(
callback: Callable[[Scenario, Job], None],
params: Optional[List[Any]] = None,
) -> None
Subscribe a function to be called on Job
status change.
The subscription is applied to all jobs created from the scenario's execution.
Note
Notification will be available only for jobs created after this subscription.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable[[Scenario, Job], None]
|
The callable function to be called on status change. |
required |
params |
Optional[List[Any]]
|
The parameters to be passed to the callback. |
None
|
unsubscribe() ¶
unsubscribe(
callback: Callable[[Scenario, Job], None],
params: Optional[List[Any]] = None,
) -> None
Unsubscribe a function that is called when the status of a Job
changes.
Note
The function will continue to be called for ongoing jobs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable[[Scenario, Job], None]
|
The callable function to unsubscribe. |
required |
params |
Optional[List[Any]]
|
The parameters to be passed to the callback. |
None
|
update_sequence() ¶
update_sequence(
name: str,
tasks: Union[List[Task], List[TaskId]],
properties: Optional[Dict] = None,
subscribers: Optional[List[_Subscriber]] = None,
) -> None
Update an existing sequence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name of the sequence to update. |
required |
tasks |
Union[List[Task], List[TaskId]]
|
The new list of scenario's tasks. |
required |
properties |
Optional[Dict]
|
The new properties of the sequence. |
None
|
subscribers |
Optional[List[_Subscriber]]
|
The new list of callbacks to be called on |
None
|
Raises:
Type | Description |
---|---|
SequenceTaskDoesNotExistInScenario
|
If a task in the list does not exist in the scenario. |
SequenceAlreadyExists
|
If a sequence with the same name already exists in the scenario. |