Marthee commited on
Commit
10ac252
·
verified ·
1 Parent(s): 93840d1

Update API.py

Browse files
Files changed (1) hide show
  1. API.py +92 -98
API.py CHANGED
@@ -25,6 +25,9 @@ import numpy as np
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=1481952020'
27
  prjsectionsURL= 'https://docs.google.com/spreadsheets/d/1n8AG8_lCyhh1X97Dg3ZwWUXRUm-G2JdIUgQBTzE4qgc/export?format=csv&gid=175272726'
 
 
 
28
 
29
  dfNames=pd.DataFrame()
30
  dfParts=pd.DataFrame()
@@ -98,114 +101,105 @@ def APIValues():
98
  # %% [markdown]
99
  # ### Generate Tables
100
 
101
- # %%
102
- def GenerateTables():
103
-
104
- dict1=APIValues()
105
-
106
- ##First table should ONLY contain: All project Ids and names
107
- #Second table should contain ProjId,ProjectPartId,ProjectPart
108
- #Third table should contain ProjId,ProjectPartId,ProjectSection
109
- table1=pd.DataFrame(columns= ['ProjectId','ProjectName'])
110
- table2=pd.DataFrame(columns= ['ProjectId','ProjectPartId','ProjectPart'])
111
- table3=pd.DataFrame(columns= ['ProjectId','ProjectPartId','ProjectSection'])
112
- for item in dict1:
113
- table1 = pd.concat([table1, pd.DataFrame([[item.get('id'),item.get('projectname')]], columns=table1.columns)], ignore_index=True)
114
- projectPartsList=item.get('projectparts')
115
- for part in projectPartsList:
116
- table2= pd.concat([table2,pd.DataFrame([[item.get('id'),part.get('id'),part.get('name')]], columns=table2.columns)], ignore_index=True)
117
- sections=part.get('projectsections')
118
- for section in sections:
119
- table3= pd.concat([table3,pd.DataFrame([[item.get('id'),section.get('projectPartId'),section.get('section')]], columns=table3.columns)], ignore_index=True)
120
- # table1 = pd.concat([table1, pd.DataFrame([['2222','trial1']], columns=table1.columns)], ignore_index=True)
121
- # table2= pd.concat([table2,pd.DataFrame([['2222','66','partname']], columns=table2.columns)], ignore_index=True)
122
- # table3= pd.concat([table3,pd.DataFrame([['2222','66','newSectionsss']], columns=table3.columns)], ignore_index=True)
123
- return table1,table2,table3
124
 
125
  # Set Tables to Sheets
126
  def AppendtablestoSheets():
127
  gc=AuthorizeGoogleSheets()[2]
128
  # createSheet()
129
  ws = gc.open_by_key('1n8AG8_lCyhh1X97Dg3ZwWUXRUm-G2JdIUgQBTzE4qgc')
 
 
 
130
  table1,table2,table3= GenerateTables()
131
- projectNamesSheet=ws.worksheet_by_title('Project Names')
132
- projectPartsSheet=ws.worksheet_by_title('Project Parts')
133
- projectSectionsSheet=ws.worksheet_by_title('Sections')
134
- projectNamesSheet.clear()
135
- projectPartsSheet.clear()
136
-
137
- #append tables to google sheets
138
- table1 = table1.drop_duplicates()
139
- table2 = table2.drop_duplicates()
140
- table3 = table3.drop_duplicates()
141
- projectNamesSheet.set_dataframe(start='A1',df=table1)
142
- projectPartsSheet.set_dataframe(start='A1',df=table2)
143
- projectSectionsSheet.clear()
144
- projectSectionsSheet.set_dataframe(start='A1',df=table3)
145
 
146
  # return table1,table2,table3
147
- # %%
148
- #-------------------------------------------------------------------------------CALL THIS FUNCTION TO BEGIN----------------------------------------------------------------------
149
- #--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
150
- def getPrjNames(url=prjnamesURL):
151
- #url first sheet of names
152
- # Read the CSV data from the Google Sheet URL
153
- df = pd.read_csv(prjnamesURL)
154
- return df['ProjectName'].tolist(), df['ProjectId'].tolist()
155
 
156
- # %%
157
- def getprjParts(chosenprjid,urlNames=prjnamesURL,urlparts=prjpartsURL):
158
-
159
- # url_1 = urlNames.replace('/edit#gid=', '/export?format=csv&gid=')
160
- # global dfNames
161
- # dfNames = pd.read_csv(url_1)
162
- # chosenprjname=chosenprjname.replace('"', '')
163
- # chosenprjnameList=chosenprjname.split(' ')
164
- # for i, row in dfNames.iterrows():
165
- # prjnameString=dfNames['ProjectName'].loc[i]
166
- # if all(name in prjnameString for name in chosenprjnameList):
167
- # projectNameID=dfNames.iloc[i]['ProjectId']
168
- # projectNameID=dfNames.iloc[np.where(dfNames['ProjectName'].str.startswith(chosenprjnameList[1]))]['ProjectId'] #ProjectName == chosenprjname
169
- # url_2 = urlparts.replace('/edit#gid=', '/export?format=csv&gid=')
170
- global dfParts
171
- dfParts = pd.read_csv(prjpartsURL)
172
- print(chosenprjid,dfParts)
173
- dfParts= dfParts.iloc[np.where(dfParts['ProjectId']==int(chosenprjid))]
174
- parts=dfParts['ProjectPart'].tolist()
175
- partsIds=dfParts['ProjectPartId'].tolist()
176
- print(parts)
177
- print(partsIds)
178
- #Get projectName Id
179
-
180
- return parts,partsIds
181
-
182
- # %%
183
- def getprjSections(chosenprjid, chosenpartid,urlSection=prjsectionsURL):
184
- #GetprojectSections
185
- # url_1 = urlSection.replace('/edit#gid=', '/export?format=csv&gid=')
186
- dfSections = pd.read_csv(prjsectionsURL)
187
- # chosenprjname=chosenprjname.replace('"', '')
188
- # chosenpart=chosenpart.replace('"', '')
189
- # projectId=''
190
- # projectPartID=''
191
- # chosenprjnameList=chosenprjname.split(' ')
192
- # chosenpartList=chosenpart.split(' ')
193
- # for i, row in dfNames.iterrows():
194
- # prjnameString=dfNames['ProjectName'].loc[i]
195
- # if all(name in prjnameString for name in chosenprjnameList):
196
- # projectId=dfNames.iloc[i]['ProjectId']
197
- # # projectId=dfNames.iloc[np.where(dfNames['ProjectName'].astype(str)==chosenprjname)]['ProjectId'] #ProjectName == chosenprjname
198
- # for i, row in dfParts.iterrows():
199
- # prjpartString=dfParts['ProjectPart'].loc[i]
200
- # if all(part in prjpartString for part in chosenpartList):
201
- # projectPartID=dfParts['ProjectPartId'].loc[i]
202
- # projectPartID=dfParts.iloc[np.where(dfParts['ProjectPart'].astype(str)==chosenpart)]['ProjectPartId']
203
- if chosenprjid and chosenpartid:
204
- dfSections=dfSections.iloc[np.where((dfSections['ProjectId']==int(chosenprjid)) & (dfSections['ProjectPartId']==int(chosenpartid)) ) ]
205
- sections=dfSections['ProjectSection'].tolist()
206
- return sections
207
- else:
208
- return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
 
210
 
211
  # ##############################################################################
 
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=1481952020'
27
  prjsectionsURL= 'https://docs.google.com/spreadsheets/d/1n8AG8_lCyhh1X97Dg3ZwWUXRUm-G2JdIUgQBTzE4qgc/export?format=csv&gid=175272726'
28
+ global table1
29
+ global table2
30
+ global table3
31
 
32
  dfNames=pd.DataFrame()
33
  dfParts=pd.DataFrame()
 
101
  # %% [markdown]
102
  # ### Generate Tables
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
  # Set Tables to Sheets
106
  def AppendtablestoSheets():
107
  gc=AuthorizeGoogleSheets()[2]
108
  # createSheet()
109
  ws = gc.open_by_key('1n8AG8_lCyhh1X97Dg3ZwWUXRUm-G2JdIUgQBTzE4qgc')
110
+ global table1
111
+ global table2
112
+ global table3
113
  table1,table2,table3= GenerateTables()
114
+ # projectNamesSheet=ws.worksheet_by_title('Project Names')
115
+ # projectPartsSheet=ws.worksheet_by_title('Project Parts')
116
+ # projectSectionsSheet=ws.worksheet_by_title('Sections')
117
+ # projectNamesSheet.clear()
118
+ # projectPartsSheet.clear()
119
+
120
+ # #append tables to google sheets
121
+ # table1 = table1.drop_duplicates()
122
+ # table2 = table2.drop_duplicates()
123
+ # table3 = table3.drop_duplicates()
124
+ # projectNamesSheet.set_dataframe(start='A1',df=table1)
125
+ # projectPartsSheet.set_dataframe(start='A1',df=table2)
126
+ # projectSectionsSheet.clear()
127
+ # projectSectionsSheet.set_dataframe(start='A1',df=table3)
128
 
129
  # return table1,table2,table3
 
 
 
 
 
 
 
 
130
 
131
+ def GenerateTables():
132
+ """
133
+ Optimized function to generate tables for projects, parts, and sections.
134
+ """
135
+ dict1 = APIValues()
136
+
137
+ # Initialize lists to collect data for each table
138
+ table1_data = []
139
+ table2_data = []
140
+ table3_data = []
141
+
142
+ # Initialize lists for bulk processing
143
+ table1_data = [[item.get('id'), item.get('projectname')] for item in dict1]
144
+
145
+ table2_data = [
146
+ [item.get('id'), part.get('id'), part.get('name')]
147
+ for item in dict1
148
+ for part in item.get('projectparts', [])
149
+ ]
150
+
151
+ table3_data = [
152
+ [item.get('id'), part.get('id'), section.get('section')]
153
+ for item in dict1
154
+ for part in item.get('projectparts', [])
155
+ for section in part.get('projectsections', [])
156
+ ]
157
+
158
+ # Convert lists to DataFrames
159
+ global table1
160
+ global table2
161
+ global table3
162
+ table1 = pd.DataFrame(table1_data, columns=['ProjectId', 'ProjectName'])
163
+ table2 = pd.DataFrame(table2_data, columns=['ProjectId', 'ProjectPartId', 'ProjectPart'])
164
+ table3 = pd.DataFrame(table3_data, columns=['ProjectId', 'ProjectPartId', 'ProjectSection'])
165
+
166
+ return table1, table2, table3
167
+
168
+
169
+ table1,table2,table3= GenerateTables()
170
+ def getPrjNames(progress_callback=None):
171
+ """
172
+ Returns a list of project names and IDs.
173
+ """
174
+ if progress_callback:
175
+ progress_callback(40)
176
+ global table1
177
+ if progress_callback:
178
+ progress_callback(60)
179
+ return table1['ProjectName'].tolist(), table1['ProjectId'].tolist()
180
+
181
+
182
+ def getprjParts(chosenprjid):
183
+ """
184
+ Returns parts and their IDs for a given project ID.
185
+ """
186
+ global table2
187
+ df_parts = table2[table2['ProjectId'] == int(chosenprjid)]
188
+ return df_parts['ProjectPart'].tolist(), df_parts['ProjectPartId'].tolist()
189
+
190
+
191
+ def getprjSections(chosenprjid, chosenpartid):
192
+ """
193
+ Returns sections for a given project ID and part ID.
194
+ """
195
+ global table3
196
+ if chosenprjid and chosenpartid:
197
+ df_sections = table3[
198
+ (table3['ProjectId'] == int(chosenprjid)) &
199
+ (table3['ProjectPartId'] == int(chosenpartid))
200
+ ]
201
+ return df_sections['ProjectSection'].tolist()
202
+ return None
203
 
204
 
205
  # ##############################################################################