import sqlite3
from flask import Flask, render_template, g, request
from flask.ext.socketio import SocketIO, send, emit
import math

app = Flask(__name__)
app.config['SECRET_KEY'] = 'mouse-brain-visualization'
socketio = SocketIO(app)
DATABASE = 'db/cells.db'

# ----------- DB -----------
def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = sqlite3.connect(DATABASE)
        db.row_factory = make_dicts
    return db

def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()

def make_dicts(cursor, row):
    return dict((cursor.description[idx][0], value)
                for idx, value in enumerate(row))

def query_db(query, args=(), one=False):
    cur = get_db().execute(query ,args)
    rv = cur.fetchall()
    cur.close()
    return (rv[0] if rv else None) if one else rv

# ----------- Routing -----------
@app.route('/')
def index():
    cells = query_db('select * from cells')
    links = query_db('select * from links')
    valid_links = []
    for link in links:
        root_coordinate = query_db('select id,x,y,z from cells where name="%s"' % link["root"])
        dest_coordinate = query_db('select id,x,y,z from cells where name="%s"' % link["dest"])
        if len(root_coordinate) != 0 and len(dest_coordinate) != 0:
            link_dict = {}
            link_dict["root"] = root_coordinate[0]
            link_dict["dest"] = dest_coordinate[0]
            link_dict["weight"] = link["weight"]
            valid_links.append(link_dict)
    return render_template('index.html', cells=cells, links=valid_links)

@app.route('/api', methods=['GET', 'POST'])
def api():
    if request.method == 'POST':
        data =  request.json
        data_index = []
        for d in data["cells"]:
            index = query_db('select id from cells where name="%s"' % d)
            if len(index) != 0:
                data_index.append(index[0]["id"])
        socketio.emit('activation', data_index)
        return "Request was sended.\n"
    else:
        return "Request was aborted.\n"

if __name__ == '__main__':
    app.debug = True
    socketio.run(app)

@@ -124,16 +124,46 @@
 ort math
+%0Aimport argparse%0Aimport random
 %0A%0Aapp = 
@@ -282,16 +282,32 @@
 ells.db'
+%0Aabortrate = 100
 %0A%0A# ----
@@ -1759,16 +1759,55 @@
 = 'POST'
+ and random.randint(0, 100) %3C abortrate
 :%0A      
@@ -2205,16 +2205,237 @@
 ain__':%0A
+    #Setup abortrate%0A    global abortrate%0A    parser = argparse.ArgumentParser()%0A    parser.add_argument('--abortrate', dest='abortrate', default=100, type=int)%0A    abortrate = parser.parse_args().abortrate%0A    %0A    #Run%0A
     app.

Update to select abortrate