From af29b4c039c5d9450b8b6d66a2edd3d649d546ad Mon Sep 17 00:00:00 2001 From: Daoud Clarke Date: Thu, 16 Dec 2021 21:36:01 +0000 Subject: [PATCH] Update results as you type --- app.py | 4 ++++ create_app.py | 16 ++++++---------- static/index.html | 7 ++++++- static/index.js | 42 ++++++++++++++++++++++++++++++++++++++++++ static/search.html | 18 ++++++++++++++++++ 5 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 static/index.js create mode 100644 static/search.html diff --git a/app.py b/app.py index 909030f..9bb801f 100644 --- a/app.py +++ b/app.py @@ -1,3 +1,5 @@ +import logging + import uvicorn import create_app @@ -8,6 +10,8 @@ from paths import INDEX_PATH tiny_index = TinyIndex(Document, INDEX_PATH, NUM_PAGES, PAGE_SIZE) app = create_app.create(tiny_index) +logging.basicConfig() + if __name__ == "__main__": uvicorn.run("app:app", host="127.0.0.1", port=8000, log_level="info") diff --git a/create_app.py b/create_app.py index 1aafff8..590b62a 100644 --- a/create_app.py +++ b/create_app.py @@ -1,3 +1,4 @@ +from logging import getLogger from typing import List import Levenshtein @@ -8,22 +9,17 @@ from starlette.staticfiles import StaticFiles from index import TinyIndex, Document +logger = getLogger(__name__) + + def create(tiny_index: TinyIndex): app = FastAPI() @app.get("/search") def search(s: str): results = get_results(s) - doc = "" - for result in results: - doc += f'

{result.title}

\n' - return HTMLResponse(doc) - - # if '—' in s: - # url = s.split('—')[1].strip() - # else: - # url = f'https://www.google.com/search?q={s}' - # return RedirectResponse(url) + logger.info("Return results: %r", results) + return results def order_results(query, results: List[Document]): ordered_results = sorted(results, key=lambda result: Levenshtein.distance(query, result.title)) diff --git a/static/index.html b/static/index.html index 2bd6812..84d2ee6 100644 --- a/static/index.html +++ b/static/index.html @@ -5,12 +5,17 @@ type="application/opensearchdescription+xml" title="Stoatally Different" href="http://localhost:8000/plugin.xml"> + Stoatally different.
- +
+ +
+ +
diff --git a/static/index.js b/static/index.js new file mode 100644 index 0000000..6d0426b --- /dev/null +++ b/static/index.js @@ -0,0 +1,42 @@ + + +window.onload = (event) => { + const searchInput = document.getElementById('search'); + + searchInput.addEventListener('keyup', (e) => { + console.log(searchInput.value); + + const encodedValue = encodeURIComponent(searchInput.value); + fetch('/search?s=' + encodedValue).then(response => { + clearResults(); + console.log(response); + response.json().then(content => { + console.log(content); + content.forEach(element => { + addResult(element.title, element.url); + }) + }); + }); + }); +}; + + +function clearResults() { + const results = document.getElementById('results'); + results.innerHTML = ''; +} + + +function addResult(title, url) { + const par = document.createElement("p"); + + const link = document.createElement("a"); + const linkText = document.createTextNode(title); + link.appendChild(linkText); + link.href = url; + + par.appendChild(link); + + const results = document.getElementById('results'); + results.appendChild(par); +} \ No newline at end of file diff --git a/static/search.html b/static/search.html new file mode 100644 index 0000000..42dc8b8 --- /dev/null +++ b/static/search.html @@ -0,0 +1,18 @@ + + + + + Search results + + + +
+
+ + +
+
+ + + + \ No newline at end of file