import os import re import webbrowser import pdfkit import time import multiprocessing import json from PIL import Image from create_instructions import getRedZones, getInstructionsByZones, createFieldDirImg from getMapByCoordinates import getFieldMapImg import matplotlib.pyplot as plt import datetime as dt # from gen_report_new2 import createShortDynamicLink # from pyvirtualdisplay import Display fieldColorImg = "report_images/reportPics/sat_img_etci.png" unavailableImg = "https://farmonaut.com/Images/unavailable.jpg" unavailableImgDecisionStyle = "width: 250px; height: auto;" companyLogos = { 'farmonaut': 'report_images/farmonaut_logo_black.png', } options = { "enable-local-file-access": "", "page-height": "192mm", "page-width": "136mm", "margin-top": "6mm", "margin-bottom": "6mm", "margin-left": "8mm", "margin-right": "8mm", # "font": "report_fonts/Noto_Sans/NotoSans-Medium.ttf", # 'page-size': 'A4', # 'margin-top': '0.75in', } defaultValues = { "etci_path": unavailableImg, "ndvi_path": unavailableImg, "irri_value": 0, "is_land_hist_cult": True, "crop_inten_value": 0, "soil_health_value": 0, "grd_water_value": 0, "land_score_value": 0, "crop_score_value": 0, "poten_earn_value": 0, "mand_dist_km": 0, "has_hazard_hist": False, } allReportTestValues = { "field_coords": { "a": {"Latitude": 23.108696466469407, "Longitude": 76.94931373000145}, "P_1": {"Latitude": 23.108788361955472, "Longitude": 76.94893788546324}, "P_2": {"Latitude": 23.107457721514155, "Longitude": 76.94890134036541}, "P_3": {"Latitude": 23.10740036336785, "Longitude": 76.94929964840412}, "P_4": {"Latitude": 23.108686598494, "Longitude": 76.94933719933033}, }, "ndvi_avg_value": 0.5, "ndvi_path": fieldColorImg, "etci_path": fieldColorImg, "generation_date": dt.datetime(2023, 3, 20), "sowing_date": dt.datetime(2022, 3, 10), # "harvest_date": dt.datetime(2022, 9, 12), "irri_value": 84, "is_land_hist_cult": True, "crop_inten_value": 84, "soil_health_value": 84, # "grd_water_value": 84, # "land_score_value": 84, # "crop_score_value": 84, # "poten_earn_value": 21000, "mandi_dist_km": 24, "has_hazard_hist": False, "company_logo": companyLogos["farmonaut"], } fileName = "ins_report_template.html" tmpFileName = "ins_report_tmp.html" # functions def createExtractTranslationTxt(langCode="en", outFile="tmpTxtFile.txt"): translations = None fileName = "jsons/app_{langCode}.json".format(langCode=langCode) with open(fileName, encoding="utf8") as f: jsonStr = f.read() translations = json.loads(jsonStr) # expects a dictionary resultStr = "" for key in translations.keys(): resultStr += "%s = translations.get('%s', None) or enTrans.get('%s', None)\n" % ( key, key, key) with open(outFile, 'w', encoding="utf8") as f: f.write(resultStr) def createTranslationJson(langs, outFile="uploadTrans.json"): allTransObj = {} for langCode in langs: translations = None fileName = "jsons/app_{langCode}.json".format(langCode=langCode) with open(fileName, encoding="utf8") as f: jsonStr = f.read() translations = json.loads(jsonStr) # expects a dictionary allTransObj[langCode] = translations with open(outFile, 'w', encoding="utf8") as f: f.write(str(allTransObj)) def removeKeysInTranslation(langs, keys): # raise Exception("this func is not working") for langCode in langs: translations = None fileName = "jsons/app_{langCode}.json".format(langCode=langCode) with open(fileName, encoding="utf8") as f: jsonStr = f.read() translations = json.loads(jsonStr) # expects a dictionary for key in keys: if key in translations: translations.pop(key) with open(fileName, 'w', encoding="utf8") as f: f.write(str(translations)) def getValueIfPresent(dict={}, key=None): return dict[key] if dict.contains(key) else None def openFile(htmlStr=None, file=None): tmpFileName = None if htmlStr: tmpFileName = "report_tmp.html" with open(tmpFileName, 'w') as f: f.write(htmlStr) webbrowser.open(file or tmpFileName or fileName) def createPdf(outfile=None, file=fileName): pdfFileName = outfile or "out.pdf" path_wkhtmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe' c = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf) # absPath = os.path.abspath(fileName) pdfkit.from_file(file, pdfFileName, options, configuration=c) def createPdf2(outfile=None, file=None): pdfFileName = outfile or "out.pdf" path_wkhtmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe' # config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf) # absPath = os.path.abspath(fileName) # with Display(): pdfkit.from_file(file or fileName, pdfFileName, options=options) def createPdfByString(string, outfile=None): pdfFileName = outfile or "out.pdf" path_wkhtmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe' config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf) # absPath = os.path.abspath(fileName) pdfkit.from_string(string, pdfFileName, configuration=config, options=options) def saveToTmpHtml(htmlStr): with open(tmpFileName, 'w', encoding="utf8") as f: f.write(htmlStr) def replaceHtmlData(elementKey, value, htmlStr, initialIndex): # assuming elements to be none container type # print(startIndex) keyIndex = htmlStr.index(elementKey, initialIndex) startIndex = htmlStr.index(">", keyIndex) endIndex = htmlStr.index("<", startIndex) return htmlStr[:startIndex+1]+value+htmlStr[endIndex:], startIndex+len(value)+1 def replaceHtmlEleStyle(elementKey, style, htmlStr, initialIndex): # assuming elements to be none container type # print(startIndex) keyIndex = htmlStr.index(elementKey, initialIndex) startIndex = htmlStr.index("style=", keyIndex) endIndex = htmlStr.index('"', startIndex+7) return htmlStr[:startIndex+7]+style+htmlStr[endIndex:], initialIndex def replaceHtmlImgSrc(elementKey, src, htmlStr, initialIndex): # assuming element id is added before src parameter in html # and also that some src is already added there keyIndex = htmlStr.index(elementKey, initialIndex) startIndex = htmlStr.index("src", keyIndex) endIndex = htmlStr.index('"', startIndex+5) return htmlStr[:startIndex+5]+src+htmlStr[endIndex:], startIndex+len(src)+1 def replaceHtmlStyle(className, style, htmlStr, initialIndex): keyIndex = htmlStr.index(className, initialIndex) startIndex = htmlStr.index("{", keyIndex) endIndex = htmlStr.index("}", startIndex+1) return htmlStr[:startIndex+1]+style+htmlStr[endIndex:], startIndex+len(style)+1 def replaceHtmlComments(htmlStr): pattern = re.compile("\/\*([^\*\/]|\n)*\*\/|", re.MULTILINE) return re.sub(pattern, "", htmlStr) def createPie(value, entity="", mediumLevels=[30, 60]): # return path to img; expects value in 0-100, mediumLevels len = 2 # create data if value < mediumLevels[0]: color = "red" elif value < mediumLevels[1]: color = "orange" else: color = "green" size_of_groups = [(100-value)/2, (100+value)/2] # Create a pieplot plt.pie(size_of_groups, colors=["lightgrey", color]) # add a circle at the center to transform it in a donut chart my_circle = plt.Circle((0, 0), 0.7, color='white') p = plt.gcf() p.gca().add_artist(my_circle) plt.tight_layout() plt.margins(0) # plt.show() imgPath = f"report_images/reportPics/pie_{entity}.png" plt.savefig(imgPath) img = Image.open(imgPath) width, height = img.size # 640, 480; 180 r; 140, 60 # print(height, width) img2 = img.crop((140, 60, width-140, height-240)) img2.save(imgPath) return imgPath def populateData(translations=None, langCode="en", allReportValues={}, isTest=False): # returns the populated data html string htmlStr = None initialIndex = 0 with open(fileName) as f: htmlStr = f.read() # return htmlStr # queries # translations = None if isTest: with open("jsons/app_{langCode}.json".format(langCode=langCode), encoding="utf8") as f: jsonStr = f.read() translations = json.loads(jsonStr) # expects a dictionary with open("jsons/app_en.json", encoding="utf8") as f: jsonStr = f.read() enTrans = json.loads(jsonStr) # expects # get data to replace if langCode in ["uz"]: introPStyle = "font-size: 16px;" companyLogoDivStyle = "height: 100px; padding-bottom: 16px; text-align: center;" else: introPStyle = "font-size: 18px;" companyLogoDivStyle = "height: 130px; padding-bottom: 16px; text-align: center;" if langCode in ["uz", "ta"]: colorBlindHeadingStyle = "margin-top: 8px; font-size: 20px;" else: colorBlindHeadingStyle = "margin-top: 8px;" if langCode == "pa": fontStyle = "font-family: MuktaMahee; src: url('report_fonts/Mukta_Mahee/MuktaMahee-Light.ttf') format('truetype');" allEleStyle = "box-sizing: border-box; font-family: MuktaMahee;" allTextStyle = "line-height: 1.6; margin: 0;" elif langCode == "bn": fontStyle = "font-family: HindSiliguri; src: url('report_fonts/Hind_Siliguri/HindSiliguri-Light.ttf') format('truetype');" allEleStyle = "box-sizing: border-box; font-family: HindSiliguri;" allTextStyle = "line-height: 1.8; margin: 0;" elif langCode == "ta": fontStyle = "font-family: NotoSansTamil; src: url('report_fonts/Noto_Sans_Tamil/static/NotoSansTamil/NotoSansTamil-Light.ttf') format('truetype');" allEleStyle = "box-sizing: border-box; font-family: NotoSansTamil;" allTextStyle = "line-height: 1.3; margin: 0;" elif langCode == "te": fontStyle = "font-family: NotoSansTelugu; src: url('report_fonts/Noto_Sans_Telugu/static/NotoSansTelugu/NotoSansTelugu-Light.ttf') format('truetype');" allEleStyle = "box-sizing: border-box; font-family: NotoSansTelugu;" allTextStyle = "line-height: 1.6; margin: 0;" elif langCode == "kn": fontStyle = "font-family: NotoSansKannada; src: url('report_fonts/Noto_Sans_Kannada/static/NotoSansKannada/NotoSansKannada-Light.ttf') format('truetype');" allEleStyle = "box-sizing: border-box; font-family: NotoSansKannada;" allTextStyle = "line-height: 1.6; margin: 0;" elif langCode == "uz": fontStyle = "font-family: NotoSans; src: url('report_fonts/Noto_Sans/NotoSans-Light.ttf') format('truetype');" allEleStyle = "box-sizing: border-box; font-family: NotoSans;" allTextStyle = "line-height: 1.5; margin: 0;" else: fontStyle = "font-family: NotoSans; src: url('report_fonts/Noto_Sans/NotoSans-Light.ttf') format('truetype');" allEleStyle = "box-sizing: border-box; font-family: NotoSans;" allTextStyle = "line-height: 1.8; margin: 0;" # translation txts # soilHealthHeadingDiv = translations.get( # 'soilHealthHeadingDiv', None) or enTrans.get('soilHealthHeadingDiv', None) # soilInfoP = translations.get( # 'soilInfoP', None) or enTrans.get('soilInfoP', None) # content generated values generationDateValueH6 = allReportValues.get( 'generation_date', None) or dt.datetime.now() sowingDateValueH6 = allReportValues.get('sowing_date', None) harvestDateValueH6 = allReportValues.get('harvest_date', None) rgbETCIImg = allReportValues.get( 'etci_path', None) or defaultValues['etci_path'] ndviImg = allReportValues.get( 'ndvi_path', None) or defaultValues['ndvi_path'] ndviValue = allReportValues.get('ndvi_avg_value', None) or 0 companyLogoImg = allReportValues.get( "companyLogoImg", None) or companyLogos['farmonaut'] valueIrriH2 = allReportValues.get("irri_value", None) valueCropIntenH2 = allReportValues.get("crop_inten_value", None) insightHazardHistH4 = allReportValues.get("has_hazard_hist", None) valueSoilHealthH2 = allReportValues.get("soil_health_value", None) valueGrdWaterH2 = allReportValues.get("grd_water_value", None) valuePotenEarnH4 = allReportValues.get("poten_earn_value", None) valueLandScoreH2 = allReportValues.get("land_score_value", None) valueCropScoreH2 = allReportValues.get("crop_score_value", None) valueMandiDistH4 = allReportValues.get("mandi_dist_km", None) valueCropHistH2 = allReportValues.get("is_land_hist_cult", None) fieldCoords = allReportValues.get("field_coords", None) print(allReportValues) if fieldCoords == None or len(fieldCoords) == 0: raise Exception("field coordinates must be provided") decisionTreeETCIImg = rgbETCIImg # get map/dir img/sizes processID = "test" if isTest else int(dt.datetime.now().timestamp()) minLat = 180 minLng = 180 maxLat = -180 maxLng = -180 for i in range(len(fieldCoords)): key = ("a" if "a" in fieldCoords else "P_0") if i == 0 else f"P_{i}" value = fieldCoords[key] lat = value["Latitude"] lng = value["Longitude"] if lat < minLat: minLat = lat elif lat > maxLat: maxLat = lat if lng < minLng: minLng = lng elif lng > maxLng: maxLng = lng fieldLatLngs = { 'minLat': minLat, 'minLng': minLng, 'maxLat': maxLat, 'maxLng': maxLng, 'centerLat': (minLat+maxLat)/2, 'centerLng': (maxLat+minLat)/2 } dirPath = f"report_images/{processID}" if not os.path.isdir(dirPath): os.mkdir(dirPath) mapImgPath = f"report_images/{processID}/mapImg.png" mapImgDimens = (230, 115, 29.663, 19.16) if isTest else getFieldMapImg( fieldCoords, fieldLatLngs, mapImgPath) decisionTreeFieldDirImg = f"report_images/{processID}/dirImg.png" createFieldDirImg(rgbETCIImg, decisionTreeFieldDirImg, translations, langCode) fieldImgPaddings = (mapImgDimens[2], mapImgDimens[3]) mapImgSize = (mapImgDimens[0], mapImgDimens[1]) if ndviImg != unavailableImg: ndviRedZones = getRedZones(ndviImg) decisionTreeCropP = getInstructionsByZones( ndviRedZones, translations, isIrri=False, lang=langCode) else: decisionTreeCropP = "" img = Image.open(rgbETCIImg) imgWidth, imgHeight = img.size if mapImgSize == None: mapImgRatio = imgHeight/imgWidth else: mapImgRatio = mapImgSize[1]/mapImgSize[0] mapImgHeight = 230 if mapImgRatio > 0.92 else 250*mapImgRatio mapImgWidth = mapImgHeight/mapImgRatio mapImgHPad = mapImgWidth*fieldImgPaddings[0]/(fieldImgPaddings[0]*2+1) mapImgVPad = mapImgHeight*fieldImgPaddings[1]/(fieldImgPaddings[1]*2+1) mapInnerImgH = "230px" if mapImgRatio > 0.92 else "auto" mapInnerImgW = "auto" if mapImgRatio > 0.92 else "250px" mapInnerPadH = (250-mapImgWidth)/2 decisionTreeImgInnerDivStyle = "display: flex; justify-content: center; margin-top: 8px; height: %s; width: %s; padding: 0 0 0 %spx" % ( mapInnerImgH, mapInnerImgW, mapInnerPadH) decisionTreeMapImgStyle = "position: absolute; height: %spx; width: %spx;" % ( mapImgHeight, mapImgWidth) decisionTreeImgStyle = "position: absolute; height: %spx; width: %spx; padding: %spx %spx;" % ( mapImgHeight, mapImgWidth, mapImgVPad, mapImgHPad) imgRatio = imgHeight/imgWidth dirImgHeight = 230 if imgRatio > 0.92 else 250*imgRatio decisionTreeFieldDirImgStyle = "height: %spx; aspect-ratio: %s;" % ( dirImgHeight, 1/imgRatio) # set visibility displayNoneStyle = "display: none" displayBlockStyle = "display: block" displayInlineBlockStyle = "display: inline-block" sowingDateDivStyle = "min-height: 70px; margin-right: 300px; %s ;" % ( displayNoneStyle if sowingDateValueH6 == None else displayInlineBlockStyle) harvestDateDivStyle = "min-height: 70px; %s ;" % ( displayNoneStyle if harvestDateValueH6 == None else displayInlineBlockStyle) decisionTreeSummaryDivStyle = "position: relative; top: 2px; left: 0px; width: 900px; height: %spx; background-color: cyan; border-radius: 8px;" % ( 280 if ndviImg == unavailableImg else 575) decisionTreeNDVIDivStyle = "margin-top: 8px; width: 900px; height: 280px; padding: 0px; %s ;" % ( displayNoneStyle if ndviImg == unavailableImg else displayBlockStyle) cropHistValueDivStyle = displayNoneStyle if valueCropHistH2 == None else displayInlineBlockStyle hazardHistValueDivStyle = displayNoneStyle if insightHazardHistH4 == None else displayInlineBlockStyle irriValueDivStyle = displayNoneStyle if valueIrriH2 == None else displayInlineBlockStyle cropIntenValueDivStyle = displayNoneStyle if valueCropIntenH2 == None else displayInlineBlockStyle soilHealthValueDivStyle = displayNoneStyle if valueSoilHealthH2 == None else displayInlineBlockStyle grdWaterValueDivStyle = displayNoneStyle if valueGrdWaterH2 == None else displayInlineBlockStyle mandiDistValueDivStyle = displayNoneStyle if ( valueMandiDistH4 == None and valuePotenEarnH4 == None) else displayInlineBlockStyle valueMandiDistH4Style = displayNoneStyle if valueMandiDistH4 == None else displayBlockStyle valuePotenEarnH4Style = displayNoneStyle if valuePotenEarnH4 == None else displayBlockStyle landScoreValueDivStyle = displayNoneStyle if valueLandScoreH2 == None else displayInlineBlockStyle cropScoreValueDivStyle = displayNoneStyle if valueCropScoreH2 == None else displayInlineBlockStyle valueIrriH2 = valueIrriH2 or 0 valueCropIntenH2 = valueCropIntenH2 or 0 valueSoilHealthH2 = valueSoilHealthH2 or 0 valueGrdWaterH2 = valueGrdWaterH2 or 0 valueLandScoreH2 = valueLandScoreH2 or 0 valueCropScoreH2 = valueCropScoreH2 or 0 insightHazardHistH4 = insightHazardHistH4 or False valueMandiDistH4 = valueMandiDistH4 or 0 valuePotenEarnH4 = valuePotenEarnH4 or 0 valueCropHistH2 = valueCropHistH2 or True cropHistResultImgStyle = "filter: unset" if valueCropHistH2 else "filter: grayscale(1)" insightCropHistH4 = "Cultivation Land" if valueCropHistH2 else "Not Cultivation Land" # dateFormat = "%d/%m%Y" #generationDateValueH6 = generationDateValueH6.strftime(dateFormat) reportDateValueH6 = generationDateValueH6 sowingDateValueH6 = generationDateValueH6 if sowingDateValueH6 == None else sowingDateValueH6 harvestDateValueH6 = generationDateValueH6 if harvestDateValueH6 == None else harvestDateValueH6 # create value result imgs irriResultImg = createPie(valueIrriH2, "irri") cropIntenResultImg = createPie(valueCropIntenH2, "crop_inten") soilHealthResultImg = createPie(valueSoilHealthH2, "soil_health") grdWaterResultImg = createPie(valueGrdWaterH2, "grd_water") landScoreResultImg = createPie(valueLandScoreH2, "land_score") cropScoreResultImg = createPie(valueCropScoreH2, "crop_score") valueHazardHistDivStyle = "background-color: %s" % ( "red" if insightHazardHistH4 else "green") # insight strings def getGoodBadStr(value, mediumLevels=[30, 60]): if value < mediumLevels[0]: return "Bad" elif value < mediumLevels[1]: return "Medium" else: return "Good" insightIrriH4 = "%s Irrigation" % (getGoodBadStr(valueIrriH2)) insightCropIntenH4 = getGoodBadStr(valueCropIntenH2) insightSoilHealthH4 = getGoodBadStr(valueSoilHealthH2) insightGrdWaterH4 = getGoodBadStr(valueGrdWaterH2) insightLandScoreH4 = getGoodBadStr(valueLandScoreH2) insightCropScoreH4 = getGoodBadStr(valueCropScoreH2) # add units to values valueIrriH2 = str(valueIrriH2)+"%" insightHazardHistH4 = "Yes" if insightHazardHistH4 else "No" valueCropIntenH2 = str(valueCropIntenH2)+"%" valueSoilHealthH2 = str(valueSoilHealthH2)+"%" valueGrdWaterH2 = str(valueGrdWaterH2)+"%" valueLandScoreH2 = str(valueLandScoreH2)+"%" valueCropScoreH2 = str(valueCropScoreH2)+"%" valueMandiDistH4 = str(valueMandiDistH4)+" Km" # valuePotenEarnH4 += " Km" # REPLACE DATA; make sure to call the function in order of html code # replacing comments # htmlStr = replaceHtmlComments(htmlStr) # page 1 - intro # print(initialIndex, htmlStr) htmlStr, initialIndex = replaceHtmlStyle( "@font-face", fontStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlStyle( "*", allEleStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlStyle( "p", allTextStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlStyle( ".decision_tree_img_inner_div", decisionTreeImgInnerDivStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlStyle( ".decision_tree_sat_back_img", decisionTreeMapImgStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlStyle( ".decision_tree_sat_img", decisionTreeImgStyle, htmlStr, initialIndex) # htmlStr, initialIndex = replaceHtmlStyle( # ".sat_img", satImgStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( "companyLogoDiv", companyLogoDivStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlImgSrc( "companyLogoImg", companyLogoImg, htmlStr, initialIndex) # htmlStr, initialIndex = replaceHtmlData( # 'fieldDetailsHeadingDiv', fieldDetailsHeadingDiv, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'reportDateValueH6', reportDateValueH6, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( "sowingDateDiv", sowingDateDivStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'sowingDateValueH6', sowingDateValueH6, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( "harvestDateDiv", harvestDateDivStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'harvestDateValueH6', harvestDateValueH6, htmlStr, initialIndex) # page 2 - decision tree # htmlStr, initialIndex = replaceHtmlData( # 'decisionTreeETCIH3', decisionTreeETCIH3, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( "decisionTreeSummaryDiv", decisionTreeSummaryDivStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlImgSrc( 'decisionTreeETCISatImg', mapImgPath, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlImgSrc( 'decisionTreeETCIImg', decisionTreeETCIImg, htmlStr, initialIndex) # htmlStr, initialIndex = replaceHtmlData( # 'decisionTreeDirH4', decisionTreeDirH3, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( 'decisionTreeFieldDirImg', decisionTreeFieldDirImgStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlImgSrc( 'decisionTreeFieldDirImg', decisionTreeFieldDirImg, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( "decisionTreeNDVIDiv", decisionTreeNDVIDivStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlImgSrc( 'decisionTreeCropSatImg', mapImgPath, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlImgSrc( 'decisionTreeCropImg', ndviImg, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'decisionTreeCropP', decisionTreeCropP, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( 'decisionTreeCropTracImg', "position: absolute; width: 350px; left: 470px; top: 150px; %s" % (displayBlockStyle if (decisionTreeCropP == translations["sms1_"]) else displayNoneStyle), htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( "irriValueDiv", irriValueDivStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlImgSrc( 'irriResultImg', irriResultImg, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'valueIrriH2', valueIrriH2, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'insightIrriH4', insightIrriH4, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( "cropHistValueDiv", cropHistValueDivStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( 'cropHistResultImg', cropHistResultImgStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'insightCropHistH4', insightCropHistH4, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( "hazardHistValueDiv", hazardHistValueDivStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( 'valueHazardHistDiv', valueHazardHistDivStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'insightHazardHistH4', insightHazardHistH4, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( "cropIntenValueDiv", cropIntenValueDivStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlImgSrc( 'cropIntenResultImg', cropIntenResultImg, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'valueCropIntenH2', valueCropIntenH2, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'insightCropIntenH4', insightCropIntenH4, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( "soilHealthValueDiv", soilHealthValueDivStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlImgSrc( 'soilHealthResultImg', soilHealthResultImg, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'valueSoilHealthH2', valueSoilHealthH2, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'insightSoilHealthH4', insightSoilHealthH4, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( "grdWaterValueDiv", grdWaterValueDivStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlImgSrc( 'grdWaterResultImg', grdWaterResultImg, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'valueGrdWaterH2', valueGrdWaterH2, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'insightGrdWaterH4', insightGrdWaterH4, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( "valueMandiDistH4", valueMandiDistH4Style, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'valueMandiDistH4', valueMandiDistH4, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( "titleMandiDistH5", valueMandiDistH4Style, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( "valuePotenEarnH4", valuePotenEarnH4Style, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'valuePotenEarnH4', str(valuePotenEarnH4), htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( "titlePotenEarnH5", valuePotenEarnH4Style, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( "landScoreValueDiv", landScoreValueDivStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlImgSrc( 'landScoreResultImg', landScoreResultImg, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'valueLandScoreH2', valueLandScoreH2, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'insightLandScoreH4', insightLandScoreH4, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlEleStyle( "cropScoreValueDiv", cropScoreValueDivStyle, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlImgSrc( 'cropScoreResultImg', cropScoreResultImg, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'valueCropScoreH2', valueLandScoreH2, htmlStr, initialIndex) htmlStr, initialIndex = replaceHtmlData( 'insightCropScoreH4', insightCropScoreH4, htmlStr, initialIndex) return htmlStr # run commands # createPie(84) # createExtractTranslationTxt() # removeKeysInTranslation(["bn", "mr", "te", "ta", "kn", "ar", "en", "hi", "uz"], ['weatherHeadingDiv', 'decisionTreeHeadingDiv']) # createTranslationJson(["bn", "mr", "te", "ta", "kn", "ar", "en", "hi", "uz", "pa"]) # openFile() # time.sleep(1) # "bn", "mr", "te", "ta", "kn", "ar", "en", "hi", "uz", "pa" # langs = ["en"] # multi-language not set yet # for langCode in langs: # # langCode = "en" # populatedString = populateData(None, langCode, allReportTestValues, True) # # print(populatedString) # saveToTmpHtml(populatedString) # # openFile(file=tmpFileName) # createPdf(file=tmpFileName, # outfile="ins_out_{langCode}.pdf".format(langCode=langCode)) # print(langCode + " done") # createPdfByString(populateData) # p = multiprocessing.Process(target = createPdf, args = ()) # Create the process # p.start() # Start the process # p.join(5) # Give it a 180 sec frame to complete before you kill it # if p.is_alive(): # p.terminate() # If after 180 sec its still running, kill it # from wkhtmltopdf import HTMLFILEToPDF # make_pdf = wkhtmltopdf.HTMLFILEToPDF( # url=fileName, # output_file='~/out.pdf', # ) # make_pdf.render() # longUrl = "https://storage.googleapis.com/farmbase-b2f7e.appspot.com/PaidMonitoredFields/fSRlyKzCxxMJHJqDzpGhQImyyxf2/1669891376338/20221013/report.pdf?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=farmbase-b2f7e%40appspot.gserviceaccount.com%2F20221209%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20221209T131824Z&X-Goog-Expires=900&X-Goog-SignedHeaders=host&X-Goog-Signature=319ecd82f2015e6886788b82453319747d25ed2f56491b46d6700cecfe93358d95d3fe258a1aab5cfc89287af1770b83a3cf3f8550bb5f4c19e0bc37af48cc708f90e5675193b23c32b50bd707332a7de513b28e2beb6991d96c388e83325e66575a07096b39864e5de55f53efdbc4e00880b547608fed7d07873d6e0c9221acf8dc6722d46955d446d22c9b6f268ccbbf3b6d588b69ead0366c2ded7d681b94f5853d7c404c39c6cfcab2b682500ba1e349e46ebeaf2880ff17650296a9d90eef2688d8bb9456921224826c5ba3cdb9340f9f5700eb8357b9477c65e40d5d19269d788a887f0eabbdaf6b82e6cc7fa6ccf1f6d41188f57f3f620e9fded49baa" # shortUrl = createShortDynamicLink(longUrl) # print(shortUrl)