====== Code Module ====== //Code// is used for performing more complex operations, compared to those that can be performed by the //Equation//, including communicating and retrieving data from external servers. ---- ===== Endpoint ===== The //Code// endpoint provides a RESTful interface to interact with //Code// objects in the database. It allows authenticated users to create, update, and delete their own //Code// objects. The base URL for the //Code// endpoint is [[https://datamanagerapi.solver-ai.com/api/data/code/]]. ---- ===== HTTP Methods ===== * **GET** //code//: Retrieves a list of all Code objects associated with the authenticated user. * **POST** //code//: Creates a new Code object associated with the authenticated user. * **GET** //code/{id}//: Retrieves the Code object with the specified //id//. * **PUT** //code/{id}//: Updates the Code object with the specified //id//. * **PATCH** //code/{id}//: Updates part of a Code object with the specified //id//. * **DELETE** //code/{id}//: Deletes the Code object with the specified //id//. ---- ===== Data ===== A Code can be set up via the [[https://www.solver-ai.com/api|Browsable API]] (2 - Module Management) or programmatically (more on this [[:api_clients|here]]). The parameters required for creating a //Code// module via the [[https://datamanagerapi.solver-ai.com/api/data/code/|Code Browsable API]] are: * **Name**: Unique name identifying the //Code//. * **Code**: Path to the python file. * **VariablesStringIn**: Is the list of the input variables. * **VariablesStringOut**: Is the list of the output variables. * **VectorizationIndices (optional)**: Indices used for vectorizing the //Code//. Following is an example: Name: Example Code Code: C:/test/code.py VariablesStringIn: x, y VariablesStringOut: z VectorizationIndices: 3-5 **More on VectorizationIndices** The //VectorizationIndices// work similarly to the [[api_reference:equation|Equation]] and all the other modules. In this example, //VariablesStringIn// represents the input variables to the code, which are 'x' and 'y'. The //VariablesStringOut// represents the output variables of the code, which for this case is only 'z'. The //VectorizationIndices// field is set to '3-5', indicating that this code should be vectorized over the indices 3, 4, and 5. This would be equivalent to defining three separate codes: 'code_3', 'code_4', and 'code_5'; where: * for code_3 the inputs are x_3 and y_3, and the output z_3 * for code_4 the inputs are x_4 and y_4, and the output z_4 * for code_5 the inputs are x_5 and y_5, and the output z_5 If //vectorizationIndices// is left empty, then the code will not be vectorized and the naming of the variables will remain as defined by the //VariablesStringIn// and //VariablesStringOut//. ---- ===== Code File ===== The code file frovided by you will contain a Python script. Things you should know: * The version of python supported is 3.9. * The code is run in a restriced environment and for security reasons you will not be able to use the "import" keyword to import libraries and modules. * You can use **numpy** within your code as **np** (e.g., np.cos(x)) * You can use functions: * post: POST HTTP request * get: GET HTTP request * dumps: converts a python object to a JSON string * loads: converts a JSON string into a python object The code file contains a Python script that uses the variables specified in //VariablesStringIn// as global variables and sets a value for one or more output variables specified in //VariablesStringOut//. For example, if we have //VariablesStringIn// as ‘x, y’ and //VariablesStringOut// as ‘z’, a possible Python script could be: **Code Example** if x > y: z = x else: z = y This script checks if ‘x’ is greater than ‘y’, and if so, sets ‘z’ to the value of ‘x’. Otherwise, it sets ‘z’ to the value of ‘y’. **Multiple Outputs Code Example**: If there are multiple output variables in variablesStringOut, you can set values for all of them in your Python script. For example: if x > y: z = x w = x + y else: z = y w = x - y This script sets values for both ‘z’ and ‘w’ based on the comparison between ‘x’ and ‘y’. **GET HTML Request Code Example**: response = get( 'https://server-url', headers={"Content-Type": "application/json"} ) if response.status_code == 200: response_data = response.json() z = response_data.get('z') + x + y else: z = 0 This script computes 'z' performing an HTTP request with a **get** function and adding 'x' and 'y' to it. **POST HTML Request Code Example**: data = { 'x': x, 'y': y, 'userId': 1, } response = post( 'https://server-url', data=dumps(data), headers={"Content-Type": "application/json"} ) if response.status_code == 200: response_data = response.json() z = response_data.get('z') else: z = 0 This script computes 'z' performing an HTTP request with a **post** function passing 'x' and 'y' as input data. ---- ===== Code Restrictions ===== The code is executed in a restricted environment for security reasons. Following is a list of python keywords and functions which will not be allowed, causing the code to fail: | import | np.load | | open | np.save | | print| np.savetxt | | exec | np.frompyfunc | | eval | np.vectorize | | compile | np.finfo | | input | np.iinfo | | getattr | | | setattr | | | delattr | | | global | | | globals | | | locals | | ---- ===== Permissions ===== Only authenticated users can interact with this endpoint, this can be done via the [[https://www.solver-ai.com/api|API]] page or pragrammatically via a token, which can be obtained from the [[https://www.solver-ai.com/accountmanagement|Account]] page. All //Code// created will be associated with the authenticated user, and and will not be accessible by other users. ---- ===== Notes ===== * If you attempt to delete a //Code// that is used in a problem, the request will be denied with a 403 Forbidden status code. * For information on doing the same programmatically follow the documentation relative to the [[:api_clients|API Clients]].