Spaces:
Sleeping
Sleeping
Update API.py
Browse files
API.py
CHANGED
|
@@ -9,14 +9,18 @@ import requests
|
|
| 9 |
import json
|
| 10 |
import pandas as pd
|
| 11 |
import threading
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
# from __future__ import print_function
|
| 15 |
from googleapiclient.discovery import build
|
| 16 |
from google.oauth2 import service_account
|
| 17 |
import pygsheets
|
| 18 |
import numpy as np
|
| 19 |
from datetime import datetime
|
|
|
|
|
|
|
| 20 |
|
| 21 |
prjnamesURL= 'https://docs.google.com/spreadsheets/d/1I4P7wiCXTAwnld0YQfUJC8bPtuiETna1HGWVuHbuDBk/edit#gid=0'
|
| 22 |
prjpartsURL= 'https://docs.google.com/spreadsheets/d/1I4P7wiCXTAwnld0YQfUJC8bPtuiETna1HGWVuHbuDBk/edit#gid=2102042200'
|
|
@@ -24,7 +28,9 @@ prjsectionsURL= 'https://docs.google.com/spreadsheets/d/1I4P7wiCXTAwnld0YQfUJC8b
|
|
| 24 |
|
| 25 |
dfNames=pd.DataFrame()
|
| 26 |
dfParts=pd.DataFrame()
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
def AuthorizeGoogleSheets():
|
| 29 |
SCOPES = [
|
| 30 |
'https://www.googleapis.com/auth/spreadsheets',
|
|
@@ -36,7 +42,7 @@ def AuthorizeGoogleSheets():
|
|
| 36 |
gc = pygsheets.authorize(custom_credentials=credentials, client_secret='credentials.json')
|
| 37 |
return spreadsheet_service,drive_service,gc
|
| 38 |
|
| 39 |
-
# %%
|
| 40 |
def createSheet():
|
| 41 |
spreadsheet_service,drive_service,gc=AuthorizeGoogleSheets()
|
| 42 |
#create sheet
|
|
@@ -93,6 +99,7 @@ def APIValues():
|
|
| 93 |
# %%
|
| 94 |
def GenerateTables():
|
| 95 |
dict1=APIValues()
|
|
|
|
| 96 |
##First table should ONLY contain: All project Ids and names
|
| 97 |
#Second table should contain ProjId,ProjectPartId,ProjectPart
|
| 98 |
#Third table should contain ProjId,ProjectPartId,ProjectSection
|
|
@@ -127,7 +134,8 @@ def AppendtablestoSheets():
|
|
| 127 |
projectNamesSheet.set_dataframe(start='A1',df=table1)
|
| 128 |
projectPartsSheet.set_dataframe(start='A1',df=table2)
|
| 129 |
projectSectionsSheet.set_dataframe(start='A1',df=table3)
|
| 130 |
-
|
|
|
|
| 131 |
# %%
|
| 132 |
#-------------------------------------------------------------------------------CALL THIS FUNCTION TO BEGIN----------------------------------------------------------------------
|
| 133 |
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
@@ -155,6 +163,9 @@ def getprjParts(chosenprjname,urlNames=prjnamesURL,urlparts=prjpartsURL):
|
|
| 155 |
dfParts = pd.read_csv(url_2)
|
| 156 |
dfParts= dfParts.iloc[np.where(dfParts['ProjectId']==int(projectNameID))]
|
| 157 |
parts=dfParts['ProjectPart'].tolist()
|
|
|
|
|
|
|
|
|
|
| 158 |
return parts
|
| 159 |
|
| 160 |
# %%
|
|
@@ -164,7 +175,8 @@ def getprjSections(chosenprjname, chosenpart,urlSection=prjsectionsURL):
|
|
| 164 |
dfSections = pd.read_csv(url_1)
|
| 165 |
chosenprjname=chosenprjname.replace('"', '')
|
| 166 |
chosenpart=chosenpart.replace('"', '')
|
| 167 |
-
|
|
|
|
| 168 |
chosenprjnameList=chosenprjname.split(' ')
|
| 169 |
chosenpartList=chosenpart.split(' ')
|
| 170 |
for i, row in dfNames.iterrows():
|
|
@@ -177,21 +189,104 @@ def getprjSections(chosenprjname, chosenpart,urlSection=prjsectionsURL):
|
|
| 177 |
if all(part in prjpartString for part in chosenpartList):
|
| 178 |
projectPartID=dfParts['ProjectPartId'].loc[i]
|
| 179 |
# projectPartID=dfParts.iloc[np.where(dfParts['ProjectPart'].astype(str)==chosenpart)]['ProjectPartId']
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
url_1 = prjnamesURL.replace('/edit#gid=', '/export?format=csv&gid=')
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
import json
|
| 10 |
import pandas as pd
|
| 11 |
import threading
|
| 12 |
+
from threading import Timer
|
| 13 |
+
from threading import Thread
|
| 14 |
+
import queue
|
| 15 |
+
import time
|
| 16 |
# from __future__ import print_function
|
| 17 |
from googleapiclient.discovery import build
|
| 18 |
from google.oauth2 import service_account
|
| 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'
|
| 26 |
prjpartsURL= 'https://docs.google.com/spreadsheets/d/1I4P7wiCXTAwnld0YQfUJC8bPtuiETna1HGWVuHbuDBk/edit#gid=2102042200'
|
|
|
|
| 28 |
|
| 29 |
dfNames=pd.DataFrame()
|
| 30 |
dfParts=pd.DataFrame()
|
| 31 |
+
que= queue.Queue()
|
| 32 |
+
returnString=''
|
| 33 |
+
#FIRST TIME ONLY , DONT CALL UNLESS NEEDED
|
| 34 |
def AuthorizeGoogleSheets():
|
| 35 |
SCOPES = [
|
| 36 |
'https://www.googleapis.com/auth/spreadsheets',
|
|
|
|
| 42 |
gc = pygsheets.authorize(custom_credentials=credentials, client_secret='credentials.json')
|
| 43 |
return spreadsheet_service,drive_service,gc
|
| 44 |
|
| 45 |
+
# %% #FIRST TIME ONLY , DONT CALL UNLESS NEEDED
|
| 46 |
def createSheet():
|
| 47 |
spreadsheet_service,drive_service,gc=AuthorizeGoogleSheets()
|
| 48 |
#create sheet
|
|
|
|
| 99 |
# %%
|
| 100 |
def GenerateTables():
|
| 101 |
dict1=APIValues()
|
| 102 |
+
|
| 103 |
##First table should ONLY contain: All project Ids and names
|
| 104 |
#Second table should contain ProjId,ProjectPartId,ProjectPart
|
| 105 |
#Third table should contain ProjId,ProjectPartId,ProjectSection
|
|
|
|
| 134 |
projectNamesSheet.set_dataframe(start='A1',df=table1)
|
| 135 |
projectPartsSheet.set_dataframe(start='A1',df=table2)
|
| 136 |
projectSectionsSheet.set_dataframe(start='A1',df=table3)
|
| 137 |
+
|
| 138 |
+
# return table1,table2,table3
|
| 139 |
# %%
|
| 140 |
#-------------------------------------------------------------------------------CALL THIS FUNCTION TO BEGIN----------------------------------------------------------------------
|
| 141 |
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
| 163 |
dfParts = pd.read_csv(url_2)
|
| 164 |
dfParts= dfParts.iloc[np.where(dfParts['ProjectId']==int(projectNameID))]
|
| 165 |
parts=dfParts['ProjectPart'].tolist()
|
| 166 |
+
print(parts)
|
| 167 |
+
#Get projectName Id
|
| 168 |
+
|
| 169 |
return parts
|
| 170 |
|
| 171 |
# %%
|
|
|
|
| 175 |
dfSections = pd.read_csv(url_1)
|
| 176 |
chosenprjname=chosenprjname.replace('"', '')
|
| 177 |
chosenpart=chosenpart.replace('"', '')
|
| 178 |
+
projectId=''
|
| 179 |
+
projectPartID=''
|
| 180 |
chosenprjnameList=chosenprjname.split(' ')
|
| 181 |
chosenpartList=chosenpart.split(' ')
|
| 182 |
for i, row in dfNames.iterrows():
|
|
|
|
| 189 |
if all(part in prjpartString for part in chosenpartList):
|
| 190 |
projectPartID=dfParts['ProjectPartId'].loc[i]
|
| 191 |
# projectPartID=dfParts.iloc[np.where(dfParts['ProjectPart'].astype(str)==chosenpart)]['ProjectPartId']
|
| 192 |
+
if projectId and projectPartID:
|
| 193 |
+
dfSections=dfSections.iloc[np.where((dfSections['ProjectId']==int(projectId)) & (dfSections['ProjectPartId']==int(projectPartID)) ) ]
|
| 194 |
+
sections=dfSections['ProjectSection'].tolist()
|
| 195 |
+
return sections
|
| 196 |
+
else:
|
| 197 |
+
return
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
# ##############################################################################
|
| 201 |
+
def update_df(table1,table2,table3): #API old dictionary
|
| 202 |
+
|
| 203 |
+
prjnamesURL= 'https://docs.google.com/spreadsheets/d/1I4P7wiCXTAwnld0YQfUJC8bPtuiETna1HGWVuHbuDBk/edit#gid=0'
|
| 204 |
url_1 = prjnamesURL.replace('/edit#gid=', '/export?format=csv&gid=')
|
| 205 |
+
|
| 206 |
+
table1N,table2N,table3N=GenerateTables()
|
| 207 |
+
table1N = pd.read_csv(url_1)
|
| 208 |
+
t1Diff=table1N[~table1N.isin(table1)].dropna()
|
| 209 |
+
t2Diff=table2N[~table2N.isin(table2)].dropna()
|
| 210 |
+
t3Diff=table3N[~table3N.isin(table3)].dropna()
|
| 211 |
+
returnString=''
|
| 212 |
+
if not t1Diff.empty:
|
| 213 |
+
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).'
|
| 214 |
+
if not t2Diff.empty:
|
| 215 |
+
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).'
|
| 216 |
+
if not t3Diff.empty:
|
| 217 |
+
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).'
|
| 218 |
+
if (not t1Diff.empty) and (not t2Diff.empty):
|
| 219 |
+
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).'
|
| 220 |
+
if (not t1Diff.empty) and (not t3Diff.empty):
|
| 221 |
+
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).'
|
| 222 |
+
if (not t2Diff.empty) and (not t3Diff.empty):
|
| 223 |
+
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).'
|
| 224 |
+
|
| 225 |
+
# print(t1Diff,t2Diff,t3Diff)
|
| 226 |
+
# result= returnString
|
| 227 |
+
# print(returnString)
|
| 228 |
+
# # que.put(returnString)
|
| 229 |
+
|
| 230 |
+
print('kkkkk',datetime.now())
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
threading.Timer(5.0,update_df, args=(table1,table2,table3)).start()
|
| 234 |
+
lock=threading.Lock()
|
| 235 |
+
|
| 236 |
+
lock.acquire()
|
| 237 |
+
if lock.locked:
|
| 238 |
+
lock.release()
|
| 239 |
+
yield returnString
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
# class CustomThread(Thread):
|
| 246 |
+
# # constructor
|
| 247 |
+
# def __init__(self, worker_func,table1,table2,table3):
|
| 248 |
+
# super().__init__()
|
| 249 |
+
# self._worker_func = worker_func
|
| 250 |
+
# self.table1_thread=table1
|
| 251 |
+
# self.table2_thread=table2
|
| 252 |
+
# self.table3_thread=table3
|
| 253 |
+
# self.value1=None
|
| 254 |
+
|
| 255 |
+
# def run(self):
|
| 256 |
+
# # while not self.stop_event.is_set():
|
| 257 |
+
# result=self._worker_func(self.table1_thread,self.table2_thread,self.table3_thread)
|
| 258 |
+
# self.value1=result
|
| 259 |
+
# time.sleep(20.0)
|
| 260 |
+
|
| 261 |
+
# def calltoUpdate(table1,table2,table3):
|
| 262 |
+
# while True:
|
| 263 |
+
# thread = CustomThread(update_df,table1,table2,table3)
|
| 264 |
+
# # start the thread
|
| 265 |
+
# thread.start()
|
| 266 |
+
# # wait for the thread to finish
|
| 267 |
+
# thread.join()
|
| 268 |
+
# # report all values returned from a thread
|
| 269 |
+
# print('OUTPIITT',thread.value1)
|
| 270 |
+
# yield thread.value1
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
def do_every (interval, worker_func, iterations = 0):
|
| 274 |
+
if iterations != 1:
|
| 275 |
+
threading.Timer (
|
| 276 |
+
interval,
|
| 277 |
+
do_every, [interval, worker_func, 0 if iterations == 0 else iterations-1]
|
| 278 |
+
).start ()
|
| 279 |
+
|
| 280 |
+
worker_func ()
|
| 281 |
+
|
| 282 |
+
def print_hw ():
|
| 283 |
+
print ("hello world")
|
| 284 |
+
|
| 285 |
+
def print_so ():
|
| 286 |
+
hi="stackoverflow"
|
| 287 |
+
print('ghh')
|
| 288 |
+
return hi
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
# call print_so every second, 5 times total
|
| 292 |
+
|