File size: 106,976 Bytes
0c019d4 0a5809e 0c019d4 3236d89 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 |
######################################################### IMPORTS #################################################################################################################################
from __future__ import print_function
from flask import Flask, render_template,request,flash , json, url_for,g , redirect , jsonify , send_file ,make_response
import json
import fitz
from PIL import Image
import cv2
import numpy as np
import pilecaps_adr
import base64
from db import dropbox_connect
import cv2
import pandas as pd
import time
from io import BytesIO, StringIO
import urllib
import tempfile
from flask import Flask, Response
from werkzeug.wsgi import wrap_file
import tameem3_2
import pypdfium2 as pdfium
from urllib.parse import urlparse
import re
import pixelconversion
import tameem2_1
import io
from urllib.parse import unquote
import API
import MC_Templates_API
import tsadropboxretrieval
import doc_search
import google_sheet_Legend
import dxf__omar3_2
import requests
import google_sheet_to_xml
from threading import Thread
import mainDBAlaa
import datetime
import doors_fasterrcnn
import deploying_3_3
import Doors_Schedule
import Code_2_7
import Counting_Columns_2_1
# import Find_Hyperlinking_text
import ezdxf
import Azure_api
from flask import Flask, render_template, session, redirect, url_for
from flask_session import Session # Capital "S"
import os, traceback
from werkzeug.utils import secure_filename
import Legend_Detection
######################################################### FLASK APP #################################################################################################################################
app = Flask(__name__)
# Configure Flask-Session for server-side session storage
app.config["SESSION_TYPE"] = "filesystem"
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_FILE_DIR"] = "./flask_session_files"
app.secret_key = "your_secret_key"
# Initialize Flask-Session
sess = Session()
sess.init_app(app)
######################################################### GLOBAL VARIABLES #################################################################################################################################
global colorsused
global pdflink
# Global cache to store last known tables
cached_tables = {
"projects": None,
"parts": None,
"sections": None
}
global hatched_areas2_7 # For 2.7
######################################################### MAIN ERROR MESSAGE FUNCTION #################################################################################################################################
def log_error(message, issue_type="backend"):
filename = os.path.basename(__file__)
# Assign contact based on filename
if filename.startswith(('2.6', '2.8', '2.1')):
contactName = 'Tameem'
elif filename.startswith(('2.7', '3.2', '3.3')):
contactName = 'Omar'
else:
contactName = 'Marthe'
# Define issue category
if issue_type == "connection":
issue_msg = "Connection issue detected"
elif issue_type == "frontend":
issue_msg = "Frontend issue detected"
else:
issue_msg = "Backend error detected"
error_msg = f"{issue_msg}. {message}. Please contact {contactName} from the ADR Team."
print(error_msg)
return error_msg
######################################################### GUI RENDER #################################################################################################################################
@app.route("/", methods=["GET", "POST"])
def getInfotoMeasure():
try:
return render_template("gui2.html")
except Exception as e:
# capture traceback for more detail
error_details = traceback.format_exc()
error_msg = log_error(error_details)
return jsonify({"error": error_msg}), 500
######################################################### PASSWORD REDIRECTION #################################################################################################################################
@app.route("/password", methods=["GET", "POST"])
def password_page():
try:
return render_template("gui2.html")
except Exception as e:
# capture traceback for more detail
error_details = traceback.format_exc()
error_msg = log_error(error_details)
return jsonify({"error": error_msg}), 500
@app.route("/check_password", methods=["POST"])
def check_password():
try:
password = request.form.get("password")
correct_password = "c900"
if password == correct_password:
session["authenticated"] = True
return jsonify({"authenticated": True}), 200
else:
return jsonify({"authenticated": False}), 200
except Exception as e:
# capture traceback for more detail
error_details = traceback.format_exc()
error_msg = log_error(error_details)
return jsonify({"error": error_msg}), 500
######################################################### MAIN GUI PAGE #################################################################################################################################
@app.route("/mainGUI", methods=["GET", "POST"])
def main_gui():
try:
if "authenticated" not in session or not session["authenticated"]:
return redirect(url_for("password_page"))
return render_template("proposed-GUI.html")
except Exception as e:
# capture traceback for more detail
error_details = traceback.format_exc()
error_msg = log_error(error_details)
return jsonify({"error": error_msg}), 500
@app.route("/measurementConsole",methods=["GET", "POST"])
def measurementConsoleFn():
try:
return render_template("proposed-GUI.html")
except Exception as e:
# capture traceback for more detail
error_details = traceback.format_exc()
error_msg = log_error(error_details)
return jsonify({"error": error_msg}), 500
######################################################### WORD SEARCH #################################################################################################################################
@app.route('/WordSearch',methods=["GET", "POST"])
def searchDocument():
return render_template('wordSearch.html')
@app.route("/WordSearch",methods=["GET", "POST"])
def getInfo2toMeasure():
try:
# API.AppendtablestoSheets()
return render_template("wordSearch.html")
except Exception as e:
# capture traceback for more detail
error_details = traceback.format_exc()
error_msg = log_error(error_details)
return jsonify({"error": error_msg}), 500
@app.route('/searchInDocs',methods=["GET", "POST"])
def getSearchinDocs():
try:
arr=[]
values = request.get_json()
keyword=values.get('keyword')
listpfProjs=values.get('listofprojs')
print(keyword,listpfProjs)
df,img_list=doc_search.search_docs(keyword,listpfProjs)
for img in img_list:
_, buffer = cv2.imencode('.png', img)
arr.append(base64.b64encode(buffer).decode('utf-8'))
return jsonify([df.to_html(index=False, escape=False),arr])
except Exception as e:
# capture traceback for more detail
error_details = traceback.format_exc()
error_msg = log_error(error_details)
return jsonify({"error": error_msg}), 500
@app.route('/searchInFolder',methods=["GET", "POST"])
def getSearchinFolder():
try:
arr=[]
values = request.get_json()
keyword=values.get('keyword')
projname=values.get('ProjectName')
df,img_list=doc_search.slow_search(keyword=keyword,project=projname)
for img in img_list:
_, buffer = cv2.imencode('.png', img)
arr.append(base64.b64encode(buffer).decode('utf-8'))
return jsonify([df.to_html(index=False, escape=False),arr])
except Exception as e:
# capture traceback for more detail
error_details = traceback.format_exc()
error_msg = log_error(error_details)
return jsonify({"error": error_msg}), 500
#get sunburst from doc_search - NOT USED
@app.route('/getsunburst',methods=["GET", "POST"])
def getSunburst():
try:
# df=tsadropboxretrieval.GetParquetDF()
tree=doc_search.prepare_sunburst()
return jsonify(tree.to_dict())
except Exception as e:
# capture traceback for more detail
error_details = traceback.format_exc()
error_msg = log_error(error_details)
return jsonify({"error": error_msg}), 500
######################################################### TESTING ROUTE #################################################################################################################################
@app.route('/test')
def test_route():
try:
# simulate an error
x = 10 / 0
return jsonify({"result": x})
except Exception as e:
# capture traceback for more detail
error_details = traceback.format_exc()
error_msg = log_error(error_details)
return jsonify({"error": error_msg}), 500
######################################################### PROJECT NAMES RETRIEVAL #################################################################################################################################
@app.route('/getprojectnames/', methods=['GET'])
def getprjnamesfromTestAPI():
try:
prjnames, prjids = API.getPrjNames()
# Save in memory
global cached_tables
cached_tables["projects"] = pd.DataFrame({
"ProjectName": prjnames,
"ProjectId": prjids
})
return jsonify([prjnames, prjids])
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
# return Response(generate_progressPrjnames(), content_type='text/event-stream')
######################################################### PROJECT PARTS RETRIEVAL #################################################################################################################################
@app.route('/getprojectparts/<jsdata>', methods=['GET'])
def getprjpartsfromTestAPI(jsdata):
try:
prjparts, partsIds = API.getprjParts(jsdata)
global cached_tables
df = pd.DataFrame({"ProjectPart": prjparts, "ProjectPartId": partsIds, "ProjectId": [int(jsdata)] * len(prjparts)})
cached_tables["parts"] = df
return jsonify([prjparts, partsIds])
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
######################################################### PROJECT SECTIONS RETRIEVAL #################################################################################################################################
@app.route('/getprojectsections/<jsdata>', methods=['GET'])
def getprjSectionsfromTestAPI(jsdata):
try:
data = json.loads(jsdata)
if data[0] == 'testing':
sections = API.getprjSections()
project_id = data[0]
project_part_id = None # No second parameter in this case
else:
# Expecting 2 values
project_id = data[0]
project_part_id = data[1] if len(data) > 1 else None
sections = API.getprjSections(project_id, project_part_id)
global cached_tables
df = pd.DataFrame({
"ProjectSection": sections,
"ProjectId": [project_id] * len(sections),
"ProjectPartId": [project_part_id] * len(sections)
})
cached_tables["sections"] = df
return jsonify(sections)
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
######################################################### PROJECT PDF NAMES RETRIEVAL #################################################################################################################################
@app.route('/getmethod/<jsdata>', methods=['GET'])
def get_javascript_data(jsdata):
progress_updates = [] # Shared list to store progress
def generate_progress():
try:
# raise ConnectionError("Failed to reach external service")
yield f"data: 5\n\n" # Initial progress
yield f"data: 10\n\n" # Initial progress
# Call retrieveProjects and pass a callback to update the shared list
documentsToMeasure, RelevantDocuments, extracted_path = tsadropboxretrieval.retrieveProjects(
jsdata,
progress_callback=lambda p: progress_updates.append(p)
)
# Continuously yield progress updates from the shared list
while progress_updates:
progress = progress_updates.pop(0) # Get the next progress value
yield f"data: {progress}\n\n"
# Final progress and result
yield f"data: 100\n\n"
result = json.dumps([documentsToMeasure, RelevantDocuments, extracted_path])
yield f"data: {result}\n\n"
except (ConnectionError, TimeoutError) as e:
# Use your log_error function for connection issues
error_msg = log_error(str(e), issue_type="connection")
# Send it as SSE custom event
yield f"event:error\ndata:{json.dumps({'error': error_msg})}\n\n"
except Exception:
# Any other backend error
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
# Send it as SSE custom event
yield f"event:error\ndata:{json.dumps({'error': error_msg})}\n\n"
return Response(generate_progress(), content_type='text/event-stream')
#---------------------------------------------------------------
######################################################### TABLE DETECTION #################################################################################################################################
@app.route('/tableDetection',methods=["GET", "POST"])
def TableDetection():
try:
return render_template('tableDetection.html')
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
#####################not added in gui yettt
@app.route('/savedetectedtables/',methods=["POST"])
def table():
try:
# 1. Get PDF bytes from Dropbox
tt = eval(request.form.get('answers')) # consider json.loads instead of eval
print("Value of tt = ",tt)
print("Value of tt[0] = ",tt[0])
pdflist=[]
pdfnames=[]
if tt[0].startswith('http'):
pdf_path=tt[0]
if pdf_path and ('http' in pdf_path or 'dropbox' in pdf_path):
pdf_path = pdf_path.replace('dl=0', 'dl=1')
response = requests.get(pdf_path)
pdf_content = BytesIO(response.content)
if not pdf_content:
raise ValueError("No valid PDF content found.")
excel_io = Azure_api.detect_tables([response.content],['pdfname.pdf'])
else:
for i in range(len(tt[0])):
print("Value of tt[0][i] = ",tt[0][i])
pdfpath, _ = tsadropboxretrieval.getPathtoPDF_File(nameofPDF=tt[0][i])
dbx = tsadropboxretrieval.ADR_Access_DropboxTeam('user')
_, res = dbx.files_download(path=pdfpath)
pdf_bytes = res.content
pdflist.append(pdf_bytes)
pdfnames.append(pdfpath)
# 2. Generate in-memory Excel
print("pdflist = ",pdflist)
excel_io = Azure_api.detect_tables(pdflist,pdfnames)
# 3. Send it as a downloadable .xlsx
if excel_io is None:
# No tables → return a JSON error (or another response of your choice)
return ('', 204)
return send_file(
excel_io,
as_attachment=True,
download_name='detected_tables.xlsx',
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
######################################################### TESTING MEASUREMENT USING LINKS #################################################################################################################################
@app.route('/TestingMeasurement',methods=["GET", "POST"])
def TestingMeasurement():
try:
return render_template('TestingMeasurement.html')
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
######################################################### FIND FUNCTIONS #################################################################################################################################
#####################not added in gui yettt
@app.route('/FindInitialMarkups',methods=["GET", "POST"])
def FindInitialMarkups():
try:
return render_template('FindInitialMarkups.html')
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
pdf_content = None
pageNumTextFound = 0
BASE_URL = "https://marthee-nbslink.hf.space" # Hugging Face Spaces base URL
# @app.route("/", methods=["GET", "POST"])
# def thismain():
# print('ayhaga')
# return render_template("gui.html")
@app.route('/view-pdf', methods=['GET'])
def download_pdf():
try:
# Manually parse the query parameters
full_query_string = request.query_string.decode() # Get raw query string
parsed_params = urllib.parse.parse_qs(full_query_string) # Parse it
# Extract pdfLink and keyword manually
pdf_link = parsed_params.get('pdfLink', [None])[0]
keyword = parsed_params.get('keyword', [None])[0]
if not pdf_link or not keyword:
return "Missing required parameters.", 400
# Decode the extracted values
pdf_link = urllib.parse.unquote(pdf_link)
keyword = urllib.parse.unquote(keyword)
# If the keyword is a JSON string, convert it back to a list
try:
keyword = json.loads(keyword)
except json.JSONDecodeError:
keyword = [keyword] # Treat it as a single keyword if not JSON
print("Extracted PDF Link:", pdf_link)
print("Extracted Keywords:", keyword)
createDF=False
pdf_content = Find_Hyperlinking_text.annotate_text_from_pdf([pdf_link], keyword)[0]
if pdf_content is None:
return "PDF content not found.", 404
pdf_bytes = BytesIO(pdf_content)
return send_file(
pdf_bytes,
mimetype='application/pdf',
as_attachment=False,
download_name=f"annotated_page_{pageNumTextFound}.pdf"
)
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
@app.route('/api/process-data', methods=['POST'])
def receive_pdf_data():
try:
global pdf_content, pageNumTextFound
# Get PDF link and keyword from finddata()
pdfLink, keyword = finddata()
if not pdfLink or not keyword:
return jsonify({"error": "Both 'pdfLink' and 'keyword' must be provided."}), 400
try:
print(pdfLink, keyword)
pdfbytes, pdf_document , df ,tablepdfoutput= Find_Hyperlinking_text.annotate_text_from_pdf([pdfLink], keyword)
dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
# Get metadata using the shared link
metadata = dbxTeam.sharing_get_shared_link_metadata(pdfLink)
dbPath='/TSA JOBS/ADR Test/FIND/'
pdflink= tsadropboxretrieval.uploadanyFile(doc=pdf_document,path=dbPath,pdfname=metadata.name) #doc=doc,pdfname=path,pdfpath=pdfpath+'Measured Plan/
print('LINKS0',pdflink)
dbPath='/TSA JOBS/ADR Test/FIND/'
tablepdfLink=tsadropboxretrieval.uploadanyFile(doc=tablepdfoutput,path=dbPath,pdfname=metadata.name.rsplit(".pdf", 1)[0] +' Markup Summary'+'.pdf')
print(f"PDF successfully uploaded to Dropbox at")
print('LINKS1',tablepdfLink)
return jsonify({
"message": "PDF processed successfully.",
"PDF_MarkedUp": pdflink,
'Table_PDF_Markup_Summary': tablepdfLink
})
except Exception as e:
return jsonify({"error": str(e)}), 500
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
def finddata():
pdfLink = 'https://www.dropbox.com/scl/fi/hnp4mqigb51a5kp89kgfa/00801-ARC-20-ZZ-S-A-0002.pdf?rlkey=45abeoebzqw4qwnslnei6dkd6&st=m4yrcjm2&dl=1'
keyword = ['115 INTEGRATED MRI ROOM LININGS', '310 ACCURACY']
return pdfLink, keyword
######################################################### LEGEND DIRECTORY #################################################################################################################################
@app.route('/legends',methods=["GET", "POST"])
def legendDirectory():
try:
return render_template('legendDirectory.html')
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
#Google sheet links
#####################not added in gui yettt
@app.route('/getdrivelinks/<jsdata>',methods=["GET", "POST"])
def getlinkscreated(jsdata):
try:
spreadsheet_service,drive_service,gc= google_sheet_Legend.authorizeLegend()
ids=gc.spreadsheet_ids()
titles=gc.spreadsheet_titles()
allpaths=[]
print('HEREEEEEEEEEE')
# for title in titles:
for i in range(0,len(titles)):
print('titles',titles[i])
if not (titles[i].startswith('API') or (titles[i].startswith('Dropbox')) ) :
ws=gc.open(titles[i])
path_metadata = ws.get_developer_metadata('path')
print(path_metadata)
allpaths.append([titles[i], ws.get_developer_metadata('path')[0].value , drive_service.files().get(fileId=ids[i],fields="createdTime, modifiedTime").execute() ,ids[i] ])
return jsonify(allpaths)
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
######################################################### MC NAMES RETRIEVAL #################################################################################################################################
@app.route('/RetrieveMCTNames/',methods=['GET'])
def CallAPIforMCTNames():
# print(jsdata)
try:
DictionaryOfTemplates=MC_Templates_API.RetrieveMC_Templates_API()
# jsdata=jsdata.replace('"', '')
print('here')
return jsonify(DictionaryOfTemplates) #[str(jsdata).lower()]
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
######################################################### HELPER FUNCTIONS: MARTHE (1.0) #################################################################################################################################
#Hex value to RGB value
def hexRGB(color):
try:
color=color.lstrip('#')
color= tuple(int(color[i:i+2], 16) for i in (0, 2, 4)) #hex to rgb
color=list(color) #rgb to bgr
return color
except Exception as e:
# capture traceback for more detail
error_details = traceback.format_exc()
error_msg = log_error(error_details)
return jsonify({"error": error_msg}), 500
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
######################################################### GUI: UPDATE IMAGE PREVIEW #################################################################################################################################
@app.route('/updatepreviewimg/<jsdata>',methods=["GET", "POST"])
def getfromdropboxImg(jsdata):
try:
# vv = eval(request.form.get('pdflink'))
pdfpath=''
# jsdata=jsdata.replace('"', '')
jsdata=eval(jsdata)
print('pdfnameeee==',jsdata)
dbPath='/TSA JOBS/ADR Test/'+jsdata[0][0]+'/'+jsdata[0][1]+'/'+jsdata[0][2]+'/Measured Plan/'+jsdata[1]
print(dbPath)
dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
md, res =dbxTeam.files_download(path=dbPath)
data = res.content
doc = fitz.open("pdf",data)
page=doc[0]
pix = page.get_pixmap() # render page to an image
pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
img=np.array(pl)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
_, buffer = cv2.imencode('.png', img)
return base64.b64encode(buffer).decode('utf-8')
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
######################################################### MAIN FUNCTION WHEN USER CLICKS MEASURE BUTTON #################################################################################################################################
@app.route('/savebase64Img/',methods=["POST"])
def stringToRGB():
try:
vv = eval(request.form.get('answers'))
# vv = json.loads(answers) # JSON null -> Python None, true->True, false->False
# print("vv for 2.7=", vv)
if type(vv[0]) == list:
vv[0] = vv[0][0]
if vv[0].startswith('http'):
if ( vv[5].startswith('3.2') or vv[5].startswith('3.3') or vv[5].startswith('2.7')) :
print('3.2 section')
# pdflink=vv[0]
# encoded_url = request.args.get('url')
link = urllib.parse.unquote(vv[0].strip('"'))
if link and ('http' in link or 'dropbox' in link):
if 'dl=0' in link:
link = link.replace('dl=0', 'dl=1')
elif 'www.dropbox.com' in link and '?dl=1' not in link:
link += '?dl=1'
response = requests.get(link)
pdf_content = BytesIO(response.content)
if not pdf_content:
raise ValueError("No valid PDF content found.")
if 'file' not in request.files:
print('error, No file part in the request')
else:
file = request.files['file']
print('file done, measuring')
arr=measureproject(result=vv,dxffile=file,pdf_content=pdf_content)
return jsonify(arr)
if vv[5].startswith('2.8') or vv[5].startswith('2.6'):
# or vv[5].startswith('2.1')
#print(f"el mawgood fe vv[0]: {vv[0]}")
# arr_s = []
# dataDocs = []
# pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF= str(v))
link = urllib.parse.unquote(vv[0].strip('"'))
if link and ('http' in link or 'dropbox' in link):
if 'dl=0' in link:
link = link.replace('dl=0', 'dl=1')
elif 'www.dropbox.com' in link and '?dl=1' not in link:
link += '?dl=1'
response = requests.get(link)
pdf_content = BytesIO(response.content)
doc = fitz.open(stream=pdf_content, filetype="pdf")
# doc = fitz.open("pdf",dataDoc)
page=doc[0]
if page.rotation!=0:
page.set_rotation(0)
pix = page.get_pixmap(dpi=300) # render page to an image
pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
if 'file' not in request.files:
print('error, No file part in the request')
else:
csvfile = request.files['csvfile']
print('csvfile done, measuring')
arr=measureproject(result=vv,img=pl,pdf_content=pdf_content,csvfile=csvfile)
else:
if ( vv[5][2].startswith('3.2') or vv[5][2].startswith('3.3') or vv[5][2].startswith('2.7')) :
print('3.2 section')
pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF=vv[0])
dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
md, res =dbxTeam.files_download(path=pdfpath)
dataDoc = res.content
if 'file' not in request.files:
print('error, No file part in the request')
else:
file = request.files['file']
print('file done, measuring')
arr=measureproject(vv,dataDoc,0,file)
return jsonify(arr)
if vv[5][2].startswith('1.0'):
opencv_img,dataDoc = plan2img( str(vv[0]) )
if vv[1]==220:
imgdata = base64.b64decode(vv[6])
img=Image.open(io.BytesIO(imgdata))
opencv_img= cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
arr=measureproject(vv,dataDoc,opencv_img)
if vv[5][2].startswith('2.1'):
#vv = eval(request.form.get('answers'))
#print(f"el mawgood fe vv[0] fe 2.1: {vv[0]}")
#print(f"el mawgood fe vv[6] fe 2.1: {vv[6]}")
#print(f"el mawgood fe vv fe 2.1: {vv}")
#answers = json.loads(request.form['answers'])
data_url = vv[6] # "data:image/png;base64,..."
header, b64 = data_url.split(',', 1)
segmented_img_bytes = base64.b64decode(b64)
pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF= str(vv[0]))
dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
md, res =dbxTeam.files_download(path=pdfpath)
dataDoc = res.content
opencv_img,_ = convert2img2_1(str(vv[0]))
#imgdata = base64.b64decode(vv[6])
#img=Image.open(io.BytesIO(imgdata))
# opencv_img= cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
arr=measureproject(vv,dataDoc,segmented_img_bytes)
if vv[5][2].startswith('2.8') or vv[5][2].startswith('2.6'):
vv = eval(request.form.get('answers'))
print(f"el mawgood fe vv[0]: {vv[0]}")
arr_s = []
dataDocs = []
pls = []
for v in vv[0]:
pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF= str(v))
dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
md, res =dbxTeam.files_download(path=pdfpath)
dataDoc = res.content
dataDocs.append(dataDoc)
doc = fitz.open("pdf",dataDoc)
page=doc[0]
if page.rotation!=0:
page.set_rotation(0)
pix = page.get_pixmap(dpi=300) # render page to an image
pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
pls.append(pl)
arr=measureproject(vv,dataDocs,pls)
# ---- CSV FILE ----#
#if 'file' not in request.files:
# print('error, No file part in the request')
# arr=measureproject(vv,dataDocs,pls)
#else:
# csvfile = request.files['csvfile']
# print('csvfile done, measuring')
#arr=measureproject(vv,dataDocs,pls,csvfile=csvfile)
#pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF= str(vv[0]))
#dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
#md, res =dbxTeam.files_download(path=pdfpath)
#dataDoc = res.content
#doc = fitz.open("pdf",dataDoc)
#page=doc[0]
#if page.rotation!=0:
# page.set_rotation(0)
#pix = page.get_pixmap(dpi=300) # render page to an image
#pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
#arr=measureproject(vv,dataDoc,pl)
return jsonify(arr)
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
@app.route('/2.1Trial/',methods=["POST"])
def measure2_1():
try:
name = request.get_json()
result=name.get('allvalues')
arr=measureproject(result)
return arr
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
#MAIN FUNCTION -- calls python code to measure the chosen plan from the interface
# @app.route('/projecttomeasure/<jsdata>',methods=["GET","POST"])
def measureproject(result,dataDoc=0,img=0,dxffile=0,pdf_content=0,csvfile=0):
try:
colorarr=[]
global pdflink
pdfpath='/'
# result = json.loads(jsdata)
if pdf_content:
section=result[5]
pdfpath+='testinglink/'
else:
section=result[5][2]
for word in result[5]:
pdfpath+=word +'/'
################################# -1.0- #################################
arr=[]
if section.startswith('1.0'): #section value - 1.0 substructure- pile caps
for item in result[2]:
# item1 ='#'+item
c=hexRGB(item)
colorarr.append(c)
print('RATIOS=',result[3], result[4])
# cv2.imwrite(img,'imgg.png')
# global pdflink
imgPerimeter1,image_new1,SimilarAreaDictionary , colorsUsed,spreadsheet_url, spreadsheetId,list1, pdflink, areas_Perimeters, namepathArr =pilecaps_adr.drawAllContours(dataDoc,img,result[1],colorarr, result[3], result[4], result[0],pdfpath)
_, buffer = cv2.imencode('.png', image_new1)
arr=[base64.b64encode(buffer).decode('utf-8'),SimilarAreaDictionary.to_dict(),spreadsheet_url , spreadsheetId,colorsUsed,list1.to_dict(), pdflink, areas_Perimeters, namepathArr]
################################# -3.2- #################################
# elif result[5][2].startswith('3.2'): #section value - 3.2 floor finishes
# print('IN HEREEEE 3.2')
# dxfpath=dxffile.read()
# with tempfile.NamedTemporaryFile(suffix='.dxf', delete=False) as temp_file:
# temp_file.write(dxfpath)
# temp_filename = temp_file.name
# print(temp_filename)
# doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas=dxf__omar3_2.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4] , pdfpath,result[0])
# dbPath='/TSA JOBS/ADR Test'+pdfpath+'Measured Plan/'
# pdflink= tsadropboxretrieval.uploadanyFile(doc=doc,path=dbPath,pdfname=result[0]) #doc=doc,pdfname=path,pdfpath=pdfpath+'Measured Plan/'
# _, buffer = cv2.imencode('.png', outputimg)
# arr=[ base64.b64encode(buffer).decode('utf-8'),SimilarAreaDictionary.to_dict(), spreadsheet_url,spreadsheetId,[],list1.to_dict(),pdflink,hatched_areas,namepathArr]# , spreadsheetId, spreadsheet_url , list1.to_dict()]
################################# -3.3 or 3.2- #################################
elif section.startswith('3.3') or section.startswith('3.2'): #section value - 3.2 floor finishes
print('code of 3.3 and 3.2')
dxfpath=dxffile.read()
with tempfile.NamedTemporaryFile(suffix='.dxf', delete=False) as temp_file:
temp_file.write(dxfpath)
temp_filename = temp_file.name
print(temp_filename)
CorrectionRatio=result[8]
print("result = ",result)
SearchArray=result[6]
parsed_url = urlparse(result[0])
filename = parsed_url.path.split('/')[-1]
print(filename)
nameofpdf=filename
points_Of_drawing_Canvas=drawonpdf(nameofpdf,result[7]) ########################## points of manual drawing hereee
if pdf_content:
if section.startswith('3.3'):
doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas , bax_pretty_xml, column_xml=deploying_3_3.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4] ,SearchArray,CorrectionRatio,points_Of_drawing_Canvas,pdfpath,result[0],pdf_content)
else:
doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas, bax_pretty_xml, column_xml=dxf__omar3_2.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4] ,SearchArray,CorrectionRatio,points_Of_drawing_Canvas,pdfpath,result[0],pdf_content)
else:
if section.startswith('3.3'):
doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas , bax_pretty_xml, column_xml=deploying_3_3.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4] ,SearchArray,CorrectionRatio,points_Of_drawing_Canvas,pdfpath,result[0])
else:
doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas, bax_pretty_xml, column_xml=dxf__omar3_2.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4] ,SearchArray,CorrectionRatio,points_Of_drawing_Canvas,pdfpath,result[0])
global colorsused
colorsused=list(SimilarAreaDictionary['Color'])
dbPath='/TSA JOBS/ADR Test'+pdfpath+'Measured Plan/'
print(dbPath,result[0])
pdflink= tsadropboxretrieval.uploadanyFile(doc=doc,path=dbPath,pdfname=nameofpdf)
_, buffer = cv2.imencode('.png', outputimg)
# if section.startswith('3.3'):
# arr=[ base64.b64encode(buffer).decode('utf-8'),SimilarAreaDictionary.to_dict(), spreadsheet_url,spreadsheetId,[],list1.to_dict(),pdflink,hatched_areas,namepathArr]#,hatched_areas,namepathArr]# , spreadsheetId, spreadsheet_url , list1.to_dict()]
# else:
bax_b64 = base64.b64encode(bax_pretty_xml.encode("utf-8")).decode("ascii") #base64.b64encode(bax_pretty_xml).decode("ascii")
xml_b64 = base64.b64encode(column_xml.encode("utf-8")).decode("ascii")
bax_link= tsadropboxretrieval.upload_string_file(content_str=bax_pretty_xml,filename="baxfile.bax",path=dbPath)
xml_link= tsadropboxretrieval.upload_string_file(content_str=column_xml,filename="customCols.xml",path=dbPath)
arr=[ base64.b64encode(buffer).decode('utf-8'),SimilarAreaDictionary.to_dict(), spreadsheet_url,spreadsheetId,[],list1.to_dict(),pdflink,hatched_areas,namepathArr ,bax_b64,xml_b64, bax_link,xml_link]
################################# 2.7- #################################
elif section.startswith('2.7') : #section value - 2.7 floor finishes
print('code of 2.7')
dxfpath=dxffile.read()
with tempfile.NamedTemporaryFile(suffix='.dxf', delete=False) as temp_file:
temp_file.write(dxfpath)
temp_filename = temp_file.name
print(temp_filename)
CorrectionRatio=result[10]
SearchArray=result[6]
CollectedColors=result[7]
print("CollectedColors in app.py = ",CollectedColors)
Thickness=result[8]
print("result[9] = ",result[9])
parsed_url = urlparse(result[0])
filename = parsed_url.path.split('/')[-1]
print(filename)
nameofpdf=filename
points_Of_drawing_Canvas=drawonpdf(nameofpdf,result[9]) ########################## points of manual drawing hereee
print("result for 2.7 = ",result)
print("SearchArray = ",SearchArray)
global hatched_areas2_7
if pdf_content:
doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas,bax_pretty_xml,column_xml=Code_2_7.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4],SearchArray,CorrectionRatio,CollectedColors,points_Of_drawing_Canvas,Thickness, pdfpath,result[0],pdf_content)
else:
doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas,bax_pretty_xml,column_xml=Code_2_7.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4],SearchArray,CorrectionRatio,CollectedColors,points_Of_drawing_Canvas,Thickness, pdfpath,result[0])
# global colorsused
hatched_areas2_7=hatched_areas
colorsused=list(SimilarAreaDictionary['Color'])
dbPath='/TSA JOBS/ADR Test'+pdfpath+'Measured Plan/'
print(dbPath,result[0])
pdflink= tsadropboxretrieval.uploadanyFile(doc=doc,path=dbPath,pdfname=nameofpdf) #doc=doc,pdfname=path,pdfpath=pdfpath+'Measured Plan/'
_, buffer = cv2.imencode('.png', outputimg)
bax_b64 = base64.b64encode(bax_pretty_xml.encode("utf-8")).decode("ascii") #base64.b64encode(bax_pretty_xml).decode("ascii")
xml_b64 = base64.b64encode(column_xml.encode("utf-8")).decode("ascii")
bax_link= tsadropboxretrieval.upload_string_file(content_str=bax_pretty_xml,filename="baxfile.bax",path=dbPath)
xml_link= tsadropboxretrieval.upload_string_file(content_str=column_xml,filename="customCols.xml",path=dbPath)
arr=[ base64.b64encode(buffer).decode('utf-8'),SimilarAreaDictionary.to_dict(), spreadsheet_url,spreadsheetId,[],list1.to_dict(),pdflink,[],namepathArr,bax_b64,xml_b64, bax_link,xml_link]#,hatched_areas,namepathArr]# , spreadsheetId, spreadsheet_url , list1.to_dict()]
# arr=[ base64.b64encode(buffer).decode('utf-8'),SimilarAreaDictionary.to_dict(), spreadsheet_url,spreadsheetId,[],list1.to_dict(),pdflink,hatched_areas,namepathArr]#,hatched_areas,namepathArr]# , spreadsheetId, spreadsheet_url , list1.to_dict()]
################################# -2.8- #################################
elif section.startswith('2.8') or section.startswith('2.6'): #section value - 2.8 floor finishes
#vv[0] array of pdf names
imgss=[]
dpxlinks=[]
legendLinks=[]
listofmarkups=[]
SearchArray=result[7]
print('searchhh:',SearchArray)
print('csv',csvfile)
CSV_UPLOAD_DIR = os.path.join(os.path.dirname(__file__), "uploaded_csv")
# annotatedimg,pdf_document,spreadsheet_url, list1 , df_doors=doors_fasterrcnn.main_run(img,dataDoc,'separated_classes_all.pth',result[0],pdfpath, result[4]) #single_double.pth
if pdf_content:
link = urllib.parse.unquote(result[6].strip('"'))
if link and ('http' in link or 'dropbox' in link):
if 'dl=0' in link:
link = link.replace('dl=0', 'dl=1')
elif 'www.dropbox.com' in link and '?dl=1' not in link:
link += '?dl=1'
response = requests.get(link)
pdf_contentSched = BytesIO(response.content)
# pdf_contentSched = fitz.open(stream=pdf_contentSched, filetype="pdf")
annotatedimgs, pdf_document , list1, repeated_labels , not_found, bax_pretty_xml, column_xml =Doors_Schedule.mainRun(pdf_contentSched, dataDoc, SearchArray,pdf_content,pdf_contentSched)
else:
sch_csv_pdf = False
file_names = result[6] # ["7376....csv", "something.csv"] in CSV mode or PDF names in PDF mode
if not file_names:
raise ValueError("No schedule files provided in result[6].")
first_name = str(file_names[0]).lower()
if first_name.endswith(".csv"):
# 🔹 CSV MODE
# Ensure folder exists
os.makedirs(CSV_UPLOAD_DIR, exist_ok=True)
# Grab uploaded CSV files from the request
# name="csvFilename" from your <input type="file">
uploaded_csvs = request.files.getlist("csvFilename")
saved_paths = []
for f in uploaded_csvs:
if not f.filename:
continue
safe_name = secure_filename(f.filename)
save_path = os.path.join(CSV_UPLOAD_DIR, safe_name)
f.save(save_path)
saved_paths.append(save_path)
annotatedimgs, pdf_document, list1, repeated_labels, not_found, bax_pretty_xml, column_xml = Doors_Schedule.mainRun(
saved_paths, # 👉 ["file1.csv", "file2.csv"]
dataDoc,
SearchArray,
sch_csv_pdf
)
else:
dataDocDoorsSchedule = []
sch_csv_pdf = True
for r in result[6]:
pdfpathDoors,_=tsadropboxretrieval.getPathtoPDF_File(nameofPDF= r)
dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
md, resDoors =dbxTeam.files_download(path=pdfpathDoors)
dataDocDoorsSchedule.append(resDoors.content)
annotatedimgs, pdf_document , list1, repeated_labels , not_found, bax_pretty_xml, column_xml =Doors_Schedule.mainRun(dataDocDoorsSchedule, dataDoc, SearchArray, sch_csv_pdf)
dbPath='/TSA JOBS/ADR Test'+pdfpath+'Measured Plan/'
pdflink= tsadropboxretrieval.uploadanyFile(doc=pdf_document,path=dbPath,pdfname="combined_output.pdf") #doc=doc,pdfname=path,pdfpath=pdfpath+'Measured Plan/
repeatedLabelsReturn=''
NotFoundReturn=''
if len(repeated_labels)>0:
repeatedLabelsReturn=repeated_labels
if len(not_found)>0:
NotFoundReturn=not_found # , spreadsheetId, spreadsheet_url , list1.to_dict()]
annotatedimgsBuffered=[]
for b in annotatedimgs:
_, buffer = cv2.imencode('.png', b)
# Convert buffer (ndarray) to bytes and then base64 string
b64_str = base64.b64encode(buffer).decode('utf-8')
annotatedimgsBuffered.append(b64_str)
bax_b64 = base64.b64encode(bax_pretty_xml.encode("utf-8")).decode("ascii") #base64.b64encode(bax_pretty_xml).decode("ascii")
xml_b64 = base64.b64encode(column_xml.encode("utf-8")).decode("ascii")
dbPath='/TSA JOBS/ADR Test'+pdfpath+'Measured Plan/'
bax_link= tsadropboxretrieval.upload_string_file(content_str=bax_pretty_xml,filename="baxfile.bax",path=dbPath)
xml_link= tsadropboxretrieval.upload_string_file(content_str=column_xml,filename="customCols.xml",path=dbPath)
arr = [
annotatedimgsBuffered,
pdflink,
list1.to_dict(),
str(repeatedLabelsReturn),
str(NotFoundReturn),
bax_b64,
xml_b64,
bax_link,
xml_link
]
################################# -2.1- #################################
elif section.startswith('2.1'): #section value - 2.8 floor finishes
#vv[0] array of pdf names
imgss=[]
dpxlinks=[]
legendLinks=[]
listofmarkups=[]
data_url = result[6] # "data:image/png;base64,..."
header, b64 = data_url.split(',', 1)
segmented_img_bytes = base64.b64decode(b64)
#annotatedimg,pdf_document,spreadsheet_url, list1 , df_doors = Counting_Columns_2_1.mainfun(dataDoc, segmented_img_bytes)
# dbPath='/TSA JOBS/ADR Test'+pdfpath+'Measured Plan/'
# pdflink= tsadropboxretrieval.uploadanyFile(doc=pdf_document,path=dbPath,pdfname=result[0]) #doc=doc,pdfname=path,pdfpath=pdfpath+'Measured Plan/
# _, buffer = cv2.imencode('.png', annotatedimg)
#arr=[base64.b64encode(buffer).decode('utf-8') ,pdflink,spreadsheet_url,list1.to_dict(), df_doors.to_dict()]# , spreadsheetId, spreadsheet_url , list1.to_dict()]
output_dict = Counting_Columns_2_1.mainfun(dataDoc, segmented_img_bytes)
arr = output_dict
################################# -2.2- #################################
# elif result[5][2].startswith('2.2'): #section value - 2.2 rc slabs
#add here python code
#
#link (add this to ur code)
# pdflink= db.dropbox_upload_file(doc=doc,pdfname=path,pdfpath=pdfpath)
# gc,spreadsheet_service,spreadsheetId, spreadsheet_url , namepathArr= pilecaps_adr.legendGoogleSheets(df,plan ,pdfpath)
#output img
# _, buffer = cv2.imencode('.png', outputimg)
#let first result to be the img
#return results in arr=[base64.b64encode(buffer).decode('utf-8'),pdflink,spreadsheetId,spreadsheet_url] like the previous sections in the above lines
# elif result[5][2].startswith('2.1'): #section value - 2.1 frames
# url = tameem2_1.mainfun( result[0], pdfpath)
# return jsonify([url])
return arr
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
######################################################### HELPER FUNCTIONS: MARTHE/OMAR #################################################################################################################################
def drawonpdf(nameofpdf,coords):
try:
pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF=nameofpdf)
dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
md, res =dbxTeam.files_download(path=pdfpath)
data = res.content
doc = fitz.open("pdf",data)
page=doc[0]
for shape in coords:
if not isinstance(shape, dict):
continue
points_list = shape.get("coordinates", [])
if not isinstance(points_list, list) or len(points_list) < 2:
continue
# Extract coordinates
vertices = [(p["x"], p["y"]) for p in points_list if "x" in p and "y" in p]
# Convert to fitz.Point objects and apply derotation
points = [fitz.Point(x, y) * page.derotation_matrix for x, y in vertices]
# --- 🟢 Use polygon annotation ---
if len(points) > 2:
annot = page.add_polygon_annot(points)
else:
annot = page.add_polyline_annot(points)
# Style annotation
annot.set_colors(stroke=(1, 0, 0)) # red border
annot.set_border(width=1)
annot.update()
# doc.save("output.pdf")
return points
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
@app.route("/wallsLegend/", methods=["POST"])
def callOmarLegend():
# TEXT DATA
nameofPDF = request.form.get("nameofPDF")
print('nameofPDF',nameofPDF)
groupedValues = json.loads(request.form.get("groupedValues"))
print('groupedValues',groupedValues)
# FILE (DXF)
if "file" not in request.files:
return jsonify({"error": "No file received"}), 400
file = request.files["file"]
with tempfile.NamedTemporaryFile(suffix=".dxf", delete=False) as temp_file:
temp_file.write(file.read())
temp_filename = temp_file.name
# Get PDF from Dropbox
pdfpath, pdflink = tsadropboxretrieval.getPathtoPDF_File(nameofPDF=nameofPDF)
dbxTeam = tsadropboxretrieval.ADR_Access_DropboxTeam("user")
md, res = dbxTeam.files_download(path=pdfpath)
dataDoc = res.content
# Run your legend detection
colorsArray = Legend_Detection.Legend_Detection(dataDoc,temp_filename,groupedValues)
print('colorsArray',colorsArray)
return jsonify(colorsArray)
######################################################### GUI: CANVAS #################################################################################################################################
@app.route("/canvaspdftoimgBackground/<jsdata>",methods=["GET", "POST"])
def pdftoimgCanvas(jsdata):
try:
img=plan2img(jsdata)[0]
_, buffer = cv2.imencode('.png', img)
arr=[base64.b64encode(buffer).decode('utf-8') , img.shape[0],img.shape[1]]
return jsonify(arr)
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
@app.route("/canvaspdftoimg/<jsdata>",methods=["GET", "POST"])
def pdftoimgCanvas2(jsdata):
try:
img=convert2img2_1(jsdata)[0]
_, buffer = cv2.imencode('.png', img)
arr=[base64.b64encode(buffer).decode('utf-8') , img.shape[0],img.shape[1]]
return jsonify(arr)
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
#pdf to img
######################################################### HELPER FUNCTIONS: MARTHE #################################################################################################################################
def plan2img(nameofpdf):
try:
pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF=nameofpdf)
dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
md, res =dbxTeam.files_download(path=pdfpath)
data = res.content
doc = fitz.open("pdf",data)
page=doc[0]
# if page.rotation!=0:
# page.set_rotation(0)
pix = page.get_pixmap() # render page to an image
pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
img=np.array(pl)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
return img ,data
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
######################################################### HELPER FUNCTIONS: TAMEEM #################################################################################################################################
img_cv2 = None
segmented_img = None
current_hsv = {
'h_min': 0, 'h_max': 179,
's_min': 0, 's_max': 255,
'v_min': 0, 'v_max': 255
}
def convert2img2_1(nameofpdf):
try:
global img_cv2
pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF=nameofpdf)
dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
md, res =dbxTeam.files_download(path=pdfpath)
data = res.content
pdf = pdfium.PdfDocument(data)
page = pdf.get_page(0)
pil_image = page.render().to_pil()
pl1=np.array(pil_image)
img = cv2.cvtColor(pl1, cv2.COLOR_RGB2BGR)
img_cv2 = img
return img, data
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
def convert2img(path):
try:
pdf = pdfium.PdfDocument(path)
page = pdf.get_page(0)
pil_image = page.render().to_pil()
pl1=np.array(pil_image)
img = cv2.cvtColor(pl1, cv2.COLOR_RGB2BGR)
return img
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
######################################################### FRONTEND ROUTES: TAMEEM #################################################################################################################################
@app.route('/click', methods=['POST'])
def click():
try:
global img_cv2, current_hsv
if img_cv2 is None:
return jsonify({'error': 'No image loaded'}), 400
data = request.json
x, y = int(data['x']), int(data['y'])
b, g, r = img_cv2[y, x]
hsv = cv2.cvtColor(np.uint8([[[b, g, r]]]), cv2.COLOR_BGR2HSV)[0][0]
h, s, v = hsv.tolist()
current_hsv = {
'h_min': h, 'h_max': h,
's_min': s, 's_max':s,
'v_min': v, 'v_max': v
}
return jsonify({
'info': f'RGB: ({r}, {g}, {b}) - HSV: ({h}, {s}, {v})',
'hMin': current_hsv['h_min'], 'hMax': current_hsv['h_max'],
'sMin': current_hsv['s_min'], 'sMax': current_hsv['s_max'],
'vMin': current_hsv['v_min'], 'vMax': current_hsv['v_max']
})
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
######################################################### FRONTEND ROUTES: TAMEEM #################################################################################################################################
@app.route('/update_mask', methods=['POST'])
def update_mask():
try:
global img_cv2, segmented_img, current_hsv
if img_cv2 is None:
return jsonify({'error': 'No image uploaded yet'}), 400
data = request.get_json(force=True) or {}
current_hsv = {
'h_min': int(data.get('hMin', 0)), 'h_max': int(data.get('hMax', 179)),
's_min': int(data.get('sMin', 0)), 's_max': int(data.get('sMax', 255)),
'v_min': int(data.get('vMin', 0)), 'v_max': int(data.get('vMax', 255)),
}
hsv = cv2.cvtColor(img_cv2, cv2.COLOR_BGR2HSV)
lower = np.array([current_hsv['h_min'], current_hsv['s_min'], current_hsv['v_min']], dtype=np.uint8)
upper = np.array([current_hsv['h_max'], current_hsv['s_max'], current_hsv['v_max']], dtype=np.uint8)
mask = cv2.inRange(hsv, lower, upper)
segmented_img = cv2.bitwise_and(img_cv2, img_cv2, mask=mask)
# Encode segmented image to PNG -> base64
ok, buf = cv2.imencode('.png', segmented_img)
if not ok:
return jsonify({'error': 'PNG encode failed'}), 500
b64 = base64.b64encode(buf).decode('utf-8')
data_url = 'data:image/png;base64,' + b64
return jsonify({
'image_data': data_url
})
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
######################################################### FRONTEND ROUTES: TAMEEM #################################################################################################################################
@app.route('/img_to_click', methods=['POST'])
def img_to_click(blob):
try:
pdf = fitz.open("pdf", blob)
page = pdf.get_page(0)
pil_image = page.render().to_pil()
pl1=np.array(pil_image)
img = cv2.cvtColor(pl1, cv2.COLOR_RGB2BGR)
_, buffer = cv2.imencode('.png', img)
return base64.b64encode(buffer).decode('utf-8')
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
######################################################### FRONTEND ROUTES: MARTHE #################################################################################################################################
#User-made MC-T NAME - in the second part of the interface
@app.route("/mctnametoGoogleSheet/<jsdata>",methods=["GET", "POST"])
def sendmctnametoLegend(jsdata):
try:
result = json.loads(jsdata)
print(result)
global pdflink
summaryid=google_sheet_Legend.mapnametoLegend(result,colorsused,pdflink)
allreturns=[summaryid]
return jsonify(allreturns)
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
@app.route("/getguessednamepath/",methods=["GET", "POST"])
def getguessedNames():
try:
guessednamesDrpdwn=google_sheet_Legend.getallguessednames()
return jsonify(guessednamesDrpdwn)
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
######################################################### HELPER FUNCTIONS: TAMEEM - NOT USED #################################################################################################################################
#NOT USED (pixel conversion instead)
def calcRef(img):
try:
blk = np.ones(img.shape, dtype="uint8") * [[[np.uint8(0), np.uint8(0), np.uint8(0)]]]
start_point = (50, 100)
end_point = (120, 200)
color = (255, 255, 255) # white BGR
thickness = -1 # Thickness of -1 will fill the entire shape
blk = cv2.rectangle(blk, start_point, end_point, color, thickness)
blk = cv2.cvtColor(blk, cv2.COLOR_BGR2GRAY)
contourzz, hierarchy = cv2.findContours(image=blk, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_NONE)
for i, cnt3 in enumerate(contourzz):
M = cv2.moments(cnt3)
if M['m00'] != 0.0:
x2 = int(M['m10']/M['m00'])
y2 = int(M['m01']/M['m00'])
area = cv2.contourArea(cnt3)
perimeter = cv2.arcLength(cnt3, True)
return area,perimeter , blk
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
#NOT USED (pixel conversion instead)
def modifyingcalcRefDynamic(img):
try:
imgcopy = img.copy()
blk = np.ones(img.shape, dtype="uint8") * [[[np.uint8(0), np.uint8(0), np.uint8(0)]]]
x = 50
y = 100
xD = int(img.shape[1] * 0.10)
yD = int(img.shape[0] * 0.10)
start_point = (x, y)
end_point = (x+xD, y+yD)
blue = (255, 0, 0) # BGR
white = (255, 255, 255) # BGR
thickness = -1 # Thickness of -1 will fill the entire shape
imgcopy = cv2.rectangle(imgcopy, start_point, end_point, blue, thickness)
blk = cv2.rectangle(blk, start_point, end_point, white, thickness)
blk = cv2.cvtColor(blk, cv2.COLOR_BGR2GRAY)
contourzz, hierarchy = cv2.findContours(image=blk, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_NONE)
for i, cnt3 in enumerate(contourzz):
M = cv2.moments(cnt3)
if M['m00'] != 0.0:
x2 = int(M['m10']/M['m00'])
y2 = int(M['m01']/M['m00'])
area = cv2.contourArea(cnt3)
perimeter = cv2.arcLength(cnt3, True)
return area, perimeter, blk , imgcopy
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
######################################################### DROPBOX CODES #################################################################################################################################
@app.route('/downloadPDFfromLink')
def downloadPDFfromLinkFunc():
try:
encoded_url = request.args.get('url')
link = urllib.parse.unquote(encoded_url)
if link and ('http' in link or 'dropbox' in link):
if 'dl=0' in link:
link = link.replace('dl=0', 'dl=1')
elif 'www.dropbox.com' in link and '?dl=1' not in link:
link += '?dl=1'
try:
res = requests.get(link)
res.raise_for_status()
except Exception as e:
return f"Error downloading PDF from link: {e}", 400
pdf_data = res.content
filename = link.split("/")[-1].split("?")[0] or "downloaded.pdf"
response = make_response(io.BytesIO(pdf_data).getvalue())
response.headers.set('Content-Type', 'application/pdf')
response.headers.set('Content-Disposition', f'attachment; filename="{filename}"')
return response
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
#get pdf dropbox url after measurement is done
@app.route('/getdropboxurl/<jsdata>',methods=["GET", "POST"])
def calldropboxurl(jsdata):
try:
print('jsdata',jsdata)
pdfurl=tsadropboxretrieval.getPathtoPDF_File(nameofPDF=jsdata)[1]
print('urll',pdfurl)
if pdfurl and ('http' in pdfurl or 'dropbox' in pdfurl):
if 'dl=0' in pdfurl:
pdfurl = pdfurl.replace('dl=0', 'dl=1')
print('urll1',pdfurl)
# Download the PDF content from the shareable link
response = requests.get(pdfurl)
pdf_content = BytesIO(response.content) # Store the content in memory
if pdf_content is None:
raise ValueError("No valid PDF content found.")
# Open the PDF using PyMuPDF
pdf_document = fitz.open(stream=pdf_content, filetype="pdf")
pdf_bytes = BytesIO()
pdf_document.save(pdf_bytes)
return Response(pdf_bytes.getvalue(), content_type='application/pdf')
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
@app.route('/refreshDropbox',methods=["GET", "POST"])
def checkdropbox():
try:
print('checkingggdf')
dfFromDropbox=tsadropboxretrieval.DropboxItemstoDF("/TSA JOBS")[0]
dfParquet=tsadropboxretrieval.GetParquetDF()
dfParquet1 = dfParquet[['name', 'path_display', 'client_modified', 'server_modified']]
deletedrows = pd.concat([dfFromDropbox, dfParquet1]).drop_duplicates(keep=False)
deletedrows = deletedrows.reset_index(drop=True)
deletedrows.columns = ['name', 'path_display', 'client_modified', 'server_modified']
differences = deletedrows[~deletedrows.isin(dfFromDropbox)].dropna()
if (len(differences)>0):
print(differences)
dbxTeam=tsadropboxretrieval.dropbox_upload_file(dfFromDropbox)
stringReturned= 'Updated Sucessfully.'
else:
stringReturned= 'Nothing to update.'
return 'stringReturned'
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
def refreshDropboxRetrievals(extractedPath):
try:
dfFromDropbox = tsadropboxretrieval.DropboxItemstoDF(extractedPath)[0]
dfParquet = tsadropboxretrieval.GetParquetDF()
# print("Original Parquet size:", len(dfParquet))
# Keep only relevant columns
dfParquet = dfParquet[['name', 'path_display', 'client_modified', 'server_modified']]
dfFromDropbox = dfFromDropbox[['name', 'path_display', 'client_modified', 'server_modified']]
# **Drop rows that start with extractedPath**
dfParquetUpdated = dfParquet[~dfParquet['path_display'].str.startswith(extractedPath)]
# **Append new data from Dropbox**
dfParquetUpdated = pd.concat([dfParquetUpdated, dfFromDropbox], ignore_index=True)
# print("Updated Parquet size:", len(dfParquetUpdated),dfParquetUpdated)
# **Save the updated Parquet file**
tsadropboxretrieval.dropbox_upload_file(dfParquetUpdated)
if len(dfFromDropbox) > 0:
print("Updated entries:", dfFromDropbox)
return 'Updated Successfully.'
else:
return 'Nothing to update.'
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
@app.route('/postdropboxprojects/<path:encoded_path>')
def handle_path(encoded_path):
try:
decoded_path = urllib.parse.unquote(encoded_path) # Decode URL encoding
extracted_path = json.loads(decoded_path) # Convert back to Python object
print('path to refresh',extracted_path)
stringReturned=refreshDropboxRetrievals(extracted_path)
print(stringReturned)
return stringReturned
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
######################################################### GET PATH OF PDF #################################################################################################################################
@app.route('/get-pdf/<jsdata>')
def get_pdf(jsdata):
try:
print('pdfname',jsdata)
# Download PDF from Dropbox
pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF=jsdata)
print('pdfpath',pdfpath)
dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
md, res =dbxTeam.files_download(path=pdfpath)
pdf_data = res.content # Ensure this is your correct PDF data from Dropbox
response = make_response(io.BytesIO(pdf_data).getvalue())
response.headers.set('Content-Type', 'application/pdf')
response.headers.set('Content-Disposition', 'attachment', filename='filename.pdf') # Replace with your desired file name
# return response
return response
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
######################################################### PIXEL CONVERSION #################################################################################################################################
@app.route('/pixeltestingimg')
def pixeltesting():
try:
encoded_url = request.args.get('url')
link = urllib.parse.unquote(encoded_url.strip('"'))
if link and ('http' in link or 'dropbox' in link):
if 'dl=0' in link:
link = link.replace('dl=0', 'dl=1')
elif 'www.dropbox.com' in link and '?dl=1' not in link:
link += '?dl=1'
pdf_path=link
response = requests.get(pdf_path)
pdf_content = BytesIO(response.content)
if not pdf_content:
raise ValueError("No valid PDF content found.")
progress_updates = [] # Shared list to store progress
def generate_progressPixel():
yield f"data: 10\n\n" # Initial progress
doc,areaPixel,perimeterPixel=pixelconversion.drawisrotated(pdf_content=pdf_content) # .openDrawPDF(data)
yield f"data: 20\n\n" # Initial progress
dbPath='/TSA JOBS/ADR Test/'+'TestingLinks'+'/'+'Scale Document' +'/'
dburl=tsadropboxretrieval.uploadanyFile(doc=doc,pdfname=str('testinglink') ,path=dbPath) #PixelConv_
# areaPixel,perimeterPixel= pixelconversion.getAreaPerimeter(dbPath, str(jsdata[3]))
yield f"data: 40\n\n" # Initial progress
outputs=[areaPixel,perimeterPixel , dburl]
while progress_updates:
progress = progress_updates.pop(0) # Get the next progress value
yield f"data: {progress}\n\n"
# Final progress and result
yield f"data: 80\n\n"
yield f"data: 100\n\n"
result = json.dumps(outputs)
yield f"data: {result}\n\n"
return Response(generate_progressPixel(), content_type='text/event-stream')
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
#PIXEL CONVERSION METHOD -- SAVES DOC ON DROPBOX TO BE MEASURED BY USER
@app.route("/pixelimg/<jsdata>",methods=["GET", "POST"])
def getimg(jsdata):
try:
progress_updates = [] # Shared list to store progress
jsdata=eval(jsdata)
print('piexxxeell',jsdata)
def generate_progressPixel():
yield f"data: 10\n\n" # Initial progress
pdfpath,pdflink=tsadropboxretrieval.getPathtoPDF_File(nameofPDF=jsdata[3], progress_callback=lambda p: progress_updates.append(p))
dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user')
md, res =dbxTeam.files_download(path=pdfpath)
data = res.content
if str(jsdata[2]).startswith('1.0'):
doc,areaPixel,perimeterPixel=pixelconversion.drawisrotated(data=data,dpi=300) # .openDrawPDF(data)
else:
doc,areaPixel,perimeterPixel=pixelconversion.drawisrotated(data=data) # .openDrawPDF(data)
yield f"data: 20\n\n" # Initial progress
dbPath='/TSA JOBS/ADR Test/'+jsdata[0]+'/'+jsdata[1]+'/'+jsdata[2]+'/'+'Scale Document' +'/'
dburl=tsadropboxretrieval.uploadanyFile(doc=doc,pdfname=str(jsdata[3]) ,path=dbPath) #PixelConv_
# areaPixel,perimeterPixel= pixelconversion.getAreaPerimeter(dbPath, str(jsdata[3]))
yield f"data: 40\n\n" # Initial progress
outputs=[areaPixel,perimeterPixel , dburl]
while progress_updates:
progress = progress_updates.pop(0) # Get the next progress value
yield f"data: {progress}\n\n"
# Final progress and result
yield f"data: 80\n\n"
yield f"data: 100\n\n"
result = json.dumps(outputs)
yield f"data: {result}\n\n"
return Response(generate_progressPixel(), content_type='text/event-stream')
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
######################################################### DELETE MARKUPS #################################################################################################################################
#DELETE MARKUPS (for comparison)
@app.route('/_submission',methods=["GET", "POST"])
def getnewlegend():
try:
pdfpth=''
alljson = request.get_json()
list1=alljson.get('dict1')
print('list1',list1)
# list1 = request.args.get('dict1')
path=alljson.get('path')
# path = request.args.get('path')
spreadsheetId=alljson.get('spreadsheetId')
# spreadsheetId =request.args.get('spreadsheetId')
pdfpathpath=alljson.get('pdfpathpath')
# pdfpathpath=request.args.get('pdfpathpath')
print(pdfpathpath,type(pdfpathpath))
pdfname=request.args.get('pdfname')
for word in eval(pdfpathpath):
pdfpth+='/' +word
pdfpth+='/'
dbPath='/TSA JOBS/ADR Test'+pdfpth+'Measured Plan/'
print(pdfpth)
deletedrows1=google_sheet_Legend.deletemarkups(list1=list1,dbPath=dbPath,path=path)
arr1=[deletedrows1.to_dict()]
print('arr,',arr1)
return jsonify(arr1)
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
#---------------------------------------------------------------------------
#if user wishes to delete
@app.route('/deletemarkupsroute',methods=["GET", "POST"])
def dltmarkupslegend():
try:
print('IN deletemarkupsroute')
pdfpth=''
alljson = request.get_json()
SimilarAreaDictionary=alljson.get('dict')
# SimilarAreaDictionary = request.args.get('dict')
deletedrows=alljson.get('deletedrows')
print('deletedrowsssssssssssssssssssssssssssssss',deletedrows)
# deletedrows = request.args.get('deletedrows')
path=alljson.get('path')
# path = request.args.get('path')
spreadsheetId=alljson.get('spreadsheetId')
# spreadsheetId =request.args.get('spreadsheetId')
areaPermArr=alljson.get('areaPermArr')
print('aaaaaaaaaaaaa',areaPermArr)
# areaPermArr=request.args.get('areaPermArr')
section=alljson.get('section')
# section=request.args.get('section')
pdfpathpath=alljson.get('pdfpathpath')
# pdfpathpath=request.args.get('pdfpathpath')
for word in eval(pdfpathpath):
pdfpth+='/' +word
pdfpth+='/'
# myDict=eval(deletedrows)
deletedrows=pd.DataFrame(deletedrows)
print('deletedrows',deletedrows)
if section.startswith('2.7'):
areaPermArr=hatched_areas2_7
if section.startswith('1.0') or section.startswith('3.2') or section.startswith('3.3'):
newlgnd=google_sheet_Legend.deletefromlegend(deletedrows=deletedrows,SimilarAreaDictionarycopy=SimilarAreaDictionary, section=section,areaPermArr=areaPermArr)
elif section.startswith('2.8') or section.startswith('2.6'):
newlgnd=google_sheet_Legend.deletedoors(deletedrows,SimilarAreaDictionary)
print('done wit 2.8 in deleting, didnt append yet ')
else:
newlgnd=google_sheet_Legend.deletefromlegend(deletedrows=deletedrows,SimilarAreaDictionarycopy=SimilarAreaDictionary, section=section)
try:
newlgnd=google_sheet_Legend.legendGoogleSheets(SimilarAreaDictionary=newlgnd,path=path,spreadsheetId=spreadsheetId ,pdfpath=pdfpth) #new legend
except:
print("An exception occurred")
time.sleep(20)
newlgnd=google_sheet_Legend.legendGoogleSheets(SimilarAreaDictionary=newlgnd,path=path,spreadsheetId=spreadsheetId,pdfpath=pdfpth)
return jsonify('donee')
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
######################################################### API #################################################################################################################################
#_________________________________________________________________________________________________________________________
#_________________________________________________________________________________________________________________________
@app.route('/getAPITables/',methods=["GET", "POST"])
def returnAPITables():
try:
# API.update_df()
table1,table2,table3=API.GenerateTables()
return jsonify([table1.to_dict(),table2.to_dict(),table3.to_dict()])
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
@app.route('/refreshAPI', methods=["GET", "POST"])
def checktables():
try:
projectname = request.args.get('prjname')
projectpart = request.args.get('prjpart')
projectpartid = request.args.get('prjpartid')
global cached_tables
# ✅ Fix: check properly for missing/empty DataFrame
if cached_tables["projects"] is None or cached_tables["projects"].empty:
return jsonify(["No cached data available yet. Please fetch projects first."])
# Get fresh values from API
new_projects, new_parts, new_sections = API.GenerateTables()
returnString = None
# 1) Compare project names
old_names = cached_tables["projects"]["ProjectName"].tolist()
new_names = new_projects["ProjectName"].tolist()
if set(old_names) != set(new_names):
added = list(set(new_names) - set(old_names))
removed = list(set(old_names) - set(new_names))
returnString = ["Changes in Projects", "project", {"added": added, "removed": removed}]
# 2) Compare parts
elif projectname and cached_tables["parts"] is not None and not cached_tables["parts"].empty:
prjid = new_projects.loc[new_projects['ProjectName'] == projectname, 'ProjectId'].values[0]
old_parts = cached_tables["parts"].loc[cached_tables["parts"]["ProjectId"] == prjid, "ProjectPart"].tolist()
new_parts_list = new_parts.loc[new_parts["ProjectId"] == prjid, "ProjectPart"].tolist()
if set(old_parts) != set(new_parts_list):
added = list(set(new_parts_list) - set(old_parts))
removed = list(set(old_parts) - set(new_parts_list))
returnString = ["Changes in Parts", "part", {"added": added, "removed": removed}]
# 3) Compare sections
elif projectname and projectpart and projectpartid and cached_tables["sections"] is not None and not cached_tables["sections"].empty:
prjid = new_projects.loc[new_projects['ProjectName'] == projectname, 'ProjectId'].values[0]
old_sections = cached_tables["sections"]["ProjectSection"].tolist()
new_sections_list = new_sections[
(new_sections["ProjectId"] == prjid) &
(new_sections["ProjectPartId"] == int(projectpartid))
]["ProjectSection"].tolist()
if set(old_sections) != set(new_sections_list):
added = list(set(new_sections_list) - set(old_sections))
removed = list(set(old_sections) - set(new_sections_list))
returnString = ["Changes in Sections", "section", {"added": added, "removed": removed}]
if not returnString:
returnString = ["No changes detected"]
# ✅ update cache
cached_tables["projects"] = new_projects
cached_tables["parts"] = new_parts
cached_tables["sections"] = new_sections
return jsonify(returnString)
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
@app.route('/refreshAPIAppendNewTables',methods=["GET", "POST"])
def appendNewTables():
try:# value = request.args.get('keyword')
# print(value)
API.AppendtablestoSheets()
return jsonify('appended')
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
######################################################### XML SUMMARY #################################################################################################################################
@app.route('/summarytoXML/<jsdata>',methods=["GET", "POST"])
def cvtSummarytoXML(jsdata):
try:
path='/TSA JOBS/ADR Test/'
result = json.loads(jsdata)
for word in result[0]:
path+=word +'/'
print(path)
path=path+'XML/'
# print(result[0])
# print(result[1])
xmllink=google_sheet_to_xml.create_xml(documentname=result[1],dbPath=path)
return jsonify(xmllink)
except (ConnectionError, TimeoutError) as e:
# Use app context when logging
from flask import current_app
with current_app.app_context():
error_msg = log_error(str(e), issue_type="connection")
return jsonify({"error": error_msg}), 500
except Exception:
from flask import current_app
with current_app.app_context():
error_details = traceback.format_exc()
error_msg = log_error(error_details, issue_type="backend")
return jsonify({"error": error_msg}), 500
################################################################################################################################################################################################
######################################################### MAIN #################################################################################################################################
################################################################################################################################################################################################
def runn():
# app.run(host="127.0.0.1")
from gevent.pywsgi import WSGIServer
http_server = WSGIServer(("0.0.0.0", 7860), app)
http_server.serve_forever()
# serve(app, host="127.0.0.1", port=8080)
def keep_alive():
t=Thread(target=runn)
t.start()
dtn = datetime.datetime.now(datetime.timezone.utc)
print(dtn)
next_start = datetime.datetime(dtn.year, dtn.month, dtn.day, 21, 0, 0).astimezone(datetime.timezone.utc) #us - 2 = utc time (21 utc is 19:00 our time and 9 is 7 our time , it needs to run 9 utc time ____ )
print(next_start)
keep_alive()
# active_path = "/TSA Team Folder/ADR Test/Test/"
while 1:
dtnNow = datetime.datetime.now(datetime.timezone.utc)
print(dtnNow)
if dtnNow >= next_start:
next_start += datetime.timedelta(hours=12) # 1 day
print(next_start)
checkdropbox()
time.sleep(1800)
if __name__ == "__main__":
runn()
|