Running nTop Command Line in Python scripts

Objective:

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

Applies to:

  • What functions, processes, products, platforms, categories, or topics do the requestor have an issue with? What types of customers or situations is this answer relevant to? Optionally you can suggest applications where the feature or process may be useful. Frequently written as bulleted lists.

Procedure:

  • 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"))

 

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?

Comments

0 comments

Please sign in to leave a comment.