Running Python scripts on AX Server
Have an Analytics Exchange administrator upload external Python scripts to the PYTHONPATH directory of AX Server and then call the scripts from your analytics to leverage the object-oriented features of the Python programming language on the server. To prepare the AX Server environment to run Python scripts, you must first install Python, and then set the PYTHONPATH environment variable.
Prerequisites
To run Python scripts on AX Server, you must:
- Install a supported version of the Python scripting language on your AX Server computer.
- Set the PYTHONPATH environment variable on AX Server.
- In Analytics, create a project to work with and import into AX Server.
Note
For help completing these prerequisites, contact your Analytics Exchange administrator and see:
Create a Python script
After you create your Analytics project in Analytics, create a Python script that you can call from an analytic.
Then, give your Analytics Exchange administrator this script file to upload to the PYTHONPATH directory of the machine hosting AX Server before you call the script from an analytic. When the analytic runs on AX Server, the Python executable looks for the script in the PYTHONPATH directory, so it must be present.
Example Python file
The following example Python file contains a trivial script that uses a lambda expression to raise a number to the power of itself. This example is intended to show how Python scripts run on AX Server, not how to analyze data with Python.
Filename: lambda_example.py
# myFunc squares value1 and returns the value myFunc = lambda value1: value1**2
Create an Analytics script
In your Analytics project, create a new script to use as the analytic you run on AX Server. This script does the following:
- Opens a simple table called py with one record.
You must open a table to execute the GROUP command in Analytics, here the py table is used only for this purpose.
- Loops 10 times and in each loop, executes the Python script by passing the incrementing counter as an argument and extracting the output to a results table.
Add the analytic header
Add the appropriate analytic header tags at the beginning of the script so that the Analytics script can run on AX Server after you import your analysis app:
COMMENT //ANALYTIC Python integration test verify Python integration on AX Server //DATA py //DATA results //RESULT TABLE results END
Add the script logic
SET SAFETY OFF DEL ALL OK CLOSE OPEN py GROUP ASSIGN v_max = 11 ASSIGN v_counter = 1 LOOP WHILE v_counter < v_max EXTRACT PYNUMERIC("lambda_example,myFunc",0,v_counter) AS "Results value" TO "results.fil" v_counter = v_counter + 1 END END CLOSE py
The complete analytic script
The complete analytic that you run on AX Server looks as follows:
COMMENT //ANALYTIC Python integration test verify Python integration on AX Server //DATA py //DATA results //RESULT TABLE results END SET SAFETY OFF DEL ALL OK CLOSE OPEN py GROUP ASSIGN v_max = 11 ASSIGN v_counter = 1 LOOP WHILE v_counter < v_max EXTRACT PYNUMERIC("lambda_example,myFunc",0,v_counter) AS "Results value" TO "results.fil" v_counter = v_counter + 1 END END CLOSE py
Import the Analytics project
Once you have authored the analytic script:
- In AX Client, create a collection and folder to house the Analytics project.
- To import the project:
- Right-click the folder you created and select Import.
- Navigate to your Analytics project on your local computer, select the .acl project file, and then click Open.
Note
Make sure you import the source data files to import the py table with your Analytics project.
Server explorer after import
- collectionName
- folderName
- Analysis Apps
- ACLProjectName
- analyticScriptName
- ACLProjectName
- Data
- py
- Related Files
- Analysis Apps
- folderName
Run the analytic
From the Server Explorer of AX Client, right-click the analytic and select Run. The Python script is executed as part of the analytic and you can access the results results table from AX Web Client.
Note
When the script runs, the Python executable looks for the script file in the PYTHONPATH directory of the machine hosting AX Server. If your Analytics Exchange administrator has not uploaded the file to this directory, the analytic fails.
Results
Server explorer after running the analytic
- collectionName
- folderName
- Analysis Apps
- ACLProjectName
- analyticScriptName
- ACLProjectName
- Data
- py
- results
- Related Files
- Analysis Apps
- folderName
Results table
- Results value
- 1
- 4
- 9
- 16
- 25
- 36
- 49
- 64
- 81
- 100