Running nTop Command Line in Python scripts

Follow

This article explains how to run your nTop notebook using nTop Command Line (nTopCL) in Python scripts.

  • Prepare the nTop notebook and JSON input for running nTopCL
  • Python dictionary data can be saved as a JSON file using Python’s json module
  • Execute nTopCL 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/nTop Platform/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"))

 

  Download the Example Files

Sample File
Script_JSON.py

Labels: ntopcl command line python api script 
Was this article helpful?
1 out of 1 found this helpful

Comments

0 comments

Article is closed for comments.