I am new to Flask (and web development) and I am trying to get the selected value from the autocomplete and pass the value to SQL and finally print the table on html.
Here is my javascript in html
<head>
<script type="text/javascript">
$(function() {
$("#autocomplete").autocomplete({
source:function(request, response) {
$.getJSON("/autocomplete",{
q: request.term,
}, function(data) {
response(data.matching_results);
});
},
minLength: 2,
select: function(event, ui) {
alert( "You selected: " + ui.item.label );
console.log(ui.item.value);
}
});
})
</script>
</head>
<body>
<div>
<form class="form-horizontal" method="post" action="/showSelection">
<h3>Genes</h3>
<input name="autocomplete" name="inputGene" type="text" placeholder="Gene name" id="autocomplete" class="form-control input-lg"/ >
</form>
</div>
</body>
In my app.py (what I have came up with so far, (the autocomplete part is working, but I am not sure how to get the value and use the value to query SQL)
@app.route('/autocomplete', methods=['GET', 'POST'])
def autocomplete():
czz = mysql.connect()
cursor=czz.cursor()
search = request.args.get('q')
query = ("SELECT Symbol from drugs where Symbol like '%"+search+"%'")
cursor.execute(query)
symbols = cursor.fetchall()
# query = metadata.query(drugs.Symbol).filter(drugs.Symbol.like('%' + str(search) + '%'))
results = [mv[0] for mv in symbols]
return jsonify(matching_results=results)
czz.close()
cursor.close()
@app.route('/showSelection', methods=['POST'])
def showSelection():
pd.set_option('display.max_colwidth', -1)
_Gene = request.form.get('inputGene')
# _Gene = request.form['inputGene']
_Gene = str(_Gene)
print str(_Gene)
conn = mysql.connect()
cursor = conn.cursor()
query=("SELECT * from tbl_wish where gene_name = %s")%(_Gene)
cursor.execute(query)
variant=cursor.fetchall()
print variant
vas_dict=[]
for vas in variant:
vaf_dict = {
'Gene':vas[1],
'Literature':vas[2],
'Variant':vas[3],
'Description':vas[4]
}
vas_dict.append(vaf_dict)
variants = pd.DataFrame(vas_dict)
variants = variants[['Gene','Literature','Variant','Description']]
#print variants
return render_template('listVariants.html', tables=[variants.to_html(index=False)], titles=["Variant list"])
cnn.close()
cursor.close()
Appreciate any help!!