Biopython on the web (Flask or Django)
1
2
Entering edit mode
5.7 years ago
jiwpark00 ▴ 210

Has anyone here tried to run Biopython scripts (i.e. Python scripts that import Biopython modules) on the web using Flask or Django? If so, are there good tutorials on how to integrate Python scripts into a web app-like?

python biopython flask django • 3.0k views
ADD COMMENT
4
Entering edit mode
5.1 years ago
l0o0 ▴ 210

I think Flask is written in python, so you can handle this job as how you treat your data in python script.

Here is a small flask app to upload a fasta file and then get sequence id of fasta file.

import os
from flask import Flask, request, redirect, url_for, send_from_directory
from werkzeug import secure_filename
from Bio import SeqIO


UPLOAD_FOLDER = 'path/to/your/upload/folder'
ALLOWED_EXTENSIONS = set(['fa'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

def handle_fa(filename):
    fa = SeqIO.to_dict(SeqIO.parse(filename, 'fasta'))
    return ', '.join([str(x) for x in fa.keys()])

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    print 'ok'
    print dir(request)
    print "Files:", request.files

    if request.method == 'POST':
        upfile = request.files['file']
        if upfile and allowed_file(upfile.filename):
            print upfile.filename
            filename = secure_filename(upfile.filename)
            upfile.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))            
            return handle_fa(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            #redirect(url_for('upload_file'))

    return '''
    <!doctype html>
    <title>Upload new File</title>
    <h1>Upload new File</h1>
    <form action="" method=post enctype=multipart/form-data>
      <p><input type=file name=file>
         <input type=submit value=Upload>
    </form>
    '''

if __name__ == "__main__":
   app.run(port=5000, debug=True)

Save the code to fa.py, and run python fa.py(PS. You should install flask and biopython first), open http://127.0.0.1:5000/ in your browser, upload a small fasta file, and your will see sequence id in your browser.

I was just add a fasta handle function in the script, you can get origin code from here. As you see in the code, I use biopython to handle the uploaded file, then pass the result to flask. So that you can see the result from web browser.

ADD COMMENT

Login before adding your answer.

Traffic: 1591 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6