Running nTop Automate in Python scripts

Objective:

Learn how to run your nTop notebook using nTop Automate (ntopcl) in Python scripts.

Applies to:

  • Automating nTop notebooks for repeatable and intelligent workflows
  • Interacting with external software suites and databases

Procedure:

  • Prepare the nTop notebook and JSON input for running nTop Automate
  • Python dictionary data can be saved as a JSON file using Python’s json module
  • Execute the nTop Automate process using Python’s subprocess module
  • Python Script_JSON.py will run a Windows process command like
    • "C:/Program Files/nTopology/nTop Platform/nTopCL.exe" -j input.json -o out.json SampleFile.ntop
  • Check if the return messages are printed as below.
[info] Logging Engine started
[info] Login successful[info] Parasolid started
[info] Parasolid started
[info] nTop successfully built.
[info] Logout successful

 Feel free to change this script to batch process your workflow by passing input lists in a For Loop!

#Imports
import os
import subprocess 
import json 

#Assuming this script, ntop file, and json files will be in the same folder
Current_Directory = os.path.dirname(os.path.realpath('__file__')) 
exePath = r"C:/Program Files/nTopology/nTopology/nTopCL.exe"  #nTopCL path
nTopFilePath = r"SampleFile.ntop"   #nTop notebook file name
Input_File_Name = "input.json"      #JSON input file name to be saved as
Output_File_Name = "out.json"       #JSON output file name to be saved as

#Input variables in JSON structure
Inputs_JSON = {
    "inputs": [
        {
        	"name": "Output directory",
        	"type": "text",
        	"value": "C:\\Users\\Your Account\\Your Path\\"
       	},
       	{
            "name": "Length",
            "type": "scalar",
            "values": 7,
            "units": "mm"
        },
       	{
            "name": "Width",
            "type": "scalar",
            "values": 2,
            "units": "mm"
        },
       	{
            "name": "Height",
            "type": "scalar",
            "values": 2,
            "units": "mm"
        }
    ]
}

#nTopCL arguments in a list
Arguments = [exePath]               #nTopCL path
Arguments.append("-j")              #json input argument
Arguments.append(Input_File_Name)   #json path
Arguments.append("-o")              #output argument
Arguments.append(Output_File_Name)  #output json path
Arguments.append(nTopFilePath)      #.ntop notebook file path

#Creating in.json file
with open(Input_File_Name, 'w') as outfile:
    json.dump(Inputs_JSON, outfile, indent=4)

#nTopCL call with arguments
print(" ".join(Arguments))
output,error = subprocess.Popen(Arguments,stdout = subprocess.PIPE, 
stderr= subprocess.PIPE).communicate() #Print the return messages print(output.decode("utf-8"))

 

And that’s it! You’ve successfully run your Notebook using Python scripts

Are you still having issues? Contact the support team, and we’ll be happy to help!

Download the Example file:

More on this topic:

Keywords:

 ntopcl command line python api script run scripts code cl 
Was this article helpful?