-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
115 lines (77 loc) · 2.88 KB
/
app.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from flask import Flask, jsonify, render_template, request
from backend.mol_sql import Database
from backend import mol_display
from io import TextIOWrapper
# open connection to database
db = Database()
db.create_tables()
app = Flask(__name__)
# ##################################################################################
# ENDPOINT DEFINITIONS
# ##################################################################################
@app.route('/', methods=['GET'])
def home():
return render_template('index.html')
@app.route('/molecule_list', methods=['GET'])
def molecule_list():
return jsonify(db.fetch_molecules())
@app.route('/element_list', methods=['GET'])
def element_list():
return jsonify(db.fetch_elements())
@app.route('/upload', methods=['POST'])
def upload():
if 'file' not in request.files:
return jsonify({'message': 'No file selected'}), 400
file = request.files['file']
# Check if the file is empty
if file.filename == '':
return jsonify({'message': 'Invalid file uploaded'}), 400
text_file_obj = TextIOWrapper(file)
name = request.form.get('name')
if db.molecule_exists(name):
return jsonify({'message': 'Molecule with this name already exists'}), 409
elif db.add_molecule(name, text_file_obj):
molecule = db.fetch_molecule(name)
return jsonify({
'message': 'File uploaded successfully',
'molecule': molecule}), 201
return jsonify({'message': 'Invalid file uploaded'}), 400
@app.route('/elements', methods=['PUT'])
def add_element():
element = request.get_json()['element']
replaced = db.force_add_element((
element['number'],
element['code'],
element['name'],
element['color1'][1:],
element['color2'][1:],
element['color3'][1:],
element['radius']
))
# element already exists in database
if replaced:
return '', 200
return '', 201
@app.route('/elements/<element_name>', methods=['DELETE'])
def remove_element(element_name):
db.delete_element(element_name)
return '', 204
@app.route('/molecule/<molecule_name>', methods=['GET'])
def view_molecule(molecule_name):
mol_display.radius = db.radius()
mol_display.element_name = db.element_name()
mol_display.gradients = db.radial_gradients()
if not db.molecule_exists(molecule_name):
return not_found_error("error")
mol = db.load_mol(molecule_name)
mol.sort()
svg = mol.svg()
return render_template('molecule.html', svg_content=svg)
@app.errorhandler(404)
def not_found_error(error):
return render_template('404.html'), 404
# ##################################################################################
# START SERVER
# ##################################################################################
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)