Marthee commited on
Commit
3236d89
·
verified ·
1 Parent(s): 2e323e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -92
app.py CHANGED
@@ -56,6 +56,12 @@ global colorsused
56
  global pdflink
57
  # For 2.7
58
  global hatched_areas2_7
 
 
 
 
 
 
59
 
60
  # Configure Flask-Session for server-side session storage
61
  app.config["SESSION_TYPE"] = "filesystem"
@@ -101,43 +107,63 @@ def getInfo2toMeasure():
101
 
102
 
103
 
104
- @app.route('/getprojectnames/',methods=['GET'])
105
  def getprjnamesfromTestAPI():
106
  progress_updates = [] # Shared list to store progress
107
 
108
  def generate_progressProjnames():
109
- yield f"data: 10\n\n" # Initial progress
110
- # Call retrieveProjects and pass a callback to update the shared list
111
- prjnames,prjids=API.getPrjNames( progress_callback=lambda p: progress_updates.append(p))
112
- # Continuously yield progress updates from the shared list
 
 
 
 
 
 
 
113
 
114
  while progress_updates:
115
- progress = progress_updates.pop(0) # Get the next progress value
116
  yield f"data: {progress}\n\n"
117
- # Final progress and result
118
  yield f"data: 80\n\n"
119
  yield f"data: 100\n\n"
120
- result = json.dumps([prjnames,prjids])
121
  yield f"data: {result}\n\n"
122
- return Response(generate_progressProjnames(), content_type='text/event-stream')
123
 
 
124
 
125
- @app.route('/getprojectparts/<jsdata>',methods=['GET'])
126
  def getprjpartsfromTestAPI(jsdata):
127
- print('kkkk',jsdata)
128
- prjparts,partsIds=API.getprjParts(jsdata)
129
- return jsonify([prjparts,partsIds])
 
 
130
 
131
- @app.route('/getprojectsections/<jsdata>',methods=['GET'])
 
 
132
  def getprjSectionsfromTestAPI(jsdata):
133
- data=json.loads(jsdata)
134
- if data[0]=='testing':
135
- sections=API.getprjSections()
136
- print(sections)
137
- return jsonify(sections)
138
- sections=API.getprjSections(data[0],data[1])
 
 
 
 
 
 
 
 
139
  return jsonify(sections)
140
 
 
 
141
  @app.route('/getmethod/<jsdata>', methods=['GET'])
142
  def get_javascript_data(jsdata):
143
  progress_updates = [] # Shared list to store progress
@@ -1104,78 +1130,61 @@ def returnAPITables():
1104
  table1,table2,table3=API.GenerateTables()
1105
  return jsonify([table1.to_dict(),table2.to_dict(),table3.to_dict()])
1106
 
1107
-
1108
- @app.route('/refreshAPI',methods=["GET", "POST"])
1109
  def checktables():
1110
- projectname = request.args.get('prjname')
1111
- projectpart = request.args.get('prjpart')
1112
- projectpartid = request.args.get('prjpartid')
1113
- print('hgh',projectname , projectpart)
1114
- table1,table2,table3=API.GenerateTables()
1115
- url_1 = prjnamesURL.replace('/edit#gid=', '/export?format=csv&gid=')
1116
- url_2 = prjpartsURL.replace('/edit#gid=', '/export?format=csv&gid=')
1117
- url_3 = prjsectionsURL.replace('/edit#gid=', '/export?format=csv&gid=')
1118
-
1119
- table1N = pd.read_csv(url_1)
1120
- table2N = pd.read_csv(url_2)
1121
- table3N = pd.read_csv(url_3)
1122
-
1123
- t1Diff=table1N[~table1N.isin(table1)].dropna()
1124
- t1Diff1=table1[~table1.isin(table1N)].dropna()
1125
-
1126
- t2Diff=pd.DataFrame()
1127
- t2Diff1=pd.DataFrame()
1128
-
1129
- t3Diff=pd.DataFrame()
1130
- t3Diff1=pd.DataFrame()
1131
-
1132
-
1133
- if projectname:
1134
- print('here')
1135
- differentParts=[]
1136
- a = table1N.to_numpy()
1137
- row = np.where(a == projectname)
1138
- print(projectname,row)
1139
- returnString=''
1140
- t2Diff1=table2[~table2.isin(table2N)].dropna()
1141
- t2Diff=table2N[~table2N.isin(table2)].dropna()
1142
-
1143
- if projectpart and projectname:
1144
-
1145
- a1 = table1N.to_numpy()
1146
- row1 = np.where(a1 == projectname)
1147
- prjid=a1[row1[0]][0][0]
1148
- t3Diff1=table3[~table3.isin(table3N)].dropna()
1149
- t3Diff=table3N[~table3N.isin(table3)].dropna()
1150
-
1151
- returnString=''
1152
- if not t1Diff.empty or not t1Diff1.empty :
1153
- if not t1Diff.empty:
1154
- newvalues= t1Diff['ProjectName'].values
1155
- else:
1156
- newvalues= t1Diff1['ProjectName'].values
1157
- returnString=''
1158
- 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 website related processes(if any).','project',(newvalues).tolist(),table2.to_dict()]
1159
-
1160
- if (not t2Diff.empty)or( not t2Diff1.empty) :
1161
- if projectname:
1162
- for i in range(len(t2Diff1['ProjectId'].values)):
1163
- if int(t2Diff1['ProjectId'].values[i]) ==int(a[row[0]][0][0]):
1164
- differentParts.append(t2Diff1['ProjectPart'].iloc[i])
1165
- if len(differentParts)>0:
1166
- returnString=['Changes have been made in Parts. Would you like to retrieve them in the console?','part',differentParts]
1167
-
1168
- if (not t3Diff.empty) or (not t3Diff1.empty) :
1169
- differentSections=[]
1170
- if projectpart and projectname:
1171
- roww=t3Diff1.iloc[np.where((t3Diff1['ProjectId']==int(prjid) ) & (t3Diff1['ProjectPartId']==int(projectpartid)) ) ]
1172
- for i in range(len(roww['ProjectId'].values)):
1173
- differentSections.append(str(roww['ProjectSection'].iloc[i]))
1174
- if len(differentSections)>0:
1175
- returnString=['Changes have been made in Sections. Would you like to retrieve them in the console?','section',differentSections]
1176
-
1177
- print(returnString)
1178
-
1179
 
1180
  return jsonify(returnString)
1181
 
@@ -1302,4 +1311,4 @@ while 1:
1302
  if __name__ == "__main__":
1303
  runn()
1304
 
1305
-
 
56
  global pdflink
57
  # For 2.7
58
  global hatched_areas2_7
59
+ # Global cache to store last known tables
60
+ cached_tables = {
61
+ "projects": None,
62
+ "parts": None,
63
+ "sections": None
64
+ }
65
 
66
  # Configure Flask-Session for server-side session storage
67
  app.config["SESSION_TYPE"] = "filesystem"
 
107
 
108
 
109
 
110
+ @app.route('/getprojectnames/', methods=['GET'])
111
  def getprjnamesfromTestAPI():
112
  progress_updates = [] # Shared list to store progress
113
 
114
  def generate_progressProjnames():
115
+ yield f"data: 10\n\n"
116
+ prjnames, prjids = API.getPrjNames(
117
+ progress_callback=lambda p: progress_updates.append(p)
118
+ )
119
+
120
+ # save in memory
121
+ global cached_tables
122
+ cached_tables["projects"] = pd.DataFrame({
123
+ "ProjectName": prjnames,
124
+ "ProjectId": prjids
125
+ })
126
 
127
  while progress_updates:
128
+ progress = progress_updates.pop(0)
129
  yield f"data: {progress}\n\n"
 
130
  yield f"data: 80\n\n"
131
  yield f"data: 100\n\n"
132
+ result = json.dumps([prjnames, prjids])
133
  yield f"data: {result}\n\n"
 
134
 
135
+ return Response(generate_progressProjnames(), content_type='text/event-stream')
136
 
137
+ @app.route('/getprojectparts/<jsdata>', methods=['GET'])
138
  def getprjpartsfromTestAPI(jsdata):
139
+ prjparts, partsIds = API.getprjParts(jsdata)
140
+
141
+ global cached_tables
142
+ df = pd.DataFrame({"ProjectPart": prjparts, "ProjectPartId": partsIds, "ProjectId": [int(jsdata)] * len(prjparts)})
143
+ cached_tables["parts"] = df
144
 
145
+ return jsonify([prjparts, partsIds])
146
+
147
+ @app.route('/getprojectsections/<jsdata>', methods=['GET'])
148
  def getprjSectionsfromTestAPI(jsdata):
149
+ data = json.loads(jsdata)
150
+ if data[0] == 'testing':
151
+ sections = API.getprjSections()
152
+ else:
153
+ sections = API.getprjSections(data[0], data[1])
154
+
155
+ global cached_tables
156
+ df = pd.DataFrame({
157
+ "ProjectSection": sections,
158
+ "ProjectId": [data[0]] * len(sections),
159
+ "ProjectPartId": [data[1]] * len(sections)
160
+ })
161
+ cached_tables["sections"] = df
162
+
163
  return jsonify(sections)
164
 
165
+
166
+
167
  @app.route('/getmethod/<jsdata>', methods=['GET'])
168
  def get_javascript_data(jsdata):
169
  progress_updates = [] # Shared list to store progress
 
1130
  table1,table2,table3=API.GenerateTables()
1131
  return jsonify([table1.to_dict(),table2.to_dict(),table3.to_dict()])
1132
 
1133
+ @app.route('/refreshAPI', methods=["GET", "POST"])
 
1134
  def checktables():
1135
+ projectname = request.args.get('prjname')
1136
+ projectpart = request.args.get('prjpart')
1137
+ projectpartid = request.args.get('prjpartid')
1138
+
1139
+ global cached_tables
1140
+
1141
+ # Fix: check properly for missing/empty DataFrame
1142
+ if cached_tables["projects"] is None or cached_tables["projects"].empty:
1143
+ return jsonify(["No cached data available yet. Please fetch projects first."])
1144
+
1145
+ # Get fresh values from API
1146
+ new_projects, new_parts, new_sections = API.GenerateTables()
1147
+
1148
+ returnString = None
1149
+
1150
+ # 1) Compare project names
1151
+ old_names = cached_tables["projects"]["ProjectName"].tolist()
1152
+ new_names = new_projects["ProjectName"].tolist()
1153
+ if set(old_names) != set(new_names):
1154
+ added = list(set(new_names) - set(old_names))
1155
+ removed = list(set(old_names) - set(new_names))
1156
+ returnString = ["Changes in Projects", "project", {"added": added, "removed": removed}]
1157
+
1158
+ # 2) Compare parts
1159
+ elif projectname and cached_tables["parts"] is not None and not cached_tables["parts"].empty:
1160
+ prjid = new_projects.loc[new_projects['ProjectName'] == projectname, 'ProjectId'].values[0]
1161
+ old_parts = cached_tables["parts"].loc[cached_tables["parts"]["ProjectId"] == prjid, "ProjectPart"].tolist()
1162
+ new_parts_list = new_parts.loc[new_parts["ProjectId"] == prjid, "ProjectPart"].tolist()
1163
+ if set(old_parts) != set(new_parts_list):
1164
+ added = list(set(new_parts_list) - set(old_parts))
1165
+ removed = list(set(old_parts) - set(new_parts_list))
1166
+ returnString = ["Changes in Parts", "part", {"added": added, "removed": removed}]
1167
+
1168
+ # 3) Compare sections
1169
+ elif projectname and projectpart and projectpartid and cached_tables["sections"] is not None and not cached_tables["sections"].empty:
1170
+ prjid = new_projects.loc[new_projects['ProjectName'] == projectname, 'ProjectId'].values[0]
1171
+ old_sections = cached_tables["sections"]["ProjectSection"].tolist()
1172
+ new_sections_list = new_sections[
1173
+ (new_sections["ProjectId"] == prjid) &
1174
+ (new_sections["ProjectPartId"] == int(projectpartid))
1175
+ ]["ProjectSection"].tolist()
1176
+ if set(old_sections) != set(new_sections_list):
1177
+ added = list(set(new_sections_list) - set(old_sections))
1178
+ removed = list(set(old_sections) - set(new_sections_list))
1179
+ returnString = ["Changes in Sections", "section", {"added": added, "removed": removed}]
1180
+
1181
+ if not returnString:
1182
+ returnString = ["No changes detected"]
1183
+
1184
+ # ✅ update cache
1185
+ cached_tables["projects"] = new_projects
1186
+ cached_tables["parts"] = new_parts
1187
+ cached_tables["sections"] = new_sections
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1188
 
1189
  return jsonify(returnString)
1190
 
 
1311
  if __name__ == "__main__":
1312
  runn()
1313
 
1314
+