autocomplete code working on backend

This commit is contained in:
Jocavdh 2018-06-11 12:01:51 +02:00
parent 932ce9102a
commit 3715f0f70f
3 changed files with 30 additions and 22 deletions

View File

@ -159,8 +159,10 @@ $(document).ready(function()
);
// Autocomplete for search - Contact Joca in case of trouble
// Check: Code doesn't work inside document.ready function
// Are the other functions correctly closed?
// Send query to server
$('#search').on("input", function() {
var query = this.value;
@ -168,26 +170,32 @@ $('#search').on("input", function() {
url: "/autocomplete_suggestions",
data: $('form').serialize(),
type: "POST",
async: false,
success: function(response) {
//console.log("Got your query!");
}
});
var JSONItems = ['battle'];
$.getJSON( "/autocomplete_suggestions", callbackFuncWithData);
$.ajax({
type: "GET",
url: "/autocomplete_suggestions",
dataType: "json",
success: function(data) {
// Start autocomplete
var availableTags = data;
console.log(availableTags);
$( "#search" ).autocomplete({
source: availableTags
});
// End of autocomplete
}
});
function callbackFuncWithData() {
$("#search").autocomplete({
source: function( request, response ) {
$.getJSON( "autocomplete_suggestions", {
term: extractLast( request.term )
}, response );
}
});
}
});
// Get autocomplete_suggestions
console.log("haha");

View File

@ -15,7 +15,6 @@
<![endif]-->
<link rel="stylesheet" href="/static/css/style.css">
<link rel="stylesheet" href="/static/js/jquery-ui-1.12.1.custom/jquery-ui.css">
<link rel="stylesheet" href="/static/js/jquery-ui-1.12.1.custom/jquery-ui.js">
{% block css %} {% endblock%}
</head>
<body>
@ -44,9 +43,8 @@
{% block js %} {% endblock%}
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.2.4.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<script type='text/javascript' src='//cdn.jsdelivr.net/jquery.marquee/1.4.0/jquery.marquee.min.js'></script>
<script type="text/javascript" src="{{ url_for("static", filename="js/jquery.tablesorter.js") }}"></script>
<script src="{{ url_for("static", filename="js/app.js") }}"></script>

View File

@ -21,6 +21,7 @@ from csv import DictWriter, DictReader
import io
from sqlalchemy.inspection import inspect
import autocomplete
from autocomplete import models
import sys
import os
@ -399,7 +400,7 @@ def show_instances():
@app.route('/books', methods= ['POST','GET'])
def show_books():
autocomplete.load() #Train markov model once, for autocomplete in search
#autocomplete.load() #Train markov model once, for autocomplete in search
books = db.session.query(Book).all()
search = SearchForm(request.form)
@ -439,14 +440,15 @@ autocomplete_suggestions = []
autocomplete.load() #Train markov model once, for autocomplete in search
@app.route('/autocomplete_suggestions', methods=['GET', 'POST'])
def test1():
def autocomplete_search():
if request.method == 'POST':
query = request.form['search']
query_tokenized = query.lower().split()
print(query_tokenized)
word_1 = query_tokenized[-2]
word_2 = query_tokenized[-1]
#print(word_1)
print(word_1)
print(word_2)
autocomplete_output = autocomplete.predict(word_1 , word_2)
autocomplete_suggestions.clear()
for suggestion, score in autocomplete_output: