Skip to content

Commit

Permalink
Merge pull request #1050 from Avaiga/feature/#986-document-best-pract…
Browse files Browse the repository at this point in the history
…ice-on-main-script

Feature/#986 - Document best practice on main script
  • Loading branch information
trgiangdo authored Aug 14, 2024
2 parents 8cde504 + 5fb68be commit 289599f
Show file tree
Hide file tree
Showing 116 changed files with 1,737 additions and 1,679 deletions.
19 changes: 15 additions & 4 deletions docs/manuals/userman/configuration/job-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,17 @@ You can configure the *standalone* mode with the following config:
To execute multiple `Job^`s simultaneously, you can set the *max_nb_of_workers* to an
integer value greater than 1. That starts each `Job^` in a dedicated process with
*max_nb_of_workers* as the limit of concurrent processes that can run simultaneously.
The default value of *max_nb_of_workers* is 2.

!!! note
!!! warning

The default value of *max_nb_of_workers* is 2.
In *standalone* mode, when a subprocess is spawned, the code is re-executed except
for the code inside the `if __name__ == "__main__":` condition. This can lead to
unexpected behavior if the code is not protected.

Therefore, it's a good practice to protect the main code with the `if __name__ == "__main":`
condition in your Taipy application main script. By doing this, the code will not be re-executed
when the subprocesses are spawned.

For example, the following configuration will allow Taipy to run up to eight `Job^`s
simultaneously:
Expand All @@ -98,15 +105,19 @@ simultaneously:
```python linenums="1"
from taipy import Config

Config.configure_job_executions(mode="standalone", max_nb_of_workers=8)
if __name__ == "__main__":
Config.configure_job_executions(mode="standalone", max_nb_of_workers=8)
...
```

=== "TOML configuration "

```python linenums="1"
from taipy import Config

Config.load("config.toml")
if __name__ == "__main__":
Config.load("config.toml")
...
```

```toml linenums="1" title="config.toml"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import random

import taipy as tp
from taipy import Config


def random_value():
return random.randint(0, 100)

if __name__ == "__main__":
# Configure a data node, a tasks and a scenario
int_cfg = Config.configure_data_node("random_val", scope=tp.Scope.GLOBAL, default_data=12)
task_cfg = Config.configure_task("random_task", function=random_value, output=int_cfg)
scenario_cfg = Config.configure_scenario("random_scenario", task_configs=[task_cfg])

# Configure a data node, a tasks and a scenario
int_cfg = Config.configure_data_node("random_val", scope=tp.Scope.GLOBAL, default_data=12)
task_cfg = Config.configure_task("random_task", function=random_value, output=int_cfg)
scenario_cfg = Config.configure_scenario("random_scenario", task_configs=[task_cfg])

# Create a data node along with a scenario. A default value is written automatically
scenario = tp.create_scenario(scenario_cfg)
data_node = scenario.random_val

# Manually write a new value to the data node
data_node.write(100, editor="John", comment="Manual edition: 100", extra="extra data")
# Create a data node along with a scenario. A default value is written automatically
scenario = tp.create_scenario(scenario_cfg)
data_node = scenario.random_val

# Submit the scenario so that the data node is written again through a job
tp.Core().run()
scenario.submit()
# Manually write a new value to the data node
data_node.write(100, editor="John", comment="Manual edition: 100", extra="extra data")

print(data_node.edits)
# [{'timestamp': datetime.datetime(2024, 5, 14, 20, 12, 27, 652773), 'editor': 'TAIPY', 'comment': 'Default data written.'},
# {'timestamp': datetime.datetime(2024, 5, 14, 20, 12, 27, 689737), 'editor': 'John', 'comment': 'Manual edition: 100', 'extra': 'extra data'},
# {'timestamp': datetime.datetime(2024, 5, 14, 20, 12, 27, 856928), 'job_id': 'JOB_random_task_a8031d80-26f7-406e-9b31-33561d0f9ccd'}]
# Submit the scenario so that the data node is written again through a job
tp.Core().run()
scenario.submit()

print(data_node.edits)
# [{'timestamp': datetime.datetime(2024, 5, 14, 20, 12, 27, 652773), 'editor': 'TAIPY', 'comment': 'Default data written.'},
# {'timestamp': datetime.datetime(2024, 5, 14, 20, 12, 27, 689737), 'editor': 'John', 'comment': 'Manual edition: 100', 'extra': 'extra data'},
# {'timestamp': datetime.datetime(2024, 5, 14, 20, 12, 27, 856928), 'job_id': 'JOB_random_task_a8031d80-26f7-406e-9b31-33561d0f9ccd'}]

tp.Gui("<|{scenario.random_val}|data_node|>").run()
tp.Gui("<|{scenario.random_val}|data_node|>").run()
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from datetime import datetime
import taipy
from taipy import Config, Core, Gui, Scope

import pandas as pd

# Creating a data node variable to be bound to the visual element
data_node = None
import taipy
from taipy import Config, Core, Gui, Scope

if __name__ == "__main__":
# Creating a data node variable to be bound to the visual element
data_node = None

# Creating various data sources. A CSV file, an integer parameter and a datetime object
pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}).to_csv("out.csv")
parameter, date = 15, datetime.now()
Expand Down
141 changes: 75 additions & 66 deletions docs/manuals/userman/data-integration/data-node-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ part of a scenario.
import taipy as tp
from my_config import sales_history_cfg

sale_history_datanode = tp.create_global_data_node(sales_history_cfg)
if __name__ == "__main__":
sale_history_datanode = tp.create_global_data_node(sales_history_cfg)
```

!!! warning
Expand Down Expand Up @@ -73,12 +74,13 @@ passing the data node id as a parameter:
import taipy as tp
import my_config

scenario = tp.create_scenario(my_config.monthly_scenario_cfg)
if __name__ == "__main__":
scenario = tp.create_scenario(my_config.monthly_scenario_cfg)

data_node = scenario.sales_history
data_node_retrieved = scenario.sales_history
data_node = tp.get(data_node.id)
# data_node == data_node_retrieved
data_node = scenario.sales_history
data_node_retrieved = scenario.sales_history
data_node = tp.get(data_node.id)
# data_node == data_node_retrieved
```

## Get data nodes by config_id
Expand All @@ -92,21 +94,22 @@ can be directly accessed as attributes by using their config_id:
import taipy as tp
import my_config

# Creating a scenario from a config
scenario = tp.create_scenario(my_config.monthly_scenario_cfg)
if __name__ == "__main__":
# Creating a scenario from a config
scenario = tp.create_scenario(my_config.monthly_scenario_cfg)

# Access the data node 'sales_history' from the scenario
scenario.sales_history
# Access the data node 'sales_history' from the scenario
scenario.sales_history

# Access the sequence 'sales' from the scenario and
# then access the data node 'sales_history' from the sequence
sequence = scenario.sales
sequence.sales_history
# Access the sequence 'sales' from the scenario and
# then access the data node 'sales_history' from the sequence
sequence = scenario.sales
sequence.sales_history

# Access the task 'training' from the sequence and
# then access the data node 'sales_history' from the task
task = sequence.training
task.sales_history
# Access the task 'training' from the sequence and
# then access the data node 'sales_history' from the task
task = sequence.training
task.sales_history
```

Data nodes can be retrieved by using `taipy.get_entities_by_config_id()^`
Expand All @@ -119,13 +122,14 @@ nodes instantiated from the config_id provided as a parameter.
import taipy as tp
import my_config

# Create 2 scenarios, which will also create 2 trained_model data nodes.
scenario_1 = tp.create_scenario(my_config.monthly_scenario_cfg)
scenario_2 = tp.create_scenario(my_config.monthly_scenario_cfg)
if __name__ == "__main__":
# Create 2 scenarios, which will also create 2 trained_model data nodes.
scenario_1 = tp.create_scenario(my_config.monthly_scenario_cfg)
scenario_2 = tp.create_scenario(my_config.monthly_scenario_cfg)

# Get all data nodes by config_id, this will return a list of 2 trained_model data nodes
# created alongside the 2 scenarios.
all_trained_model_dns = tp.get_entities_by_config_id("trained_model")
# Get all data nodes by config_id, this will return a list of 2 trained_model data nodes
# created alongside the 2 scenarios.
all_trained_model_dns = tp.get_entities_by_config_id("trained_model")
```

## Get all data nodes
Expand All @@ -139,16 +143,17 @@ directly accessed as attributes:
import taipy as tp
import my_config

# Creating a scenario from a config
scenario = tp.create_scenario(my_config.monthly_scenario_cfg)
if __name__ == "__main__":
# Creating a scenario from a config
scenario = tp.create_scenario(my_config.monthly_scenario_cfg)

# Access all the data nodes from the scenario
scenario.data_nodes
# Access all the data nodes from the scenario
scenario.data_nodes

# Access the sequence 'sales' from the scenario and
# then access all the data nodes from the sequence
sequence = scenario.sales
sequence.data_nodes
# Access the sequence 'sales' from the scenario and
# then access all the data nodes from the sequence
sequence = scenario.sales
sequence.data_nodes
```

All the data nodes can be retrieved using the method `taipy.get_data_nodes()^`
Expand Down Expand Up @@ -177,14 +182,15 @@ the type of data node:
import taipy as tp
import my_config

# Creating a scenario from a config
scenario = tp.create_scenario(my_config.monthly_scenario_cfg)
if __name__ == "__main__":
# Creating a scenario from a config
scenario = tp.create_scenario(my_config.monthly_scenario_cfg)

# Retrieve a data node
data_node = scenario.sales_history
# Retrieve a data node
data_node = scenario.sales_history

# Returns the content stored on the data node
data_node.read()
# Returns the content stored on the data node
data_node.read()
```

To write some data on the data node, like the output of a task,
Expand All @@ -199,19 +205,20 @@ type) as a parameter and writes it on the data node:
import taipy as tp
import my_config

# Creating a scenario from a config
scenario = tp.create_scenario(my_config.monthly_scenario_cfg)
if __name__ == "__main__":
# Creating a scenario from a config
scenario = tp.create_scenario(my_config.monthly_scenario_cfg)

# Retrieve a data node
data_node = scenario.sales_history
# Retrieve a data node
data_node = scenario.sales_history

data = [{"product": "a", "qty": "2"}, {"product": "b", "qty": "4"}]
data = [{"product": "a", "qty": "2"}, {"product": "b", "qty": "4"}]

# Writes the dictionary on the data node
data_node.write(data)
# Writes the dictionary on the data node
data_node.write(data)

# Returns the new data stored on the data node
data_retrieved = data_node.read()
# Returns the new data stored on the data node
data_retrieved = data_node.read()
```

## Pickle
Expand Down Expand Up @@ -1120,19 +1127,20 @@ existing data.
import taipy as tp
import my_config
# Creating a scenario from a config
scenario = tp.create_scenario(my_config.monthly_scenario_cfg)
if __name__ == "__main__":
# Creating a scenario from a config
scenario = tp.create_scenario(my_config.monthly_scenario_cfg)
# Retrieve a data node
data_node = scenario.sales_history
# Retrieve a data node
data_node = scenario.sales_history
data = [{"product": "a", "qty": "2"}, {"product": "b", "qty": "4"}]
data = [{"product": "a", "qty": "2"}, {"product": "b", "qty": "4"}]
# Append the dictionary to the data node
data_node.append(data)
# Append the dictionary to the data node
data_node.append(data)
# Returns the new data stored on the data node
data_retrieved = data_node.read()
# Returns the new data stored on the data node
data_retrieved = data_node.read()
```

!!! warning "Supported data node types"
Expand Down Expand Up @@ -1698,17 +1706,18 @@ To get the parent entities of a data node (scenarios, sequences, or tasks) you c
import taipy as tp
import my_config
# Create a scenario from a config
scenario = tp.create_scenario(my_config.monthly_scenario_cfg)
if __name__ == "__main__":
# Create a scenario from a config
scenario = tp.create_scenario(my_config.monthly_scenario_cfg)
# Retrieve a data node
data_node = scenario.sales_history
# Retrieve a data node
data_node = scenario.sales_history
# Retrieve the parent entities of the data node
parent_entities = data_node.get_parents()
# {'scenarios': [Scenario 1], 'sequences': [Sequence 1], 'tasks': [Task 1]}
# Retrieve the parent entities of the data node
parent_entities = data_node.get_parents()
# {'scenarios': [Scenario 1], 'sequences': [Sequence 1], 'tasks': [Task 1]}
# Retrieve the parent entities of the data node
tp.get_parents(data_node)
# {'scenarios': [Scenario 1], 'sequences': [Sequence 1], 'tasks': [Task 1]}
# Retrieve the parent entities of the data node
tp.get_parents(data_node)
# {'scenarios': [Scenario 1], 'sequences': [Sequence 1], 'tasks': [Task 1]}
```
Loading

0 comments on commit 289599f

Please sign in to comment.