-
Notifications
You must be signed in to change notification settings - Fork 2
/
widgets.py
225 lines (192 loc) · 6.85 KB
/
widgets.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
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
'''
Created on 2022-05-22
@author: wf
'''
from typing import List, Union, Tuple
import justpy as jp
import pandas as pd
class LodGrid(jp.AgGrid):
'''
agGrid wrapper to be loaded from list of dicts
'''
def __init__(self, options:dict={},**kwargs):
'''
constructor
Args:
grid_options(dict): AgGrid options
'''
# set up the aggrid
lodGrid_options={
'enableCellTextSelection':True,
# enable sorting on all columns by default
'defaultColDef': {
'sortable': True
},
}
grid_options = {**options, **lodGrid_options}
super().__init__(options=grid_options,**kwargs)
def load_lod(self,lod:list):
'''
load the given list of dicts
'''
# https://stackoverflow.com/questions/20638006/convert-list-of-dictionaries-to-a-pandas-dataframe
df=pd.DataFrame(lod)
self.load_pandas_frame(df)
class MenuButton(jp.QBtn):
'''
a menu button
'''
def __init__(self, **kwargs):
'''
constructor
'''
super().__init__(**kwargs,color='primary')
class MenuLink(MenuButton):
'''
a menu link
'''
def __init__(self, **kwargs):
'''
constructor
'''
super().__init__(**kwargs,type="a",target="_blank")
class QPasswordDialog(jp.QDialog):
'''
a Quasar framework based password dialog
'''
password_dialog_html="""
<q-card>
<q-card-section>
<q-form class="q-gutter-md">
<q-input clearable type='text' name="user" label="User"/>
<q-input clearable type='password' name="password" label="Password">
<q-icon name="visibility_off"/>
</q-input>
</q-form>
</q-card-section>
<q-card-actions align="right">
<q-btn flat name="Login" label="Login" color="primary" v-close-popup></q-btn>
<q-btn flat name="Cancel" label="Cancel" color="primary" v-close-popup></q-btn>
</q-card-actions>
</q-card>
"""
def onVisibilityClick(self,_msg):
#print(msg)
vt=self.visibilityToggle
if vt.name=="visibility_off":
vt.name="visibility"
self.passwordInput.type="text"
else:
vt.name="visibility_off"
self.passwordInput.type="password"
def __init__(self,**kwargs):
'''
constructor
'''
jp.QDialog.__init__(self,**kwargs)
self.card=jp.parse_html(QPasswordDialog.password_dialog_html,a=self)
self.loginButton=self.card.name_dict["Login"]
self.cancelButton=self.card.name_dict["Cancel"]
self.userInput=self.card.name_dict["user"]
self.passwordInput=self.card.name_dict["password"]
self.visibilityToggle=self.card.name_dict["visibility_off"]
self.visibilityToggle.on("click",self.onVisibilityClick)
class QAlert(jp.QDialog):
'''
alert dialog
'''
alert_dialog_html = """
<q-card>
<q-card-section>
<div name="alertTitle" class="text-h6">Alert</div>
</q-card-section>
<q-card-section name="alertContent">
</q-card-section>
<q-card-actions align="right">
<q-btn flat label="OK" color="primary" v-close-popup />
</q-card-actions>
</q-card>
"""
def __init__(self,**kwargs):
'''
constructor
'''
jp.QDialog.__init__(self,**kwargs)
self.card=jp.parse_html(QAlert.alert_dialog_html,a=self)
self.alertTitle=self.card.name_dict["alertTitle"]
self.alertContent=self.card.name_dict["alertContent"]
class QuasarColorPalette:
"""
Provides the Quasar color palette
see https://quasar.dev/style/color-palette
"""
colors = ["red", "pink","purple", "deep-purple", "indigo", "blue", "light-blue", "cyan", "teal", "green",
"light-green", "lime", "yellow", "amber", "orange", "deep-orange", "brown", "grey", "blue-grey"]
@classmethod
def colorPalette(cls):
palette = []
for color in cls.colors:
colors = [f"{color}-{i}" for i in range(1,15)]
palette.append(color)
palette.extend(colors)
return palette
@classmethod
def getBackgroundColor(cls, color:str):
return f"bg-{color}"
@classmethod
def getTextColor(cls, color:str):
return f"text-{color}"
@classmethod
def getAllBackgroundColors(cls):
return [cls.getBackgroundColor(color) for color in cls.colorPalette()]
@classmethod
def getAllTextColors(cls):
return [cls.getTextColor(color) for color in cls.colorPalette()]
class Token(jp.QDiv):
"""
Displays a token and its label in a given color
"""
TOKEN_CLASSES = "rounded-borders text-center row inline flex-center brand-color shadow-4 q-mx-xs"
TOKEN_VALUE_CLASSES = "q-pa-xs"
TOKEN_LABEL_CLASSES = "text-italic text-weight-light text-white q-pa-xs"
def __init__(self, label:str, value:str, color:str=None, **kwargs):
"""
constructor
Args:
label: label of the token (token name in NER)
value: value of the token
color: background color of the token Should be in QuasarColorPalette.colorPalette()
**kwargs:
"""
if color is None:
color = "primary"
classes = f"{self.TOKEN_CLASSES} bg-{color}"
super(Token, self).__init__(classes=classes, **kwargs)
self.label = label
self.value = value
self.valueWrapper = jp.QDiv(a=self, classes=self.TOKEN_VALUE_CLASSES)
jp.QDiv(a=self.valueWrapper, text=self.value, classes="col")
self.labelWrapper = jp.QDiv(a=self, classes=self.TOKEN_LABEL_CLASSES)
jp.QDiv(a=self.labelWrapper, text=self.label, classes="col")
class TokenSequence(jp.QDiv):
"""
Displays the sequence of given tokens.
If the Token is a tuple of the form (label, value) the token value is displayed with its label.
inspired by https://github.com/tvst/st-annotated-text
"""
def __init__(self, tokens:List[Union[str,Tuple[str,str]]], colorMap:dict=None, **kwargs):
super(TokenSequence, self).__init__(classes="q-pr-md q-ma-lg row justify-start", **kwargs)
if colorMap is None:
colorMap={}
labels = {token[0] for token in tokens if isinstance(token, tuple)}
colors = [ color for color in QuasarColorPalette.colorPalette() if '4' in color]
totalColors=len(colors)
for i, label in enumerate(labels):
colorIndex = label.__hash__() % totalColors
colorMap[label]=colors[colorIndex]
for token in tokens:
if isinstance(token, str):
jp.QDiv(a=self, text=token, classes="q-mx-xs text-center q-pa-xs")
else:
label, value = token
Token(label, value, color=colorMap.get(label),a=self)