Marthee commited on
Commit
4b706b3
·
1 Parent(s): b20bfb9

Update API.py

Browse files
Files changed (1) hide show
  1. API.py +46 -35
API.py CHANGED
@@ -17,6 +17,15 @@ import pandas as pd
17
  from googleapiclient.discovery import build
18
  from google.oauth2 import service_account
19
  import pygsheets
 
 
 
 
 
 
 
 
 
20
  def AuthorizeGoogleSheets():
21
  SCOPES = [
22
  'https://www.googleapis.com/auth/spreadsheets',
@@ -123,48 +132,42 @@ def AppendtablestoSheets(ws):
123
  # %%
124
  #-------------------------------------------------------------------------------CALL THIS FUNCTION TO BEGIN----------------------------------------------------------------------
125
  #--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
126
- def getPrjNames():
127
- ws,gc,spreadsheetId=createSheet()
128
- AppendtablestoSheets(ws)
129
- projectNamesSheet=ws.worksheet_by_title('Project Names')
130
- prjnamesfromSheet=projectNamesSheet.get_col(2, returnas='matrix', include_tailing_empty=False)
131
- # print(prjnamesfromSheet[2])
132
- return prjnamesfromSheet[1:]
133
 
134
  # %%
135
- def getprjParts(chosenprjname):
 
 
 
 
 
 
 
 
 
 
 
 
136
  #Get projectName Id
137
- ws,gc,spreadsheetId=createSheet()
138
- projectNamesSheet=ws.worksheet_by_title('Project Names')
139
- prjnamesfromSheet= getPrjNames()
140
- index=prjnamesfromSheet.index(chosenprjname.replace('"', ''))
141
- # index,indexE = [o for o, x in prjnamesfromSheet if x == str(chosenprjname)] #==clicked value
142
- prjnameId=projectNamesSheet.cell((index+2 ,1)).value #retrieved Id
143
- print('iddd',prjnameId)
144
- #Get projectParts
145
- projparts=[]
146
- projectPartsSheet=ws.worksheet_by_title('Project Parts')
147
- prjpartsfromSheet=projectPartsSheet.get_col(1, returnas='matrix', include_tailing_empty=False)
148
-
149
- indices = [o for o, x in enumerate(prjpartsfromSheet[1:]) if x == prjnameId] #==clicked value
150
- print('ind',indices)
151
- for index in indices:
152
- projparts.append([projectPartsSheet.cell((index+2 ,3)).value, projectPartsSheet.cell((index+2 ,2)).value]) #retrieved Id
153
- return projparts
154
 
155
  # %%
156
- def getprjSections(chosenpart):
157
  #GetprojectSections
158
- ws,gc,spreadsheetId=createSheet()
159
- projsections=[]
160
- projectSectionsSheet=ws.worksheet_by_title('Sections')
161
- prjsectionsfromSheet=projectSectionsSheet.get_col(2, returnas='matrix', include_tailing_empty=False)
162
  chosenpart=chosenpart.replace('"', '')
163
- indices2 = [o for o, x in enumerate(prjsectionsfromSheet) if x == chosenpart] #part clicked on
164
- print('indices2',indices2)
165
- for index in indices2:
166
- projsections.append(projectSectionsSheet.cell((index+2 ,3)).value) #retrieved Id
167
- return projsections
168
 
169
  # %%
170
  # tableTrial=pd.DataFrame(data=[[0,'0x','part1'],[0,'1x','part2']],columns= ['ProjectId','ProjectPartId','ProjectPart'])
@@ -183,3 +186,11 @@ def getprjSections(chosenpart):
183
 
184
 
185
 
 
 
 
 
 
 
 
 
 
17
  from googleapiclient.discovery import build
18
  from google.oauth2 import service_account
19
  import pygsheets
20
+ import numpy as np
21
+
22
+ prjnamesURL= 'https://docs.google.com/spreadsheets/d/1I4P7wiCXTAwnld0YQfUJC8bPtuiETna1HGWVuHbuDBk/edit#gid=0'
23
+ prjpartsURL= 'https://docs.google.com/spreadsheets/d/1I4P7wiCXTAwnld0YQfUJC8bPtuiETna1HGWVuHbuDBk/edit#gid=2102042200'
24
+ prjsectionsURL= 'https://docs.google.com/spreadsheets/d/1I4P7wiCXTAwnld0YQfUJC8bPtuiETna1HGWVuHbuDBk/edit#gid=1708565170'
25
+
26
+ dfNames=pd.DataFrame()
27
+ dfParts=pd.DataFrame()
28
+
29
  def AuthorizeGoogleSheets():
30
  SCOPES = [
31
  'https://www.googleapis.com/auth/spreadsheets',
 
132
  # %%
133
  #-------------------------------------------------------------------------------CALL THIS FUNCTION TO BEGIN----------------------------------------------------------------------
134
  #--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
135
+ def getPrjNames(url=prjnamesURL):
136
+ #url first sheet of names
137
+ url_1 = url.replace('/edit#gid=', '/export?format=csv&gid=')
138
+ df = pd.read_csv(url_1)
139
+ return df['ProjectName'].tolist()
 
 
140
 
141
  # %%
142
+ def getprjParts(chosenprjname,urlNames=prjnamesURL,urlparts=prjpartsURL):
143
+
144
+ url_1 = urlNames.replace('/edit#gid=', '/export?format=csv&gid=')
145
+ global dfNames
146
+ dfNames = pd.read_csv(url_1)
147
+ chosenprjname=chosenprjname.replace('"', '')
148
+ projectNameID=dfNames.iloc[np.where(dfNames['ProjectName'].astype(str)==chosenprjname)]['ProjectId'] #ProjectName == chosenprjname
149
+ url_2 = urlparts.replace('/edit#gid=', '/export?format=csv&gid=')
150
+ global dfParts
151
+ dfParts = pd.read_csv(url_2)
152
+ dfParts= dfParts.iloc[np.where(dfParts['ProjectId']==int(projectNameID))]
153
+ parts=dfParts['ProjectPart'].tolist()
154
+ print(parts)
155
  #Get projectName Id
156
+
157
+ return parts
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
  # %%
160
+ def getprjSections(chosenprjname, chosenpart,urlSection=prjsectionsURL):
161
  #GetprojectSections
162
+ url_1 = urlSection.replace('/edit#gid=', '/export?format=csv&gid=')
163
+ dfSections = pd.read_csv(url_1)
164
+ chosenprjname=chosenprjname.replace('"', '')
 
165
  chosenpart=chosenpart.replace('"', '')
166
+ projectId=dfNames.iloc[np.where(dfNames['ProjectName'].astype(str)==chosenprjname)]['ProjectId'] #ProjectName == chosenprjname
167
+ projectPartID=dfParts.iloc[np.where(dfParts['ProjectPart'].astype(str)==chosenpart)]['ProjectPartId']
168
+ dfSections=dfSections.iloc[np.where((dfSections['ProjectId']==int(projectId)) & (dfSections['ProjectPartId']==int(projectPartID)) ) ]
169
+ sections=dfSections['ProjectSection'].tolist()
170
+ return sections
171
 
172
  # %%
173
  # tableTrial=pd.DataFrame(data=[[0,'0x','part1'],[0,'1x','part2']],columns= ['ProjectId','ProjectPartId','ProjectPart'])
 
186
 
187
 
188
 
189
+
190
+ ##############################################################################
191
+ def update_df(url): #get file from drive
192
+
193
+ # threading.Timer(200.0,update_df).start()
194
+ url_1 = url.replace('/edit#gid=', '/export?format=csv&gid=')
195
+ df = pd.read_csv(url_1)
196
+ return df