tags:

views:

77

answers:

1

I am working on python and biopython right now. I have a file upload form and whatever file is uploaded suppose(abc.fasta) then i want to pass same name in execute (abc.fasta) function parameter and display function parameter (abc.aln). Right now i am changing file name manually, but i want to have it automatically.

Workflow goes like this.

----If submit is not true then display only header and form part

--- if submit is true then call execute() and get file name from form input

--- Then displaying result file name is same as executed file name but only change in extension

My raw code is here -- http://pastebin.com/FPUgZSSe

Any suggestions, changes and algorithm is appreciated

Thanks

A: 

You need to read the uploaded file out of the cgi.FieldStorage() and save it onto the server. Ususally a temp directory (/tmp on Linux) is used for this. You should remove these files after processing or on some schedule to clean up the drive.

def main():
  import cgi
  import cgitb; cgitb.enable()
  f1 = cgi.FieldStorage()
  if "dfile" in f1:
    fileitem = f1["dfile"]
    pathtoTmpFile = os.path.join("path/to/temp/directory", fileitem.filename)
    fout = file(pathtoTmpFile, 'wb')
    while 1:
        chunk = fileitem.file.read(100000)
        if not chunk: break
        fout.write (chunk)
    fout.close()
    execute(pathtoTmpFile)
    os.remove(pathtoTmpFile)
  else:
    header()
    form()

This modified the execute to take the path to the newly saved file.

cline = ClustalwCommandline("clustalw", infile=pathToFile)

For the result file, you could also stream it back so the user gets a "Save as..." dialog. That might be a little more usable than displaying it in HTML.

Mark
still error in ---if (f1["exe"].value == "Execute"): line
KeyError: 'exe'
@user: sorry didn't really test that, see above. You need to test if "exe" or "dfile" is in your form variables.
Mark
Both form attributes are there
On line 47 of your code you need to test for them. The KeyError is because when the code is initially run, there is no form submit. So f1 does not have an item with key "exe"
Mark
That's what i am trying to do, if no submit then show header and form if submit then execute() and show result in the same page