Spaces:
Runtime error
Runtime error
File size: 9,480 Bytes
e79ac05 5a75ac1 e79ac05 |
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 |
import streamlit as st
import json
import pandas as pd
from tools import agstyler
from tools.agstyler import PINLEFT
from toolbar import component_toolbar_buttons
class Setup:
class Model:
header1 = "Labels"
header2 = "Groups"
labels_file = "docs/labels.json"
groups_file = "docs/groups.json"
def view(self, model):
tab = st.radio("Select", ["Labels", "Groups"], horizontal=True, label_visibility="collapsed")
if tab == "Labels":
st.title(model.header1)
self.setup_labels(model)
elif tab == "Groups":
st.title(model.header2)
self.setup_groups(model)
def setup_labels(self, model):
self.action_event = False
if 'action' not in st.session_state:
st.session_state['action'] = None
with open(model.labels_file, "r") as f:
labels_json = json.load(f)
labels = labels_json["labels"]
data = []
for label in labels:
data.append({'id': label['id'], 'name': label['name'], 'description': label['description']})
self.df = pd.DataFrame(data)
formatter = {
'id': ('ID', {'hide': True}),
'name': ('Label', {**PINLEFT, 'editable': True}),
'description': ('Description', {**PINLEFT, 'editable': True})
}
def run_component(props):
value = component_toolbar_buttons(key='toolbar_buttons_labels', **props)
return value
def handle_event(value):
if value is not None:
if 'action_timestamp' not in st.session_state:
self.action_event = True
st.session_state['action_timestamp'] = value['timestamp']
else:
if st.session_state['action_timestamp'] != value['timestamp']:
self.action_event = True
st.session_state['action_timestamp'] = value['timestamp']
else:
self.action_event = False
if value is not None and value['action'] == 'create' and self.action_event:
if st.session_state['action'] != 'delete':
max_id = self.df['id'].max()
self.df.loc[-1] = [max_id + 1, '', ''] # adding a row
self.df.index = self.df.index + 1 # shifting index
self.df.sort_index(inplace=True)
st.session_state['action'] = 'create'
elif value is not None and value['action'] == 'delete' and self.action_event:
if st.session_state['action'] != 'delete' and st.session_state['action'] != 'create':
rows = st.session_state['selected_rows']
if len(rows) > 0:
idx = rows[0]['_selectedRowNodeInfo']['nodeRowIndex']
self.df.drop(self.df.index[idx], inplace=True)
self.df.reset_index(drop=True, inplace=True)
st.session_state['action'] = 'delete'
elif value is not None and value['action'] == 'save' and self.action_event:
st.session_state['action'] = 'save'
props = {
'buttons': {
'create': True,
'delete': True,
'save': True,
}
}
handle_event(run_component(props))
if st.session_state['action'] == 'save' and 'response' in st.session_state:
if st.session_state['response'] is not None:
self.df = st.session_state['response']
st.session_state['response'] = None
if st.session_state['action'] == 'create' and 'response' in st.session_state:
if st.session_state['response'] is not None:
self.df = st.session_state['response']
if st.session_state['action'] == 'delete' and 'response' in st.session_state:
if st.session_state['response'] is not None:
self.df = st.session_state['response']
response = agstyler.draw_grid(
self.df,
formatter=formatter,
fit_columns=True,
pagination_size=10,
selection="single",
use_checkbox=False
)
rows = response['selected_rows']
st.session_state['selected_rows'] = rows
if st.session_state['action'] == 'create' and self.action_event:
st.session_state['response'] = response['data']
elif st.session_state['action'] == 'delete' and self.action_event:
st.session_state['response'] = response['data']
elif st.session_state['action'] == 'save' and self.action_event:
data = response['data'].values.tolist()
rows = []
for row in data:
rows.append({'id': row[0], 'name': row[1], 'description': row[2]})
labels_json['labels'] = rows
with open(model.labels_file, "w") as f:
json.dump(labels_json, f, indent=2)
def setup_groups(self, model):
self.action_event = False
if 'action' not in st.session_state:
st.session_state['action'] = None
with open(model.groups_file, "r") as f:
groups_json = json.load(f)
groups = groups_json["groups"]
data = []
for group in groups:
data.append({'id': group['id'], 'name': group['name'], 'description': group['description']})
self.df = pd.DataFrame(data)
formatter = {
'id': ('ID', {'hide': True}),
'name': ('Group', {**PINLEFT, 'editable': True}),
'description': ('Description', {**PINLEFT, 'editable': True})
}
def run_component(props):
value = component_toolbar_buttons(key='toolbar_buttons_groups', **props)
return value
def handle_event(value):
if value is not None:
if 'action_timestamp' not in st.session_state:
self.action_event = True
st.session_state['action_timestamp'] = value['timestamp']
else:
if st.session_state['action_timestamp'] != value['timestamp']:
self.action_event = True
st.session_state['action_timestamp'] = value['timestamp']
else:
self.action_event = False
if value is not None and value['action'] == 'create' and self.action_event:
if st.session_state['action'] != 'delete':
max_id = self.df['id'].max()
self.df.loc[-1] = [max_id + 1, '', ''] # adding a row
self.df.index = self.df.index + 1 # shifting index
self.df.sort_index(inplace=True)
st.session_state['action'] = 'create'
elif value is not None and value['action'] == 'delete' and self.action_event:
if st.session_state['action'] != 'delete' and st.session_state['action'] != 'create':
rows = st.session_state['selected_rows']
if len(rows) > 0:
idx = rows[0]['_selectedRowNodeInfo']['nodeRowIndex']
self.df.drop(self.df.index[idx], inplace=True)
self.df.reset_index(drop=True, inplace=True)
st.session_state['action'] = 'delete'
elif value is not None and value['action'] == 'save' and self.action_event:
st.session_state['action'] = 'save'
props = {
'buttons': {
'create': True,
'delete': True,
'save': True,
}
}
handle_event(run_component(props))
if st.session_state['action'] == 'save' and 'response' in st.session_state:
if st.session_state['response'] is not None:
self.df = st.session_state['response']
st.session_state['response'] = None
if st.session_state['action'] == 'create' and 'response' in st.session_state:
if st.session_state['response'] is not None:
self.df = st.session_state['response']
if st.session_state['action'] == 'delete' and 'response' in st.session_state:
if st.session_state['response'] is not None:
self.df = st.session_state['response']
response = agstyler.draw_grid(
self.df,
formatter=formatter,
fit_columns=True,
pagination_size=10,
selection="single",
use_checkbox=False
)
rows = response['selected_rows']
st.session_state['selected_rows'] = rows
if st.session_state['action'] == 'create' and self.action_event:
st.session_state['response'] = response['data']
elif st.session_state['action'] == 'delete' and self.action_event:
st.session_state['response'] = response['data']
elif st.session_state['action'] == 'save' and self.action_event:
data = response['data'].values.tolist()
rows = []
for row in data:
rows.append({'id': row[0], 'name': row[1], 'description': row[2]})
groups_json['groups'] = rows
with open(model.groups_file, "w") as f:
json.dump(groups_json, f, indent=2)
|