Created
July 4, 2021 10:17
-
-
Save shreyanshu7101904/461a35d07df71fbe2b26af1da779fc0a to your computer and use it in GitHub Desktop.
flak server, html assignment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import os | |
import re | |
from flask import Flask, jsonify, request | |
app = Flask(__name__) | |
FILE = os.environ.get("FILE", "test_data_sample.csv") | |
with open(FILE) as f: | |
reader = csv.reader(f) | |
terms = [row[0] for row in reader][0:] | |
print(terms) | |
@app.route('/process_search') | |
def gen_search_json(): | |
query = request.args.get("q", '') | |
if len(query) < 3: | |
results = [{"name" : "please Enter 3 or more characters"}] | |
else: | |
# match queryresults occurence anywhere in string | |
reg = re.compile("^.*"+query, re.IGNORECASE) | |
values = list(filter(reg. match, terms[1:])) | |
results = [{"name": val, "rank": rank} for rank, val in enumerate(values)] | |
resp = jsonify(results=results[:10]) # top 10 results | |
resp.headers['Access-Control-Allow-Origin'] = '*' | |
return resp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Autocomplete</title> | |
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/css/select2.min.css" rel="stylesheet" /> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css"> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/js/select2.min.js"></script> | |
<style> | |
.container { | |
padding-top: 2rem; | |
max-width: 768px; | |
margin: 0 auto; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<h2>Type your query..</h2> | |
<br> | |
<select class="js-data-example-ajax form-control"></select> | |
</div> | |
<script> | |
$('.js-data-example-ajax').select2({ | |
ajax: { | |
url: 'http://127.0.0.1:8080/process_search', | |
dataType: 'json' | |
}, | |
templateResult: function(e) { return e.name + " " + e.rank; }, | |
templateSelection: function(e) { return e.name; } | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment