[cbecd8]: / ct-eligible-flask / index.py

Download this file

82 lines (51 with data), 1.7 kB

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