def barh(labels, values): df = pd.DataFrame(columns=['labels', 'values', 'color']) df['labels'] = labels df['values'] = values.astype(float) df.loc[df['values'] >= 4, 'color'] = 'blue' df.loc[(df['values'] >= 3) & (df['values'] < 4), 'color'] = 'green' df.loc[(df['values'] >= 2) & (df['values'] < 3), 'color'] = 'purple' df.loc[df['values'] < 2, 'color'] = 'red' df['Metrics'] = df['labels'] + '(' + df['values'].astype(str) + ')' df = df.set_index('Metrics') fig = plt.figure(figsize=(14, 16)) ax = fig.add_subplot(111) title = 'Areas doing well and areas needing attention' df['values'].plot(kind='barh', ax=ax, alpha=0.6, legend=False, color=df.color, edgecolor='w', xlim=(0, max(df['values'])), title=title) #print(df) file = 'Report_{}_{}.png'.format('barh', bl.getTimeStamp()) plt.savefig(file, bbox_inches="tight") plt.close() return file
def barRadar(sec, values, subsecs): y_pos = np.arange(len(subsecs)) plt.bar(y_pos, values, color=['#ff6666', '#ffcc99', '#99ff99']) plt.xticks(y_pos, subsecs) plt.ylim(0, 5) file = 'radar_{}_{}.png'.format(sec, bl.getTimeStamp()) plt.savefig(file, bbox_inches="tight") plt.close() return file
def genFullRadar(sectors, subsectors, subscores): import matplotlib matplotlib.use('Agg') from matplotlib import pyplot ################### PIE-DONUT-RADAR CHART ###################### x = subscores labels = sectors sizes = [ subscores[0] + subscores[4] + subscores[8], subscores[1] + subscores[5] + subscores[9], subscores[2] + subscores[6] + subscores[10], subscores[3] + subscores[7] + subscores[11] ] # have to calculate from subscores labels_sub = [ 'Physical (score - {})'.format(x[0]), 'Organizational ({})'.format( x[1]), 'Technical ({})'.format(x[2]), 'Physical ({})'.format(x[3]), 'Organizational ({})'.format(x[4]), 'Technical ({})'.format(x[5]), 'Physical ({})'.format(x[6]), 'Organizational ({})'.format(x[7]), 'Technical ({})'.format(x[8]), 'Physical ({})'.format(x[9]), 'Organizational ({})'.format(x[10]), 'Technical ({})'.format(x[11]) ] sizes_sub = x colors = ['#ff6666', '#ffcc99', '#99ff99', '#66b3ff'] colors_sub = [ '#ff6666', '#ff6666', '#ff6666', '#ffcc99', '#ffcc99', '#ffcc99', '#99ff99', '#99ff99', '#99ff99', '#66b3ff', '#66b3ff', '#66b3ff' ] explode = (0.2, 0.2, 0.2, 0.2) explode_sub = (0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1) #Plot plt.pie(sizes, startangle=90, colors=colors, radius=2) plt.legend(labels, fontsize='large', fancybox=True) plt.pie(sizes_sub, explode=explode_sub, labels=labels_sub, startangle=90, colors=colors_sub, textprops={'fontsize': 20}, frame=True, radius=4) centre_circle = plt.Circle( (0, 0), 2.5, color='black', fc='white', linewidth=0) #### Chnage 2.75 to see diff variations fig = plt.gcf() fig.gca().add_artist(centre_circle) fig.set_size_inches(20, 15, forward=False) plt.axis('equal') #plt.tight_layout() #plt.title('Overall Resilience Metrics Summary \n \n') file = 'Report_{}.png'.format(bl.getTimeStamp()) plt.savefig(file, bbox_inches="tight") plt.close() return file
def questionsUpload(): survey = request.args.get('survey') company = request.args.get('company') #file = request.files['file'] file = 'C:\\Users\\bhara\\Desktop\\workspace\\questions.xlsx' df = pd.read_excel(file) #df.pop('category_id') list = df['category_name'].unique() cn = 1 print(df.columns) for i in list: df.loc[df['category_name'] == i, 'category_id'] = cn cn += 1 df.category_id = df.category_id.astype(int) #print(df) #Getting timestamp backup = bl.getTimeStamp() #code pending for taking backup of this json jFile = 'C:/Users/bhara/Desktop/workspace/db.json' jsonFile = 'C:/Users/bhara/Desktop/workspace/db_{}.json'.format(backup) copyfile(jFile, jsonFile) #Parsing and divding into sector chunks of data from excel df_R1 = df[df['sector'] == 'R1'] df_R2 = df[df['sector'] == 'R2'] df_R3 = df[df['sector'] == 'R3'] df_R4 = df[df['sector'] == 'R4'] #initialize Mongo DB details or can be taken from a config file in future host = 'localhost' database = 'BKBASE' collection = 'questions' user = '******' pwd = 'Blackpearl' #update the db.json file with related info bl.parseDF(df_R1, df_R2, df_R3, df_R4, jsonFile, survey, company) #push json to mongo bl.pushMongoDB(host, database, collection, jsonFile, user, pwd, survey, company) #function to clean the workspace and close all handlers and files #bl.cleanup() print(df) return 'Upload Done -- replace this with web page'
def genRadar(labels, values, clr, sector): import matplotlib matplotlib.use('Agg') from matplotlib import pyplot from math import pi N = len(values) # What will be the angle of each axis in the plot? (we divide the plot / number of variable) angles = [n / float(N) * 2 * pi for n in range(N)] angles += angles[:1] cn = len(labels) for i in range(cn): labels[i] = labels[i] + ' (' + (str(values[i])) + ')' # Initialise the spider plot ax = plt.subplot(111, polar=True) # Draw one axe per variable + add labels labels yet plt.xticks(angles[:-1], labels, color='black', size=9) # # Draw ylabels ax.set_rlabel_position(0) values += values[:1] # Draw ylabels plt.yticks([1, 2, 3, 4, 5], ["1", "2", "3", "4", "5"], color="black", size=8) plt.ylim(0, 5) # # # Plot data ax.plot(angles, values, linewidth=1.5, color=clr, linestyle='solid') # # # Fill area ax.fill(angles, values, color=clr, alpha=1) # Add a title #plt.title('{} metrics radar display'.format(sector), size=11, color='k', y=1.1) file = 'Report_{}_{}.png'.format(sector, bl.getTimeStamp()) plt.savefig(file, bbox_inches="tight") plt.close() return file
def usersUpload(): survey = request.args.get('survey') company = request.args.get('company') file = request.files['file'] #file = xlrd.open_workbook("/apps/surveyapp_python/user_details.xlsx") df = pd.read_excel(file) # Getting timestamp backup = bl.getTimeStamp() # code pending for taking backup of this json jFile = './users.json' jsonFile = './users_{}.json'.format(backup) copyfile(jFile, jsonFile) lFile = './login.json' ljsonFile = './login_{}.json'.format(backup) copyfile(lFile, ljsonFile) # Parsing and divding into sector chunks of data from excel #df_username = df[df['username']] # initialize Mongo DB details or can be taken from a config file in future host = 'localhost' database = 'Surveyapp' collection = 'userdetails' user = '******' pwd = 'Blackpearl' # update the db.json file with related info bl.parseDFusers(df, jsonFile, ljsonFile, survey, company) # push json to mongo bl.pushMongoDB(host, database, collection, jsonFile, user, pwd, survey, company) # function to clean the workspace and close all handlers and files # bl.cleanup() return 'Upload Done -- replace this with web page'
def questionsUpload(): survey = request.args.get('survey') company = request.args.get('company') file = request.files['file'] #file = xlrd.open_workbook("C:/Users/headway/PycharmProjects/untitled/questions.xlsx") df = pd.read_excel(file) n = 1 df1, n = bl.parse(df, 'R1', 'Physical', n) df2, n = bl.parse(df, 'R1', 'Organizational', n) df3, n = bl.parse(df, 'R1', 'Technical', n) df4, n = bl.parse(df, 'R2', 'Physical', n) df5, n = bl.parse(df, 'R2', 'Organizational', n) df6, n = bl.parse(df, 'R2', 'Technical', n) df7, n = bl.parse(df, 'R3', 'Physical', n) df8, n = bl.parse(df, 'R3', 'Organizational', n) df9, n = bl.parse(df, 'R3', 'Technical', n) df10, n = bl.parse(df, 'R4', 'Physical', n) df11, n = bl.parse(df, 'R4', 'Organizational', n) df12, n = bl.parse(df, 'R4', 'Technical', n) df = pd.concat( [df1, df2, df3, df4, df5, df6, df7, df8, df9, df10, df11, df12]) print(df1[['sector', 'subsector', 'category_id', 'category_name', 'qid']]) print(df2[['sector', 'subsector', 'category_id', 'category_name', 'qid']]) print(df3[['sector', 'subsector', 'category_id', 'category_name', 'qid']]) print(df4[['sector', 'subsector', 'category_id', 'category_name', 'qid']]) print(df5[['sector', 'subsector', 'category_id', 'category_name', 'qid']]) print(df6[['sector', 'subsector', 'category_id', 'category_name', 'qid']]) print(df7[['sector', 'subsector', 'category_id', 'category_name', 'qid']]) print(df8[['sector', 'subsector', 'category_id', 'category_name', 'qid']]) print(df9[['sector', 'subsector', 'category_id', 'category_name', 'qid']]) print(df10[['sector', 'subsector', 'category_id', 'category_name', 'qid']]) print(df11[['sector', 'subsector', 'category_id', 'category_name', 'qid']]) print(df12[['sector', 'subsector', 'category_id', 'category_name', 'qid']]) print("MAINFRAME") print(df[['sector', 'subsector', 'category_name', 'category_id', 'qid']]) fileName = 'dataframe_BK.xlsx' wb = Workbook() sheet1 = wb.add_sheet('Sheet 1') wb.save(fileName) writer = ExcelWriter(fileName) df.to_excel(writer, 'Sheet1', index=False) writer.save() #df.pop('category_id') #print(df) #Getting timestamp backup = bl.getTimeStamp() #code pending for taking backup of this json jFile = './db.json' jsonFile = './db_{}.json'.format(backup) copyfile(jFile, jsonFile) #Parsing and divding into sector chunks of data from excel df_R1 = df[df['sector'] == 'R1'] df_R2 = df[df['sector'] == 'R2'] df_R3 = df[df['sector'] == 'R3'] df_R4 = df[df['sector'] == 'R4'] #initialize Mongo DB details or can be taken from a config file in future host = 'localhost' database = 'Surveyapp' collection = 'questions' user = '******' pwd = 'Blackpearl' #update the db.json file with related info bl.parseDF(df_R1, df_R2, df_R3, df_R4, jsonFile, survey, company) #push json to mongo bl.pushMongoDB(host, database, collection, jsonFile, user, pwd, survey, company) #function to clean the workspace and close all handlers and files #bl.cleanup() return 'Upload Done -- replace this with web page'
def genReport(): survey = request.args.get('survey') company = request.args.get('company') now = datetime.datetime.now() curdate = now.strftime("%Y-%m-%d") host, base, colection, dbuser, pwd = bl.mongoInit('users') #file='C:\\toHDD\\toViggu\\surveyapp_python\\dataframe_1555184982.xlsx' req = requests.get( "http://localhost/backend/chartsAll?survey={}&company={}".format( survey, company)) print(req.text) file = req.text r = requests.get( "http://localhost/backend/getAllRadar?dataframe={}".format(file)) jsonres = r.json() subscores = [] subscores.append(jsonres['Physical'][0]) subscores.append(jsonres['Organizational'][0]) subscores.append(jsonres['Technical'][0]) subscores.append(jsonres['Physical'][1]) subscores.append(jsonres['Organizational'][1]) subscores.append(jsonres['Technical'][1]) subscores.append(jsonres['Physical'][2]) subscores.append(jsonres['Organizational'][2]) subscores.append(jsonres['Technical'][2]) subscores.append(jsonres['Physical'][3]) subscores.append(jsonres['Organizational'][3]) subscores.append(jsonres['Technical'][3]) R0 = cl.genFullRadar(sectors, subsectors, subscores) r = requests.get( "http://localhost/backend/getAllRadarBySector?dataframe={}".format( file)) jsonres = r.json() R1_labels = jsonres[0]['labels'] R1_values = jsonres[0]['score'] R2_labels = jsonres[1]['labels'] R2_values = jsonres[1]['score'] R3_labels = jsonres[2]['labels'] R3_values = jsonres[2]['score'] R4_labels = jsonres[3]['labels'] R4_values = jsonres[3]['score'] R1 = cl.genRadar(R1_labels, R1_values, '#ff6666', 'Robustness') R2 = cl.genRadar(R2_labels, R2_values, '#ffcc99', 'Redundancy') R3 = cl.genRadar(R3_labels, R3_values, '#99ff99', 'Resourcefulness') R4 = cl.genRadar(R4_labels, R4_values, '#66b3ff', 'Rapidity') r = requests.get( "http://localhost/backend/getAllRadarAllSectors?dataframe={}".format( file)) jsonres = r.json() labels = jsonres['category'] values = jsonres['scores'] print(values) labels = pd.Series(labels) values = pd.Series(values) R5 = cl.barh(labels, values) r = requests.get( "http://localhost/backend/getAllRadar?dataframe={}".format(file)) jsonres = r.json() phy = jsonres['Physical'] org = jsonres['Organizational'] tech = jsonres['Technical'] subsecs = ['Physical', 'Organizational', 'Technical'] bar1 = [phy[0], org[0], tech[0]] bar2 = [phy[1], org[1], tech[1]] bar3 = [phy[2], org[2], tech[2]] bar4 = [phy[3], org[3], tech[3]] R1_bar = cl.barRadar('R1', bar1, subsecs) R2_bar = cl.barRadar('R2', bar2, subsecs) R3_bar = cl.barRadar('R3', bar3, subsecs) R4_bar = cl.barRadar('R4', bar4, subsecs) lists = [R0, R1, R2, R3, R4, R5, R1_bar, R2_bar, R3_bar, R4_bar] print(lists) doc = DocxTemplate("sampleWord.docx") s1 = doc.new_subdoc() s1.add_picture(R1, width=Inches(7)) s2 = doc.new_subdoc() s2.add_picture(R2, width=Inches(7)) s3 = doc.new_subdoc() s3.add_picture(R3, width=Inches(7)) s4 = doc.new_subdoc() s4.add_picture(R4, width=Inches(7)) s5 = doc.new_subdoc() s5.add_picture(R0, width=Inches(7)) s6 = doc.new_subdoc() s6.add_picture(R5, width=Inches(7)) s7 = doc.new_subdoc() s7.add_picture(R1_bar, width=Inches(5)) s8 = doc.new_subdoc() s8.add_picture(R2_bar, width=Inches(5)) s9 = doc.new_subdoc() s9.add_picture(R3_bar, width=Inches(5)) s10 = doc.new_subdoc() s10.add_picture(R4_bar, width=Inches(5)) req = requests.get( "http://localhost/backend/surveysCount?survey={}&company={}".format( survey, company)) count = req.text context = { 'bar1': s7, 'bar2': s8, 'bar3': s9, 'bar4': s10, 'chart5': s6, 'radar1': s1, 'radar2': s2, 'radar3': s3, 'radar4': s4, 'complete': s5, 'company': company, 'survey': survey, 'date': curdate, 'number': count } doc.render(context) filename = "Survey_Metrics_{}.docx".format(bl.getTimeStamp()) doc.save(filename) outputFile = bl.convertToPDF(filename) bl.sendReport(survey, company, outputFile) return "Sent Report"
import pandas as pd import businessLogic as bl import sys, os from shutil import copyfile survey = sys.argv[1] company = sys.argv[2] xl = sys.argv[3] #Needs to be updated from config file path = 'C:/Users/bhara/Desktop/workspace' #filepath=os.path.join(path, filename) #Getting timestamp backup = bl.getTimeStamp() #code pending for taking backup of this json jFile = 'C:/Users/bhara/Desktop/workspace/db.json' jsonFile = 'C:/Users/bhara/Desktop/workspace/db_{}.json'.format(backup) copyfile(jFile, jsonFile) # Parsing excel data #xl = pd.read_excel(path+'/'+filename) #xl = pd.read_excel(filename) #Parsing and divding into sector chunks of data from excel df_R1 = xl[xl['sector'] == 'R1'] df_R2 = xl[xl['sector'] == 'R2'] df_R3 = xl[xl['sector'] == 'R3'] df_R4 = xl[xl['sector'] == 'R4']