Have an Analytics Exchange administrator upload external Python scripts to the PYTHONPATH directory of AX Server and then call the Python scripts from your analytic scripts 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.

To run Python scripts on AX Server, you must:

  1. Install a supported version of the Python scripting language on your AX Server computer.
  2. Set the PYTHONPATH environment variable on AX Server.
  3. 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:

After you create your Analytics project in Analytics, create a Python script that you can call from an analytic script.

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 Python script from an analytic script. When the analytic script 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

In your Analytics project, create a new script to use as the analytic script you run on AX Server. This script does the following:

  1. 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.

  2. 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
DELETE
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 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
DELETE
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

Once you have authored the analytic script:

  1. In AX Client, create a collection and folder to house the Analytics project.
  2. To import the project:
    1. Right-click the folder you created and select Import.
    2. 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
      • Data
        • py
      • Related Files

From the Server Explorer of AX Client, right-click the analytic script and select Run. The Python script is executed as part of the analytic script and you can access the 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 script fails.

Results

Server explorer after running the analytic script

  • collectionName
    • folderName
      • Analysis Apps
        • ACLProjectName
          • analyticScriptName
      • Data
        • py
        • results
      • Related Files

Results table

  • Results value
  • 1
  • 4
  • 9
  • 16
  • 25
  • 36
  • 49
  • 64
  • 81
  • 100