|
a |
|
b/ct-eligible-flask/index.py |
|
|
1 |
import json |
|
|
2 |
|
|
|
3 |
import click |
|
|
4 |
from flask import jsonify, make_response, render_template |
|
|
5 |
|
|
|
6 |
from app import app, mongo |
|
|
7 |
from app.suggestions.suggest_cluster import ClusterSuggestor |
|
|
8 |
|
|
|
9 |
|
|
|
10 |
def add_cluster_json_file_to_mongo(json_file): |
|
|
11 |
|
|
|
12 |
with open(json_file) as f: |
|
|
13 |
clust_list = json.load(f) |
|
|
14 |
|
|
|
15 |
for clust in clust_list: |
|
|
16 |
mongo.db.clusters.replace_one( |
|
|
17 |
{'_id': clust['_id']}, clust, upsert=True) |
|
|
18 |
|
|
|
19 |
|
|
|
20 |
def add_ctep_json_file_to_mongo(json_file): |
|
|
21 |
|
|
|
22 |
with open(json_file) as f: |
|
|
23 |
ctep_list = json.load(f) |
|
|
24 |
|
|
|
25 |
for ctep in ctep_list: |
|
|
26 |
mongo.db.ctep.replace_one( |
|
|
27 |
{'_id': ctep['_id']}, ctep, upsert=True) |
|
|
28 |
|
|
|
29 |
|
|
|
30 |
@app.errorhandler(404) |
|
|
31 |
def not_found(error): |
|
|
32 |
return make_response(jsonify({'error': 'Not found'}), 404) |
|
|
33 |
|
|
|
34 |
|
|
|
35 |
@app.route('/') |
|
|
36 |
def index(): |
|
|
37 |
return render_template('index.html') |
|
|
38 |
|
|
|
39 |
|
|
|
40 |
@app.route('/get-suggestion/') |
|
|
41 |
@app.route('/get-suggestion/<input_json>') |
|
|
42 |
def get_suggestions(input_json=None, methods=['POST']): |
|
|
43 |
|
|
|
44 |
if not input_json: |
|
|
45 |
return 'No input text.' |
|
|
46 |
|
|
|
47 |
try: |
|
|
48 |
input_data = json.loads(input_json) |
|
|
49 |
except ValueError: |
|
|
50 |
input_data = json.loads(json.dumps(input_json)) |
|
|
51 |
|
|
|
52 |
cluster_suggestor = ClusterSuggestor() |
|
|
53 |
suggestion = cluster_suggestor.suggest(input_data) |
|
|
54 |
|
|
|
55 |
return jsonify(suggestion) |
|
|
56 |
|
|
|
57 |
|
|
|
58 |
@click.group() |
|
|
59 |
def cli(): |
|
|
60 |
pass |
|
|
61 |
|
|
|
62 |
|
|
|
63 |
@cli.command() |
|
|
64 |
def run_server(): |
|
|
65 |
app.run(host='0.0.0.0', port=4000) |
|
|
66 |
|
|
|
67 |
|
|
|
68 |
@cli.command() |
|
|
69 |
@click.argument('json_file') |
|
|
70 |
def add_cluster_json(json_file): |
|
|
71 |
add_cluster_json_file_to_mongo(json_file) |
|
|
72 |
|
|
|
73 |
|
|
|
74 |
@cli.command() |
|
|
75 |
@click.argument('json_file') |
|
|
76 |
def add_ctep_json(json_file): |
|
|
77 |
add_ctep_json_file_to_mongo(json_file) |
|
|
78 |
|
|
|
79 |
|
|
|
80 |
if __name__ == '__main__': |
|
|
81 |
cli() |