-
Notifications
You must be signed in to change notification settings - Fork 0
/
sweep_field.py
98 lines (78 loc) · 3.25 KB
/
sweep_field.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
from flask.ext.wtf import Form
from wtforms import widgets, Field, TextField, IntegerField, validators, FormField, HiddenField
from wtforms.compat import text_type, iteritems
import numpy as np
class SweepForm(Form):
start = IntegerField('Start', [validators.required()])
end = IntegerField('End', [validators.required()])
incr = IntegerField('Incr', [validators.required()])
use_range = HiddenField('Use_Range', [validators.required()])
def __init__(self, formdata=None, obj=None, prefix='', data=None, meta=None, **kwargs):
super(SweepForm, self).__init__(formdata, obj, prefix, data, meta, kwargs)
def range(self):
start = self.start.data
end = self.end.data
incr = self.incr.data
use_range = self.use_range.data == '1'
if incr == 0:
incr = 1
if not use_range:
return [start]
else:
if np.sign(end - start) != np.sign(incr):
return [start,end]
else:
output = range(start, end, incr)
output.append(end)
return output
class RowWidget(object):
"""
Renders a list of fields as a set of table rows with th/td pairs.
If `with_table_tag` is True, then an enclosing <table> is placed around the
rows.
Hidden fields will not be displayed with a row, instead the field will be
pushed into a subsequent table row to ensure XHTML validity. Hidden fields
at the end of the field list will appear outside the table.
"""
def __init__(self):
pass
def __call__(self, field, **kwargs):
html = []
kwargs.setdefault('id', field.id)
with_div = True
expended = False
exp_text = 'style=display:none' if expended else ''
if with_div:
html.append('<div>')
html.append('<span %s>' % widgets.html_params(**kwargs))
hidden = ''
html.append('<span class="range-span">')
for subfield in field:
if subfield.type == 'HiddenField':
hidden += text_type(subfield)
else:
html.append('<div class="range-div">%s%s</div>' % (hidden, text_type(subfield)))
hidden = ''
html.append('</span>')
html.append('<button type="button" class="btn btn-default btn-sm" id="%s-toggle-button" name="%s-toggle-button" style="font-size: 16px;">' % (field.name, field.name))
html.append(' <span></span>')
html.append('</button>')
html.append('</span>')
if with_div:
html.append('</div>')
if hidden:
html.append(hidden)
return widgets.HTMLString(''.join(html))
class RowFormField(FormField):
"""
Encapsulate a form as a field in another form.
:param form_class:
A subclass of Form that will be encapsulated.
:param separator:
A string which will be suffixed to this field's name to create the
prefix to enclosed fields. The default is fine for most uses.
"""
widget = RowWidget()
def __init__(self, form_class, label=None, validators=None, separator='-', tooltip='', **kwargs):
super(RowFormField, self).__init__(form_class, label, validators, separator, **kwargs)
self.tooltip = tooltip