From 1a72f8960ea041bc1bdf10ed03c612d29236ccc4 Mon Sep 17 00:00:00 2001
From: felixwelter <felixwelter@gmail.com>
Date: Mon, 12 Oct 2020 13:20:47 +0200
Subject: [PATCH] Return multiple slides per request

---
 app.py               | 9 ++++++---
 templates/index.html | 8 +++++---
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/app.py b/app.py
index 416ff96..72ad559 100644
--- a/app.py
+++ b/app.py
@@ -33,6 +33,7 @@ def allowed_file(filename):
 def benchmark_allowed_file(filename):
     return '.' in filename and filename.rsplit('.', 1)[1].lower() in ["csv"]
 
+
 @app.route('/upload', methods=['POST'])
 def upload():
     if 'files' in request.files:
@@ -58,11 +59,13 @@ def query():
         index = Index(index_dir=INDEX_DIR)
         query = request.form.get("term")
         context = request.form.get("context")
-        result = index.search(query, context)
-        img_name = result["file_name"] + "_" + str(result["page"]) + ".jpg"
+        amount = int(request.form.get("amount"))
+        results = index.search(query, context)[:amount]
+        full_path = lambda res: os.getenv('EXTERNAL_HOST', '<PLEASE_SET_EXTERNAL_HOST_ENV_VAR>') + \
+                                "/slide/" + res["file_name"] + "_" + str(res["page"]) + ".jpg"
         return jsonify({
             "type": "image",
-            "path": os.getenv('EXTERNAL_HOST', '<PLEASE_SET_EXTERNAL_HOST_ENV_VAR>') + "/slide/" + img_name
+            "paths": [{"path": full_path(res), "url": full_path(res)} for res in results]
         })
     except IndexError:
         return jsonify({
diff --git a/templates/index.html b/templates/index.html
index 949ac89..f7062b2 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -41,7 +41,8 @@
         var http = new XMLHttpRequest();
         var url = '/search';
         var params = 'term=' + document.getElementsByName("term")[0].value +
-            '&context=' + document.getElementsByName("context")[0].value
+            '&context=' + document.getElementsByName("context")[0].value +
+            '&amount=3';
         console.log(params)
         http.open('POST', url, true);
         http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
@@ -53,8 +54,9 @@
                     document.getElementById("form-result").innerHTML = "No slide found";
                     return;
                 }
-                img_path = res["path"];
-                html = '<img src="' + img_path + '" height="300px;">';
+                html = res["paths"].map(function (entry) {
+                    return '<img src="' + entry["path"] + '" height="250px;"><br>';
+                }).join("");
                 document.getElementById("form-result").innerHTML = html;
             }
         }
-- 
GitLab