How do I run a Python Script in a Java web application?
Question: I need to run a Python script (write input and read output) inside my Java
application that will eventually be uploaded onto the web. How do I do this
such that it is compatible with the web? I've tried things like `Jython` and
`Runtime.exec()` in Java and I think both require `Python` to be installed on
the computer (correct me if I'm wrong) but I want the app to be run by anyone
on the web.

The Python script imports `win32com.client` to operate on a `COM` object. It
reads in a .csv file, runs the external software, then writes a .csv file
using the methods RCSV(...), Run(...) and WCSV(...). Instead of a .csv file, I
would like this data to be accessed from my Java app directly. This is my
python script in full for reference:

    
    
    import win32com.client
    from win32com.client import VARIANT
    import csv
    
    # This will import VT_VARIANT
    import pythoncom
    
    #dictionary function designed to read .csv file
    def RCSV(address):
            input=[]
            csv_reader = csv.DictReader(open(address, 'r'), delimiter=',', quotechar='"')
            headers = csv_reader.fieldnames
            for line in csv_reader:
                    for i in range(len(csv_reader.fieldnames)):
                            input.append(line[csv_reader.fieldnames[i]])
            InVal=[]
            for i in range(int(len(input)/len(headers))):
                    InVal.append([])
            for i in range(len(InVal)):
                    for j in range(i*len(headers), (i+1)*len(headers)):
                            InVal[i].append(input[j])
            return InVal
    
    #dictionary function which writes a .csv file given its address
    def WCSV(address, output, headers):
        with open(address, 'w') as csvfile:
            writer = csv.DictWriter(csvfile, fieldnames=headers, lineterminator = '\n')
            writer.writeheader()
    
            for i in range(len(output[0])):
                writer.writerow({headers[x]: output[x][i] for x in range(len(headers))})
    
    def Run(InType,InDesc,InVal,OutType,OutDesc):
        FieldArray = VARIANT(pythoncom.VT_VARIANT | pythoncom.VT_ARRAY, InDesc)
        AllValueArray=[None]*len(InVal)
    
        for i in range(len(InVal)):
            AllValueArray[i]=VARIANT(pythoncom.VT_VARIANT | pythoncom.VT_ARRAY, InVal[i])
    
        object.ChangeParametersMultipleElement(InType, FieldArray, AllValueArray)
        object.RunScriptCommand("SolvePowerFlow")
        OutVal = object.GetParametersMultipleElement(OutType, OutDesc,'')
        return OutVal
    
    # This will establish the connection
    object = win32com.client.Dispatch("pwrworld.SimulatorAuto") 
    filename= r"C:\Users\janusz\Desktop\NTU microgrid topology\ICESO Scaledown microgrid.pwb"
    object.OpenCase(filename)
    
    # Reading inputs from a .csv
    ADIN='IN.csv'
    InVal = RCSV(ADIN)
    
    InType = "GEN"
    InDesc = ["BusNum", "GenID", "GenMW"]
    
    OutType = "BUS"
    OutDesc = ["BUSNUM", "BUSNAME", "BUSPUVOLT", "BUSANGLE", "BUSKVVOLT"]
    
    OutVal = Run(InType,InDesc,InVal,OutType,OutDesc)
    ADOUT='OUT.csv'
    WCSV(ADOUT,OutVal[1],OutDesc)
    
    #This will close the connection
    del object
    object = None
    


Answer: Jython works without Python being installed on the host, because it is a 100%
Java implementation of Python. That being said, only `win32` clients can run
win32 COM anything. So, that's never going to be compatible across any
platform but win32 (and possibly win64 through wow).

