Spaces:
Sleeping
Sleeping
Update API.py
Browse files
API.py
CHANGED
|
@@ -6,26 +6,25 @@
|
|
| 6 |
|
| 7 |
# %%
|
| 8 |
import requests
|
| 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/1nsIgi9o9VSBKQlNxbxihPzG_N7s4um0eNVfgL4gaGPc/export?format=csv&gid=0'
|
| 26 |
-
prjpartsURL= 'https://docs.google.com/spreadsheets/d/1nsIgi9o9VSBKQlNxbxihPzG_N7s4um0eNVfgL4gaGPc/export?format=csv&gid=34865056'
|
| 27 |
-
prjsectionsURL= 'https://docs.google.com/spreadsheets/d/1nsIgi9o9VSBKQlNxbxihPzG_N7s4um0eNVfgL4gaGPc/export?format=csv&gid=1751466819'
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
dfNames=pd.DataFrame()
|
| 31 |
dfParts=pd.DataFrame()
|
|
@@ -92,6 +91,7 @@ def APIValues():
|
|
| 92 |
head={'Content-Type':'application/json;charset=UTF-8',"Authorization":'Bearer {}'.format(key)}
|
| 93 |
response=requests.post(url="https://console.trevorsadd.co.uk/project/getprojectslist",json=query,headers=head)
|
| 94 |
dict1=response.json()
|
|
|
|
| 95 |
return dict1
|
| 96 |
|
| 97 |
# %% [markdown]
|
|
@@ -99,6 +99,7 @@ def APIValues():
|
|
| 99 |
|
| 100 |
# %%
|
| 101 |
def GenerateTables():
|
|
|
|
| 102 |
dict1=APIValues()
|
| 103 |
|
| 104 |
##First table should ONLY contain: All project Ids and names
|
|
@@ -123,17 +124,22 @@ def GenerateTables():
|
|
| 123 |
# Set Tables to Sheets
|
| 124 |
def AppendtablestoSheets():
|
| 125 |
gc=AuthorizeGoogleSheets()[2]
|
| 126 |
-
|
|
|
|
| 127 |
table1,table2,table3= GenerateTables()
|
| 128 |
projectNamesSheet=ws.worksheet_by_title('Project Names')
|
| 129 |
projectPartsSheet=ws.worksheet_by_title('Project Parts')
|
| 130 |
projectSectionsSheet=ws.worksheet_by_title('Sections')
|
| 131 |
projectNamesSheet.clear()
|
| 132 |
projectPartsSheet.clear()
|
| 133 |
-
|
| 134 |
#append tables to google sheets
|
|
|
|
|
|
|
|
|
|
| 135 |
projectNamesSheet.set_dataframe(start='A1',df=table1)
|
| 136 |
projectPartsSheet.set_dataframe(start='A1',df=table2)
|
|
|
|
| 137 |
projectSectionsSheet.set_dataframe(start='A1',df=table3)
|
| 138 |
|
| 139 |
# return table1,table2,table3
|
|
@@ -142,13 +148,23 @@ def AppendtablestoSheets():
|
|
| 142 |
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
| 143 |
def getPrjNames(url=prjnamesURL):
|
| 144 |
#url first sheet of names
|
| 145 |
-
#
|
| 146 |
df = pd.read_csv(prjnamesURL)
|
| 147 |
return df['ProjectName'].tolist(), df['ProjectId'].tolist()
|
| 148 |
|
| 149 |
# %%
|
| 150 |
def getprjParts(chosenprjid,urlNames=prjnamesURL,urlparts=prjpartsURL):
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
# url_2 = urlparts.replace('/edit#gid=', '/export?format=csv&gid=')
|
| 153 |
global dfParts
|
| 154 |
dfParts = pd.read_csv(prjpartsURL)
|
|
@@ -158,7 +174,8 @@ def getprjParts(chosenprjid,urlNames=prjnamesURL,urlparts=prjpartsURL):
|
|
| 158 |
partsIds=dfParts['ProjectPartId'].tolist()
|
| 159 |
print(parts)
|
| 160 |
print(partsIds)
|
| 161 |
-
|
|
|
|
| 162 |
return parts,partsIds
|
| 163 |
|
| 164 |
# %%
|
|
@@ -166,6 +183,22 @@ def getprjSections(chosenprjid, chosenpartid,urlSection=prjsectionsURL):
|
|
| 166 |
#GetprojectSections
|
| 167 |
# url_1 = urlSection.replace('/edit#gid=', '/export?format=csv&gid=')
|
| 168 |
dfSections = pd.read_csv(prjsectionsURL)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
if chosenprjid and chosenpartid:
|
| 170 |
dfSections=dfSections.iloc[np.where((dfSections['ProjectId']==int(chosenprjid)) & (dfSections['ProjectPartId']==int(chosenpartid)) ) ]
|
| 171 |
sections=dfSections['ProjectSection'].tolist()
|
|
|
|
| 6 |
|
| 7 |
# %%
|
| 8 |
import requests
|
| 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/1n8AG8_lCyhh1X97Dg3ZwWUXRUm-G2JdIUgQBTzE4qgc/export?format=csv&gid=0'
|
| 26 |
+
prjpartsURL= 'https://docs.google.com/spreadsheets/d/1n8AG8_lCyhh1X97Dg3ZwWUXRUm-G2JdIUgQBTzE4qgc/export?format=csv&gid=34865056'
|
| 27 |
+
prjsectionsURL= 'https://docs.google.com/spreadsheets/d/1n8AG8_lCyhh1X97Dg3ZwWUXRUm-G2JdIUgQBTzE4qgc/export?format=csv&gid=1751466819'
|
| 28 |
|
| 29 |
dfNames=pd.DataFrame()
|
| 30 |
dfParts=pd.DataFrame()
|
|
|
|
| 91 |
head={'Content-Type':'application/json;charset=UTF-8',"Authorization":'Bearer {}'.format(key)}
|
| 92 |
response=requests.post(url="https://console.trevorsadd.co.uk/project/getprojectslist",json=query,headers=head)
|
| 93 |
dict1=response.json()
|
| 94 |
+
print(dict1)
|
| 95 |
return dict1
|
| 96 |
|
| 97 |
# %% [markdown]
|
|
|
|
| 99 |
|
| 100 |
# %%
|
| 101 |
def GenerateTables():
|
| 102 |
+
|
| 103 |
dict1=APIValues()
|
| 104 |
|
| 105 |
##First table should ONLY contain: All project Ids and names
|
|
|
|
| 124 |
# Set Tables to Sheets
|
| 125 |
def AppendtablestoSheets():
|
| 126 |
gc=AuthorizeGoogleSheets()[2]
|
| 127 |
+
# createSheet()
|
| 128 |
+
ws = gc.open_by_key('1n8AG8_lCyhh1X97Dg3ZwWUXRUm-G2JdIUgQBTzE4qgc')
|
| 129 |
table1,table2,table3= GenerateTables()
|
| 130 |
projectNamesSheet=ws.worksheet_by_title('Project Names')
|
| 131 |
projectPartsSheet=ws.worksheet_by_title('Project Parts')
|
| 132 |
projectSectionsSheet=ws.worksheet_by_title('Sections')
|
| 133 |
projectNamesSheet.clear()
|
| 134 |
projectPartsSheet.clear()
|
| 135 |
+
|
| 136 |
#append tables to google sheets
|
| 137 |
+
table1 = table1.drop_duplicates()
|
| 138 |
+
table2 = table2.drop_duplicates()
|
| 139 |
+
table3 = table3.drop_duplicates()
|
| 140 |
projectNamesSheet.set_dataframe(start='A1',df=table1)
|
| 141 |
projectPartsSheet.set_dataframe(start='A1',df=table2)
|
| 142 |
+
projectSectionsSheet.clear()
|
| 143 |
projectSectionsSheet.set_dataframe(start='A1',df=table3)
|
| 144 |
|
| 145 |
# return table1,table2,table3
|
|
|
|
| 148 |
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
| 149 |
def getPrjNames(url=prjnamesURL):
|
| 150 |
#url first sheet of names
|
| 151 |
+
# Read the CSV data from the Google Sheet URL
|
| 152 |
df = pd.read_csv(prjnamesURL)
|
| 153 |
return df['ProjectName'].tolist(), df['ProjectId'].tolist()
|
| 154 |
|
| 155 |
# %%
|
| 156 |
def getprjParts(chosenprjid,urlNames=prjnamesURL,urlparts=prjpartsURL):
|
| 157 |
|
| 158 |
+
# url_1 = urlNames.replace('/edit#gid=', '/export?format=csv&gid=')
|
| 159 |
+
# global dfNames
|
| 160 |
+
# dfNames = pd.read_csv(url_1)
|
| 161 |
+
# chosenprjname=chosenprjname.replace('"', '')
|
| 162 |
+
# chosenprjnameList=chosenprjname.split(' ')
|
| 163 |
+
# for i, row in dfNames.iterrows():
|
| 164 |
+
# prjnameString=dfNames['ProjectName'].loc[i]
|
| 165 |
+
# if all(name in prjnameString for name in chosenprjnameList):
|
| 166 |
+
# projectNameID=dfNames.iloc[i]['ProjectId']
|
| 167 |
+
# projectNameID=dfNames.iloc[np.where(dfNames['ProjectName'].str.startswith(chosenprjnameList[1]))]['ProjectId'] #ProjectName == chosenprjname
|
| 168 |
# url_2 = urlparts.replace('/edit#gid=', '/export?format=csv&gid=')
|
| 169 |
global dfParts
|
| 170 |
dfParts = pd.read_csv(prjpartsURL)
|
|
|
|
| 174 |
partsIds=dfParts['ProjectPartId'].tolist()
|
| 175 |
print(parts)
|
| 176 |
print(partsIds)
|
| 177 |
+
#Get projectName Id
|
| 178 |
+
|
| 179 |
return parts,partsIds
|
| 180 |
|
| 181 |
# %%
|
|
|
|
| 183 |
#GetprojectSections
|
| 184 |
# url_1 = urlSection.replace('/edit#gid=', '/export?format=csv&gid=')
|
| 185 |
dfSections = pd.read_csv(prjsectionsURL)
|
| 186 |
+
# chosenprjname=chosenprjname.replace('"', '')
|
| 187 |
+
# chosenpart=chosenpart.replace('"', '')
|
| 188 |
+
# projectId=''
|
| 189 |
+
# projectPartID=''
|
| 190 |
+
# chosenprjnameList=chosenprjname.split(' ')
|
| 191 |
+
# chosenpartList=chosenpart.split(' ')
|
| 192 |
+
# for i, row in dfNames.iterrows():
|
| 193 |
+
# prjnameString=dfNames['ProjectName'].loc[i]
|
| 194 |
+
# if all(name in prjnameString for name in chosenprjnameList):
|
| 195 |
+
# projectId=dfNames.iloc[i]['ProjectId']
|
| 196 |
+
# # projectId=dfNames.iloc[np.where(dfNames['ProjectName'].astype(str)==chosenprjname)]['ProjectId'] #ProjectName == chosenprjname
|
| 197 |
+
# for i, row in dfParts.iterrows():
|
| 198 |
+
# prjpartString=dfParts['ProjectPart'].loc[i]
|
| 199 |
+
# if all(part in prjpartString for part in chosenpartList):
|
| 200 |
+
# projectPartID=dfParts['ProjectPartId'].loc[i]
|
| 201 |
+
# projectPartID=dfParts.iloc[np.where(dfParts['ProjectPart'].astype(str)==chosenpart)]['ProjectPartId']
|
| 202 |
if chosenprjid and chosenpartid:
|
| 203 |
dfSections=dfSections.iloc[np.where((dfSections['ProjectId']==int(chosenprjid)) & (dfSections['ProjectPartId']==int(chosenpartid)) ) ]
|
| 204 |
sections=dfSections['ProjectSection'].tolist()
|