Python Forum

Full Version: Open and read a tab delimited file from html using python cgi
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying build a webpage which takes a large tab delimited .txt/.txt.gz file as user input from a form and using POST method(test.html) to send the data to cgi-bin directory to file.py which ideally should open the file read and put the data into a dataframe and do some analysis(which i have already wrttien in python and works well on terminal) on the data and send back the analysis results to a html page. That is where the problem i am facing, how to read the data in the file exactly with the separator. How to pass these data into a pandas dataframe with delimiter?

test.html

<form enctype = "multipart/form-data" action = "/cgi-bin/file.py" method = "post">
<p>File: <input type = "file" name = "filename" /></p>
<p><input type = "submit" value = "Upload" /></p>
</form>


file.py

import cgi, os
import cgitb; cgitb.enable()
import pandas as pd

form = cgi.FieldStorage()

fileitem = form['filename']
if fileitem.file:
    message=fileitem.file.read()

#df = pd.read_csv(message, sep='\t')
#some code for analysis
#some code for analysis
#some code for analysis
The 'message' if printed, prints the data without any separator in a single line, this should ideally passed into a pandas dataframe.

Thanks in advance for your time
You should not use CGI anymore.
Python doc Wrote:CGI is going to be deprecated in Python 3.8 and removed in 3.10
The modern and better way is to use eg Flask.

Here a couple of Thread you can look at.
Read CSV data into Pandas DataSet From Variable Flask?
Show csv file in flask template.html
@snippsat Thank you for your response and suggestion. I am a bioinformatician and a beginner in python and i did try Flask, it was bit of complex for me hence decided to do with CGI.