Spaces:
Sleeping
Sleeping
Update API.py
Browse files
API.py
CHANGED
|
@@ -19,7 +19,7 @@ from google.oauth2 import service_account
|
|
| 19 |
import pygsheets
|
| 20 |
import numpy as np
|
| 21 |
from datetime import datetime
|
| 22 |
-
|
| 23 |
from threading import Thread, Event
|
| 24 |
|
| 25 |
prjnamesURL= 'https://docs.google.com/spreadsheets/d/1I4P7wiCXTAwnld0YQfUJC8bPtuiETna1HGWVuHbuDBk/edit#gid=0'
|
|
@@ -143,56 +143,30 @@ def getPrjNames(url=prjnamesURL):
|
|
| 143 |
#url first sheet of names
|
| 144 |
url_1 = url.replace('/edit#gid=', '/export?format=csv&gid=')
|
| 145 |
df = pd.read_csv(url_1)
|
| 146 |
-
return df['ProjectName'].tolist()
|
| 147 |
|
| 148 |
# %%
|
| 149 |
-
def getprjParts(
|
| 150 |
|
| 151 |
-
url_1 = urlNames.replace('/edit#gid=', '/export?format=csv&gid=')
|
| 152 |
-
global dfNames
|
| 153 |
-
dfNames = pd.read_csv(url_1)
|
| 154 |
-
chosenprjname=chosenprjname.replace('"', '')
|
| 155 |
-
chosenprjnameList=chosenprjname.split(' ')
|
| 156 |
-
for i, row in dfNames.iterrows():
|
| 157 |
-
prjnameString=dfNames['ProjectName'].loc[i]
|
| 158 |
-
if all(name in prjnameString for name in chosenprjnameList):
|
| 159 |
-
projectNameID=dfNames.iloc[i]['ProjectId']
|
| 160 |
-
# projectNameID=dfNames.iloc[np.where(dfNames['ProjectName'].str.startswith(chosenprjnameList[1]))]['ProjectId'] #ProjectName == chosenprjname
|
| 161 |
url_2 = urlparts.replace('/edit#gid=', '/export?format=csv&gid=')
|
| 162 |
global dfParts
|
| 163 |
dfParts = pd.read_csv(url_2)
|
| 164 |
-
|
|
|
|
| 165 |
parts=dfParts['ProjectPart'].tolist()
|
| 166 |
partsIds=dfParts['ProjectPartId'].tolist()
|
| 167 |
print(parts)
|
| 168 |
print(partsIds)
|
| 169 |
-
|
| 170 |
-
|
| 171 |
return parts,partsIds
|
| 172 |
|
| 173 |
# %%
|
| 174 |
-
def getprjSections(
|
| 175 |
#GetprojectSections
|
| 176 |
url_1 = urlSection.replace('/edit#gid=', '/export?format=csv&gid=')
|
| 177 |
dfSections = pd.read_csv(url_1)
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
projectId=''
|
| 181 |
-
projectPartID=''
|
| 182 |
-
chosenprjnameList=chosenprjname.split(' ')
|
| 183 |
-
chosenpartList=chosenpart.split(' ')
|
| 184 |
-
for i, row in dfNames.iterrows():
|
| 185 |
-
prjnameString=dfNames['ProjectName'].loc[i]
|
| 186 |
-
if all(name in prjnameString for name in chosenprjnameList):
|
| 187 |
-
projectId=dfNames.iloc[i]['ProjectId']
|
| 188 |
-
# projectId=dfNames.iloc[np.where(dfNames['ProjectName'].astype(str)==chosenprjname)]['ProjectId'] #ProjectName == chosenprjname
|
| 189 |
-
for i, row in dfParts.iterrows():
|
| 190 |
-
prjpartString=dfParts['ProjectPart'].loc[i]
|
| 191 |
-
if all(part in prjpartString for part in chosenpartList):
|
| 192 |
-
projectPartID=dfParts['ProjectPartId'].loc[i]
|
| 193 |
-
# projectPartID=dfParts.iloc[np.where(dfParts['ProjectPart'].astype(str)==chosenpart)]['ProjectPartId']
|
| 194 |
-
if projectId and projectPartID:
|
| 195 |
-
dfSections=dfSections.iloc[np.where((dfSections['ProjectId']==int(projectId)) & (dfSections['ProjectPartId']==int(projectPartID)) ) ]
|
| 196 |
sections=dfSections['ProjectSection'].tolist()
|
| 197 |
return sections
|
| 198 |
else:
|
|
@@ -200,95 +174,3 @@ def getprjSections(chosenprjname, chosenpart,urlSection=prjsectionsURL):
|
|
| 200 |
|
| 201 |
|
| 202 |
# ##############################################################################
|
| 203 |
-
def update_df(table1,table2,table3): #API old dictionary
|
| 204 |
-
|
| 205 |
-
prjnamesURL= 'https://docs.google.com/spreadsheets/d/1I4P7wiCXTAwnld0YQfUJC8bPtuiETna1HGWVuHbuDBk/edit#gid=0'
|
| 206 |
-
url_1 = prjnamesURL.replace('/edit#gid=', '/export?format=csv&gid=')
|
| 207 |
-
|
| 208 |
-
table1N,table2N,table3N=GenerateTables()
|
| 209 |
-
table1N = pd.read_csv(url_1)
|
| 210 |
-
t1Diff=table1N[~table1N.isin(table1)].dropna()
|
| 211 |
-
t2Diff=table2N[~table2N.isin(table2)].dropna()
|
| 212 |
-
t3Diff=table3N[~table3N.isin(table3)].dropna()
|
| 213 |
-
returnString=''
|
| 214 |
-
if not t1Diff.empty:
|
| 215 |
-
returnString='Changes have been made in Projects. Would you like to retrieve them in the console? By saying yes, you may need to repeat the process(if any).'
|
| 216 |
-
if not t2Diff.empty:
|
| 217 |
-
returnString='Changes have been made in Parts. Would you like to retrieve them in the console? By saying yes, you may need to repeat the process(if any).'
|
| 218 |
-
if not t3Diff.empty:
|
| 219 |
-
returnString='Changes have been made in Sections. Would you like to retrieve them in the console? By saying yes, you may need to repeat the process(if any).'
|
| 220 |
-
if (not t1Diff.empty) and (not t2Diff.empty):
|
| 221 |
-
returnString='Changes have been made in Projects and Parts. Would you like to retrieve them in the console? By saying yes, you may need to repeat the process(if any).'
|
| 222 |
-
if (not t1Diff.empty) and (not t3Diff.empty):
|
| 223 |
-
returnString='Changes have been made in Projects and Sections. Would you like to retrieve them in the console? By saying yes, you may need to repeat the process(if any).'
|
| 224 |
-
if (not t2Diff.empty) and (not t3Diff.empty):
|
| 225 |
-
returnString='Changes have been made in Parts and Sections. Would you like to retrieve them in the console? By saying yes, you may need to repeat the process(if any).'
|
| 226 |
-
|
| 227 |
-
# print(t1Diff,t2Diff,t3Diff)
|
| 228 |
-
# result= returnString
|
| 229 |
-
# print(returnString)
|
| 230 |
-
# # que.put(returnString)
|
| 231 |
-
|
| 232 |
-
print('kkkkk',datetime.now())
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
threading.Timer(5.0,update_df, args=(table1,table2,table3)).start()
|
| 236 |
-
lock=threading.Lock()
|
| 237 |
-
|
| 238 |
-
lock.acquire()
|
| 239 |
-
if lock.locked:
|
| 240 |
-
lock.release()
|
| 241 |
-
yield returnString
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
# class CustomThread(Thread):
|
| 248 |
-
# # constructor
|
| 249 |
-
# def __init__(self, worker_func,table1,table2,table3):
|
| 250 |
-
# super().__init__()
|
| 251 |
-
# self._worker_func = worker_func
|
| 252 |
-
# self.table1_thread=table1
|
| 253 |
-
# self.table2_thread=table2
|
| 254 |
-
# self.table3_thread=table3
|
| 255 |
-
# self.value1=None
|
| 256 |
-
|
| 257 |
-
# def run(self):
|
| 258 |
-
# # while not self.stop_event.is_set():
|
| 259 |
-
# result=self._worker_func(self.table1_thread,self.table2_thread,self.table3_thread)
|
| 260 |
-
# self.value1=result
|
| 261 |
-
# time.sleep(20.0)
|
| 262 |
-
|
| 263 |
-
# def calltoUpdate(table1,table2,table3):
|
| 264 |
-
# while True:
|
| 265 |
-
# thread = CustomThread(update_df,table1,table2,table3)
|
| 266 |
-
# # start the thread
|
| 267 |
-
# thread.start()
|
| 268 |
-
# # wait for the thread to finish
|
| 269 |
-
# thread.join()
|
| 270 |
-
# # report all values returned from a thread
|
| 271 |
-
# print('OUTPIITT',thread.value1)
|
| 272 |
-
# yield thread.value1
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
def do_every (interval, worker_func, iterations = 0):
|
| 276 |
-
if iterations != 1:
|
| 277 |
-
threading.Timer (
|
| 278 |
-
interval,
|
| 279 |
-
do_every, [interval, worker_func, 0 if iterations == 0 else iterations-1]
|
| 280 |
-
).start ()
|
| 281 |
-
|
| 282 |
-
worker_func ()
|
| 283 |
-
|
| 284 |
-
def print_hw ():
|
| 285 |
-
print ("hello world")
|
| 286 |
-
|
| 287 |
-
def print_so ():
|
| 288 |
-
hi="stackoverflow"
|
| 289 |
-
print('ghh')
|
| 290 |
-
return hi
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
# call print_so every second, 5 times total
|
| 294 |
-
|
|
|
|
| 19 |
import pygsheets
|
| 20 |
import numpy as np
|
| 21 |
from datetime import datetime
|
| 22 |
+
from save_thread_result import ThreadWithResult
|
| 23 |
from threading import Thread, Event
|
| 24 |
|
| 25 |
prjnamesURL= 'https://docs.google.com/spreadsheets/d/1I4P7wiCXTAwnld0YQfUJC8bPtuiETna1HGWVuHbuDBk/edit#gid=0'
|
|
|
|
| 143 |
#url first sheet of names
|
| 144 |
url_1 = url.replace('/edit#gid=', '/export?format=csv&gid=')
|
| 145 |
df = pd.read_csv(url_1)
|
| 146 |
+
return df['ProjectName'].tolist(), df['ProjectId'].tolist()
|
| 147 |
|
| 148 |
# %%
|
| 149 |
+
def getprjParts(chosenprjid,urlNames=prjnamesURL,urlparts=prjpartsURL):
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
url_2 = urlparts.replace('/edit#gid=', '/export?format=csv&gid=')
|
| 152 |
global dfParts
|
| 153 |
dfParts = pd.read_csv(url_2)
|
| 154 |
+
print(chosenprjid,dfParts)
|
| 155 |
+
dfParts= dfParts.iloc[np.where(dfParts['ProjectId']==int(chosenprjid))]
|
| 156 |
parts=dfParts['ProjectPart'].tolist()
|
| 157 |
partsIds=dfParts['ProjectPartId'].tolist()
|
| 158 |
print(parts)
|
| 159 |
print(partsIds)
|
| 160 |
+
|
|
|
|
| 161 |
return parts,partsIds
|
| 162 |
|
| 163 |
# %%
|
| 164 |
+
def getprjSections(chosenprjid, chosenpartid,urlSection=prjsectionsURL):
|
| 165 |
#GetprojectSections
|
| 166 |
url_1 = urlSection.replace('/edit#gid=', '/export?format=csv&gid=')
|
| 167 |
dfSections = pd.read_csv(url_1)
|
| 168 |
+
if chosenprjid and chosenpartid:
|
| 169 |
+
dfSections=dfSections.iloc[np.where((dfSections['ProjectId']==int(chosenprjid)) & (dfSections['ProjectPartId']==int(chosenpartid)) ) ]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
sections=dfSections['ProjectSection'].tolist()
|
| 171 |
return sections
|
| 172 |
else:
|
|
|
|
| 174 |
|
| 175 |
|
| 176 |
# ##############################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|