From 3715f0f70f1c81b1524845db54c615e79617fecf Mon Sep 17 00:00:00 2001 From: Jocavdh Date: Mon, 11 Jun 2018 12:01:51 +0200 Subject: [PATCH] autocomplete code working on backend --- app/static/js/app.js | 38 +++++++++++++++++++++++--------------- app/templates/base.html | 6 ++---- app/views.py | 8 +++++--- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/app/static/js/app.js b/app/static/js/app.js index 29adeff..2d887fe 100755 --- a/app/static/js/app.js +++ b/app/static/js/app.js @@ -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"); diff --git a/app/templates/base.html b/app/templates/base.html index 965bce3..1525b89 100755 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -15,7 +15,6 @@ - {% block css %} {% endblock%} @@ -44,9 +43,8 @@ {% block js %} {% endblock%} - - - + + diff --git a/app/views.py b/app/views.py index 7c11b44..a7cd5a4 100755 --- a/app/views.py +++ b/app/views.py @@ -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: