Biopython on the web (Flask or Django)
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
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'

app = Flask(__name__)

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)
  ['UPLOAD_FOLDER'], filename))            
            return handle_fa(os.path.join(app.config['UPLOAD_FOLDER'], filename))

    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>

if __name__ == "__main__":, debug=True)

Save the code to, and run python You should install flask and biopython first), open 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.


Login before adding your answer.

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

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

Powered by the version 2.3.6