Creating a Script Component
Script Component Application Scenarios
Script components are suitable for simple code processing tasks, especially problems that can be solved by a single code file. For example, you can set the function of reading local data to a script component.
The script component supports only Python scripts. It can integrate code to make code more modular and improve code readability and maintainability.
Script Component Usage Specifications
- Script language: The script component supports only Python scripts.
- Class definition: A class named Template must be defined for the script component. The class name is fixed and cannot be changed.
- Method definition: The Template class must implement two methods: __init__ and process. The names of the two methods are fixed and cannot be changed.
- Method parameters: Parameters of the __init__ and process methods cannot be added. The __init__ method has no parameter, and the process method has a parameter params_dict.
- Class initialization: The __init__ method is executed only once in the lifecycle of the script component. This method is usually used to perform some initialization operations.
- Data processing: The process method is the core of the script component and is responsible for processing data. This method can be used to obtain input data from the params_dict parameter. params_dict is a dictionary that contains all input parameters. As shown in the example comments, a and b correspond to two input parameters of the component.
- Return value: The return value of the process method must be a dictionary. This dictionary contains the processing result and all output parameters of the corresponding component. As shown in the example comments, c corresponds to an output parameter of the component.
- Encoding declaration: The encoding mode of a script component file must be declared in the file header. Generally, the declaration is as follows: # -*- coding: utf-8 -*-
- Comments: The code of the script component should contain sufficient comments to help others understand the functions and working principles of the code.
Script component example:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class Template:
def __init__(self):
# todo
pass
def process(self, params_dict):
# Obtain input parameters.
# a = params_dict.get("a")
# b = params_dict.get("b")
# todo
# return {"a": a}
return None
Creating a Script Component
- Log in to ModelArts Studio and access the required workspace.
- In the navigation pane, choose Vertical Application Development > Application Components > Component Management. On the Customization tab page, click Create Component in the upper right corner, set the algorithm type to Script Component, and click OK.
Figure 1 Script components
- On the Create Script Component page, set basic information by referring to Table 1.
Table 1 Parameters for creating a script component Parameter
Description
English Name
English name of a component.
Chinese name
Chinese name of a component.
Service type
Service scenario used to distinguish script components. The options are as follows:
Data read/write, data preprocessing, data postprocessing, feature processing, optimization scenario definition, mechanism model, process control, image component, video component, and other
Industry
Industry type of a script component. Components can be filtered by industry type. The options are as follows:
Automotive, government, transportation, electric power, steel, mining, oil and gas, and heating
Tags
- Bind tags to a component. You can bind existing or custom tags to a component. A maximum of 10 tags can be bound to a component.
- You can create tags and delete existing tags. A maximum of 200 tags can be created in a single workspace.
- Create a tag: Enter a tag name in the text box and press Enter.
Figure 2 Creating a tag
- Delete a tag: Click X next to a specified tag from the drop-down list and click OK.
Figure 3 Deleting a tag
CAUTION:If you delete a tag that is associated with a component, the association will also be deleted. The deleted tag cannot be restored. Exercise caution when deleting tags.
- Create a tag: Enter a tag name in the text box and press Enter.
Figure 4 Creating a script component
- After setting the basic information, set the attributes by referring to Table 2.
Attribute parameters have multiple functions. For example, during static application orchestration, multiple components need to be integrated to implement a function. It is essential to clearly define the reference relationships between input and output parameters for each component to facilitate seamless data transfer among them.
Table 2 Attribute parameter description Parameter
Description
English Name
English name of a parameter.
Chinese name
Chinese name of a parameter.
Category
- Input parameter: indicates input parameters of the node during application orchestration. The input parameters can be set on the GUI, output parameters of other component nodes as the input parameters of the current node or canvas parameters.
- Output parameter: indicates the result generated after the component is executed. The output parameter can be used by other component nodes.
- Streaming output parameter: In scenarios such as NLP, streaming output parameters are supported and can be displayed in streaming mode. When a component is used, it must contain only one streaming output parameter and does not contain other output parameters.
- Environment variable: indicates a global variable that can be used during component operation. Environment variables can be used to store some shared configuration information. For example, if an application needs to obtain to-be-processed data from a database, the application may use environment variables to store database information such as location information, account password information, API keys.
Data Type
Value type of a component parameter.
Service Type
Service type of a parameter. This parameter is optional and is left empty by default. Two types are supported: model and model service.
Model: The OBS address of the selected model is automatically filled in the static application page after you select a model.
Model service: Select a service on the static application page to automatically fill in the access address of the selected model service so that the component service can invoke the selected model service.
Parent parameter
A parent parameter is usually used to configure a complex structure. You can use a data parameter you have added as the parent parameter of the structure. That is, the parent parameter must be a defined component parameter.
It is used to configure a complex structure. The parent parameter must be an added component parameter whose data type is object.
Default value
Default parameter value.
Valid value
Valid value range of a parameter. Example: integer: [0,10]; floating-point number: [0.1,10.1]; enumerated value: A|B|C.
Mandatory
Whether the parameter is mandatory.
is it sensitive
Whether a parameter is sensitive. Sensitive parameters cannot be entered in the canvas and can only be transferred through canvas parameters.
is it an array
Whether the parameter is an array. If a parameter is an array, it can contain multiple values. For example, a parameter named "tag" can be an array of strings that contain multiple tag names. If the parameter is not an array, it can contain only a single value.
- After setting the parameters, modify the script content and click Create immediately.
Figure 5 Modifying a script
- After the component is created, click Edit in the Operation column on the script list page to modify the parameter name, parameters, and script. Component parameters can be imported in batches. If a component has a large number of parameters and these parameters have been created in other components, you can copy the parameter code and enter the code in the component to which the parameters need to be imported by using the parameter import function. In this way, the parameters can be imported in batches.
Figure 6 Viewing component parameters
Figure 7 Importing parameters
- After the component is developed, click Save and Validate. The component can be used in Creating a Static Application.
- On the component list page, filter components by name, tag, or industry.
Figure 8 Components

Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot