-
Notifications
You must be signed in to change notification settings - Fork 2
/
viewer.enaml
280 lines (258 loc) · 10.6 KB
/
viewer.enaml
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import operator
import pandas as pd
from atom.api import Atom, ContainerList, Typed, observe
from enaml.application import timed_call
from tornado.web import app_log as log
from web.core.api import Looper, Conditional
from web.components.api import *
class SearchModel(Atom):
# Dataframe to search
dataframe = Typed(pd.DataFrame)
# Search parameters for each column
parameters = ContainerList(str)
# Saved results
saved = ContainerList()
# Results of the search
results = Typed(pd.DataFrame)
def _default_parameters(self):
return ['' for c in self.dataframe.columns]
@observe('parameters')
def _update_search_results(self, change):
self.results = self._default_results()
def _default_results(self):
df = self.dataframe
cols = df.columns
filters = [df[cols[i]].str.contains(q.strip(), na=False)
for i, q in enumerate(self.parameters) if q.strip()]
for f in filters:
df = df[f]
return df
enamldef ResistorCalculator(Div):
tag = 'div'
func calculate(value):
try:
value = value.upper()
n = len(value)
if n not in (3, 4):
return '-'
u = ''
if 'R' in value:
v = round(float(value.replace('R', '.')), 2)
v = str(v).rstrip('0').rstrip('.')
else:
v = int(value[0:-1])
#if n == 4:
# v += float('0.'+value[2])
v *= 10**int(value[-1])
if v >= 1e6:
u = 'M'
v = str(round(v/1e6, 2)).rstrip('0').rstrip('.')
elif v >= 1e3:
u = 'k'
v = str(round(v/1e3, 2)).rstrip('0').rstrip('.')
return f'{v}{u}Ω'
except Exception as e:
log.error(e)
return '-'
H4:
text = 'SMD Resistor calculator'
Label:
style = 'padding-right: 1em'
text = 'Code:'
Input: input:
type = 'text'
value = '470'
P:
text << f'Resistance: {calculate(input.value)}'
enamldef CapacitorCalculator(Div):
tag = 'div'
func calculate(value, cap_type):
try:
value = value.upper()
n = len(value)
if n not in (3, 4):
return '-'
# Aluminum caps are different
is_aluminum = int(cap_type) == 1
u = 'u' if is_aluminum else 'p'
if 'R' in value:
v = round(float(value.replace('R', '.')), 2)
v = str(v).rstrip('0').rstrip('.')
else:
v = int(value[0:-1])
v *= 10**int(value[-1])
if v >= 1e6:
u = '' if is_aluminum else 'u'
v = str(round(v/1e6, 2)).rstrip('0').rstrip('.')
elif v >= 1e3:
u = 'm' if is_aluminum else 'n'
v = str(round(v/1e3, 2)).rstrip('0').rstrip('.')
return f'{v}{u}F'
except Exception as e:
log.error(e)
return '-'
H4:
text = 'SMD Capacitor calculator'
Label:
style = 'padding-right: 1em'
text = 'Code:'
Input: input:
type = 'text'
value = '102'
Label:
style = 'padding: 0 1em'
text = 'Type:'
Select: select:
value = '0'
Looper:
iterable = ('Tantalum, ceramic, film', 'Aluminum')
Option:
value = f'{loop_index}'
text = f'{loop_item}'
P:
text << f'Capacitance: {calculate(input.value, select.value)}'
enamldef Viewer(Html): viewer:
attr request # The tornado request
attr response # The tornado response handler
attr dataframe # The pandas dataframe
attr search = SearchModel(dataframe=dataframe)
attr loading = False
attr view_counts = [10, 25, 50, 100]
attr view_count = view_counts[1]
Head:
Title:
text = "SMD Component Tools"
Script:
src = 'https://code.jquery.com/jquery-3.3.1.min.js'
type = 'text/javascript'
Script:
src = '/static/app.js'
type = 'text/javascript'
Script:
text = '''
$(document).ready(function(){
initViewer("%s");
});
'''% viewer.id
Link:
rel="stylesheet"
href="//stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"
attrs = dict(
integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS",
crossorigin="anonymous"
)
Body:
Nav:
cls = 'navbar navbar-dark fixed-top bg-dark flex-md-nowrap p-0 shadow'
A:
cls = 'navbar-brand col-sm-3 col-md-2 mr-0'
href = '#'
text = 'SMD Component Tools'
Div:
cls = 'container-fluid'
Div:
cls = 'row'
Div:
cls = 'col-md-2 bg-light sidebar'
style = 'padding-top: 48px;'
Div:
cls = 'sidebar-sticky'
H6:
cls = 'sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted'
text = 'Saved components'
Ul:
cls = 'nav flex-column mb-2'
Looper:
iterable << search.saved[:]
Li:
cls = 'nav-item'
Span:
cls = 'nav-link'
text = 'Code: {} Dev: {} Mfg: {} Base: {} Pkg: {} Desc: {}'.format(*loop_item)
Div:
cls = 'nav-link'
Button:
cls = 'btn btn-sm btn-danger '
text = 'Remove'
clickable = True
clicked ::
try:
search.saved.remove(loop_item)
except:
pass
Div:
cls = 'col-md-10 px-4'
style = 'padding-top: 48px;'
Hr:
pass
Div:
cls = 'row'
ResistorCalculator:
cls = 'col-md-6'
CapacitorCalculator:
cls = 'col-md-6'
Hr:
pass
Div:
cls = 'row'
Div:
cls = 'col-sm-12'
H4:
text = 'SMD Component search'
P:
text << 'Showing {} of {} results'.format(
search.results.shape[0],
dataframe.shape[0]
)
Select:
value << str(viewer.view_count)
value :: viewer.view_count = int(change['value'])
Looper:
iterable << view_counts
Option:
value = str(loop_item)
text = str(loop_item)
selected << view_count == loop_item
Table:
cls = 'table table-striped table-sm'
THead:
Tr:
Looper:
iterable << dataframe.columns
Th:
Label:
text << str(loop_item)
Br:
pass
Input:
type = 'text'
value := search.parameters[loop_index]
Th:
text = ''
TBody:
func lookup_safe(df, row, col):
try:
return str(df.iloc[row, col])
except:
return '-'
Looper:
# The number of rows shown is defined by pandas's config
iterable << range(view_count)
Tr:
attr row = loop_index
Looper:
iterable << dataframe.columns
Td:
attr col = loop_index
text << lookup_safe(search.results, row, col)
Td:
Button:
cls = 'btn btn-sm btn-info'
text = 'Save'
clickable = True
clicked ::
try:
item = search.results.iloc[row]
search.saved.append(tuple(item))
except IndexError as e:
pass