-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
115 lines (88 loc) · 3.71 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
import json
from flask import Flask, render_template
app = Flask(__name__)
path_to_json = 'data/PeriodicTableJSON.json'
CATEGORIES = [ "alkali-metal",
"alkaline-earth-metal",
"transition-metal",
"post-transition-metal",
"metalloid",
"polyatomic-nonmetal",
"diatomic-nonmetal",
"noble-gas",
"lanthanide",
"actinide" ]
class Element(object):
def __init__(self, **kwargs):
self.attributes = kwargs
class ElementStore(object):
def __init__(self):
self.data = ElementStore.get_element_data_from_file(path_to_json)
self.elements = self.map_nrs_to_details()
def cut_first_phrase(elements):
for number in elements:
item = elements[number]
item['summary'] = '.'.join(item['summary'].split('.')[1:])
cut_first_phrase(self.elements)
self.lanthanides_and_actinides = self.get_lanthanides_and_actinides()
def get_element_by_number(self, number):
return [item for item in self.data if item['number'] == number].pop()
def map_nrs_to_details(self):
return {item['number']: {attribute: item[attribute] for attribute in item.keys()} for item in self.data}
@staticmethod
def get_element_data_from_file(json_path):
with open(json_path, 'r') as json_file:
data = json.load(json_file)
return data['elements']
def get_lanthanides_and_actinides(self):
return [[element[0] for element in self.elements.items() if element[1]['category'] == 'lanthanide'],
[element[0] for element in self.elements.items() if element[1]['category'] == 'actinide']]
class PeriodicLayout(object):
def __init__(self):
periodic_table = [[1 if PeriodicLayout.valid_row_and_column(row, column)
else 0 for column in range(18)] for row in range(7)]
PeriodicLayout.fill_atomic_numbers(periodic_table)
PeriodicLayout.insert_lanthanides_and_actinides(periodic_table)
self.layout = periodic_table
@staticmethod
def valid_row_and_column(row, column):
if (row in range(3,7) or
column in (0,1,12,13,14,15,16,17) and row in (1,2) or
column in (0,17) and row == 0):
return True
else:
return False
@staticmethod
def fill_atomic_numbers(table):
index = 1
for row_n, row in enumerate(table):
for column, item in enumerate(row):
if item:
table[row_n][column] = index
index += 1
@staticmethod
def insert_lanthanides_and_actinides(table):
for row_n, row in enumerate(table):
for column, item in enumerate(row):
if item == 57:
table[row_n][column] = 0
if item > 57:
table[row_n][column] = item + 14
for row_n, row in enumerate(table):
for column, item in enumerate(row):
if item == 89:
table[row_n][column] = 0
if item > 89:
table[row_n][column] = item + 14
@app.route('/')
def index():
element_store = ElementStore()
elements = element_store.elements
lanthanides_and_actinides = element_store.lanthanides_and_actinides
return render_template('index.html',
periodic_table=PeriodicLayout().layout,
data=elements,
lanthanides_and_actinides=lanthanides_and_actinides,
categories = CATEGORIES)
if __name__ == '__main__':
app.run(debug=True, use_reloader=False)