nickreitz
nickreitz

Reputation: 33

How do I implement Python code in a Web Browser?

I have a program that reads a csv file and filters the data in it, then writes a new csv with the filtered data. I need to make this code available and executable within a web browser, but so far the only way I've seen to do it involves some use of JavaScript. I've never tried anything like this so any information to point me in the right direction is much appreciated!! Thanks! Here's a draft of the code if that helps.

readFile    = input("--Enter name of file to be read: ")
writeFile   = input("--Enter name of file to be written: ")
with open(readFile,'r') as readFile:
    
    print("Reading File...\n")
    
    for eachline in readFile:                               # converting columns to lists
        parts = eachline.strip('\n').split(',')
        column1.append(parts[0])
        column2.append(parts[1])
      
    del column1[0]                                          # deleting the header of the column
    del column2[0]
                                                        
    for i in range(len(column1)):                           # converting strings to int/float
        column1[i]  = float(column1[i]) 
        column2[i] = float(column2[i])


###########  data filter  #####################

with open(writeFile,'w') as writeFile:
    
    print("Writing new file...\n")
    
    for x in range(len(biglist2[1])):                       # will iterate as many times as there are rows in csv
        
        for y in range(len(biglist2)):                      # will iterate as many times as there are columns in csv (3)
            
            if y == 2:                                      
                writeFile.write(str(biglist2[y][x]))
            
            else:
                writeFile.write(str(biglist2[y][x])+',')
                
        writeFile.write('\n')


print("Program Complete!\n")

Upvotes: 0

Views: 234

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54832

You don't. Web browsers don't run Python, they run Javascript. You'll have to rewrite your code. And by the way, Javascript code in a browser can't open arbitrary files like that. It would be a huge security hole.

Perhaps you should really have your HTML code upload the file to your server, where you can run your Python code and send the data back.

Upvotes: 1

Related Questions