def plot_set(set_id): if fk.request.method == 'GET': _set = SetModel.objects(id=set_id).first() if _set: if _set.status == 'empty': return core_response(204, 'Nothing done', 'No data in this set.') else: file_buffer = None try: set_plot = SetPlot(set_id) set_plot.plot() filename = 'set-{0}.png'.format(set_id) with open('plots/{0}'.format(filename), 'r') as _file: file_buffer = StringIO(_file.read()) file_buffer.seek(0) except: print traceback.print_exc() if file_buffer != None: os.remove('plots/{0}'.format(filename)) return fk.send_file(file_buffer, attachment_filename=filename, mimetype='image/png') else: return core_response(404, 'Request suggested an empty response', 'Unable to find this file.') else: return core_response(204, 'Nothing done', 'Could not find this set.') else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def download_file(set_id): if fk.request.method == 'GET': _set = SetModel.objects.with_id(set_id) if _set is None: return core_response(404, 'Request suggested an empty response', 'Unable to find this set.') else: file_buffer = None try: file_path = 'sets/{0}'.format(_set.filename) with open(file_path, 'r') as _file: file_buffer = StringIO(_file.read()) file_buffer.seek(0) except: print traceback.print_exc() pass if file_buffer != None: return fk.send_file(file_buffer, as_attachment=True, attachment_filename=_set.filename, mimetype='application/zip') else: return core_response(404, 'Request suggested an empty response', 'Unable to find this set file.') else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def reference_delete(ref_id): if fk.request.method == 'GET': _ref = ReferenceModel.objects.with_id(ref_id) if _ref is None: return core_response(404, 'Request suggested an empty response', 'Unable to find this reference.') else: if _ref.status != 'done': for _set in _ref.sets['sets']: set_ = SetModel.objects.with_id(_set['id']) set_.status = "unlocked" set_.save() else: try: ref_a1 = 'plots/ref-{0}-aliq1.png'.format(str(_ref.id)) ref_a2 = 'plots/ref-{0}-aliq2.png'.format(str(_ref.id)) os.remove(ref_a1) os.remove(ref_a2) sta_a1 = 'plots/stats-ref-{0}-aliq1.png'.format( str(_ref.id)) sta_a2 = 'plots/stats-ref-{0}-aliq2.png'.format( str(_ref.id)) os.remove(sta_a1) os.remove(sta_a2) print "Reference stats deleted." except: print traceback.print_exc() print "plots deletion failed." _ref.delete() return core_response(200, 'Reference [{0}] deleted'.format(ref_id), 'The provided reference has been deleted.') else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def home(): if fk.request.method == 'GET': _refs = [r for r in ReferenceModel.objects()] _ref = _refs[-1] print str(_ref.id) if _ref: if _ref.status != 'done': return core_response(204, 'Nothing done', 'No graph generated yet. This reference is not finished yet.') else: file_buffer = None try: filename = 'error-{0}.png'.format(str(_ref.id)) with open('plots/{0}'.format(filename), 'r') as _file: file_buffer = StringIO(_file.read()) file_buffer.seek(0) except: print traceback.print_exc() if file_buffer != None: return fk.send_file(file_buffer, attachment_filename=filename, mimetype='image/png') else: return fk.redirect('http://0.0.0.0:4000/background1.jpg') # return core_response(404, 'Request suggested an empty response', 'Unable to find this file.') else: return core_response(204, 'Nothing done', 'Could not find this reference.') else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def home(aliq): if fk.request.method == 'GET': # aliq = "aliq1" _refs = [r for r in ReferenceModel.objects()] _ref = _refs[-1] if _ref: if _ref.status != 'done': return core_response( 204, 'Nothing done', 'No graph generated yet. This reference is not finished yet.' ) else: file_buffer = None try: filename = 'error-{0}-{1}.png'.format(str(_ref.id), aliq) with open('plots/{0}'.format(filename), 'r') as _file: file_buffer = StringIO(_file.read()) file_buffer.seek(0) except: print traceback.print_exc() if file_buffer != None: return fk.send_file(file_buffer, attachment_filename=filename, mimetype='image/png') else: return core_response( 404, 'Request suggested an empty response', 'Unable to find this file.') else: return core_response(204, 'Nothing done', 'Could not find this reference.') else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def set_all(): if fk.request.method == 'GET': sets = {'total_sets':0, 'sets':[]} for _set in SetModel.objects(): sets['sets'].append(_set.summary()) sets['total_sets'] = len(sets['sets']) return core_response(200, 'All the sets', sets) else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def set_show(set_id): if fk.request.method == 'GET': _set = SetModel.objects.with_id(set_id) if _set is None: return core_response(404, 'Request suggested an empty response', 'Unable to find this set.') else: return core_response(200, 'Set [{0}] info'.format(_set.filename), _set.info()) else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def set_all(): if fk.request.method == 'GET': sets = {'total_sets': 0, 'sets': []} for _set in SetModel.objects(): sets['sets'].append(_set.summary()) sets['total_sets'] = len(sets['sets']) return core_response(200, 'All the sets', sets) else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def reference_show(ref_id): if fk.request.method == 'GET': _ref = ReferenceModel.objects(id=ref_id).first() if _ref is None: return core_response(404, 'Request suggested an empty response', 'Unable to find this reference.') else: return core_response(200, 'Reference [{0}] info.'.format(str(_ref.id)), _ref.info()) else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def home_set_include(set_id): if fk.request.method == 'GET': _set = SetModel.objects.with_id(set_id) if _set is None: return core_response(404, 'Request suggested an empty response', 'Unable to find this set.') else: if _set.status == 'excluded': _set.status = 'included' _set.save() return fk.redirect('http://0.0.0.0:4000/sets') else: return core_response(204, 'Inclusion failed', 'Set [{0}] is not excluded'.format(_set.filename)) else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def set_include(set_id): if fk.request.method == 'GET': _set = SetModel.objects.with_id(set_id) if _set is None: return core_response(404, 'Request suggested an empty response', 'Unable to find this set.') else: if _set.status == 'excluded': _set.status = 'included' _set.save() return core_response(200, 'Inclusion succeed', 'Set [{0}] is now included for the upcoming new reference.'.format(_set.filename)) else: return core_response(204, 'Inclusion failed', 'Set [{0}] is not excluded'.format(_set.filename)) else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def home_reference_sets(): if fk.request.method == 'GET': refs = [r for r in ReferenceModel.objects()] _ref = refs[-1] if _ref is None: return core_response(404, 'Request suggested an empty response', 'Unable to find the newest reference.') else: sets = [] for _set_id in _ref.sets['sets']: _set = SetModel.objects.with_id(_set_id['id']) if _set: sets.append(_set.summary()) return core_response(200, 'Reference [{0}] sets.'.format(str(_ref.id)), sets) else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def reference_all(): if fk.request.method == 'GET': refs = {'total_refs': 0, 'refs': []} for _ref in ReferenceModel.objects(): if len(_ref.formula) == 0 and _ref.status == "done": z_1 = np.polyfit(_ref.fit_pressure['aliq1'], _ref.mn_uptake['aliq1'], 30) z_2 = np.polyfit(_ref.fit_pressure['aliq2'], _ref.mn_uptake['aliq2'], 30) _ref.formula = {'aliq1': z_1.tolist(), 'aliq2': z_2.tolist()} _ref.save() refs['refs'].append(_ref.summary()) refs['total_refs'] = len(refs['refs']) return core_response(200, 'All the refs', refs) else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def reference_create(): if fk.request.method == 'GET': new_ref = ReferenceModel.objects(status='new').first() if new_ref: return core_response( 200, 'Existing new reference found', 'The reference [{0}] was already created and not used.'.format( str(new_ref.id))) else: _ref, created = ReferenceModel.objects.get_or_create( created_at=str(datetime.datetime.utcnow())) if created: sets = [] for _set in SetModel.objects(): if _set.status != 'excluded': if str(_set.id) not in sets: if _set.updated_from: sets.remove(_set.updated_from) sets.append(str(_set.id)) else: sets.append(str(_set.id)) if len(sets) > 0: _sets = [] for _set in sets: set_ = SetModel.objects.with_id(_set) set_.status = "locked" set_.save() if set_: _sets.append({ 'id': str(set_.id), 'filename': set_.filename }) _ref.sets = {'size': len(_sets), 'sets': _sets} _ref.save() return core_response( 200, 'Creation succeed', 'Reference [{0}] created.'.format(str(_ref.id))) else: return core_response(204, 'Creation failed', 'No data sets provided.') else: return core_response(204, 'Creation failed', 'Could not create a new reference.') else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def reference_sets(ref_id): if fk.request.method == 'GET': _ref = ReferenceModel.objects(id=ref_id).first() if _ref is None: return core_response(404, 'Request suggested an empty response', 'Unable to find this reference.') else: sets = [] for _set_id in _ref.sets['sets']: _set = SetModel.objects.with_id(_set_id['id']) if _set: sets.append(_set.summary()) return core_response(200, 'Reference [{0}] sets.'.format(str(_ref.id)), sets) else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def reference_build(ref_id): if fk.request.method == 'GET': _ref = ReferenceModel.objects(id=ref_id).first() if _ref is None: return core_response(404, 'Request suggested an empty response', 'Unable to find this reference.') else: # Should we allow a built to be done again when already done? if _ref.status == "done": return core_response( 204, 'Built failed', 'Reference[{0}] already built.'.format(ref_id)) else: sets = [] for _set_id in _ref.sets['sets']: _set = SetModel.objects.with_id(_set_id['id']) if _set: sets.append(_set) try: # build # maybe run a daemon build = Build(_ref) build.compute() build.plot_all() build.plot_stat() build.plot_bars() for _set in sets: _set.status = 'used' _set.save() _ref.status = 'done' _ref.save() return core_response( 200, 'Build done.', 'Reference [{0}] was successfully built.'.format( str(_ref.id))) except: print traceback.print_exc() print "reference build failed." return core_response(500, 'Error occured', 'Failed to build this reference.') else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def set_include(set_id): if fk.request.method == 'GET': _set = SetModel.objects.with_id(set_id) if _set is None: return core_response(404, 'Request suggested an empty response', 'Unable to find this set.') else: if _set.status == 'excluded': _set.status = 'included' _set.save() return core_response( 200, 'Inclusion succeed', 'Set [{0}] is now included for the upcoming new reference.' .format(_set.filename)) else: return core_response( 204, 'Inclusion failed', 'Set [{0}] is not excluded'.format(_set.filename)) else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def plot_stats(ref_id): if fk.request.method == 'GET': _ref = ReferenceModel.objects(id=ref_id).first() if _ref: if _ref.status != 'done': return core_response(204, 'Nothing done', 'No graph generated yet. This reference is not finished yet.') else: file_buffer = None try: filename = 'stats-ref-{0}.png'.format(ref_id) with open('plots/{0}'.format(filename), 'r') as _file: file_buffer = StringIO(_file.read()) file_buffer.seek(0) except: print traceback.print_exc() if file_buffer != None: return fk.send_file(file_buffer, attachment_filename=filename, mimetype='image/png') else: return core_response(404, 'Request suggested an empty response', 'Unable to find this file.') else: return core_response(204, 'Nothing done', 'Could not find this reference.') else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def set_delete(set_id): if fk.request.method == 'GET': _set = SetModel.objects.with_id(set_id) if _set is None: return core_response(404, 'Request suggested an empty response', 'Unable to find this set.') else: _refs = ReferenceModel.objects() ref = None for _ref in _refs: for set_ in _ref.sets['sets']: if set_['id'] == set_id: ref = _ref break if ref: return core_response( 204, 'Set deletion failed', 'The set[{0}] was used/is being used in generating a reference[{1}]. It cannot be deleted.' .format(set_id, str(ref.id))) else: file_path = 'sets/{0}'.format(_set.filename) try: os.remove(file_path) print "File deleted." _set.delete() return core_response( 200, 'Set [{0}] deleted'.format(_set.filename), 'The provided set has been deleted.') except: _set.delete() print traceback.print_exc() print "file deletion failed." return core_response(500, 'Error occured', 'Failed to delete the set file.') else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def set_delete(set_id): if fk.request.method == 'GET': _set = SetModel.objects.with_id(set_id) if _set is None: return core_response(404, 'Request suggested an empty response', 'Unable to find this set.') else: _refs = ReferenceModel.objects() ref = None for _ref in _refs: for set_ in _ref.sets['sets']: if set_['id'] == set_id: ref = _ref break if ref: # flash(u'Invalid password provided', 'error') # Materialize.toast('Set deletion faile. The set[{0}] was used/is being used in generating a reference[{1}]. It cannot be deleted.'.format(set_id, str(ref.id)), 4000) return fk.redirect('http://0.0.0.0:4000/sets/?result=failure') # return core_response(204, 'Set deletion failed', 'The set[{0}] was used/is being used in generating a reference[{1}]. It cannot be deleted.'.format(set_id, str(ref.id))) else: file_path = 'sets/{0}'.format(_set.filename) try: os.remove(file_path) print "File deleted." _set.delete() # Materialize.toast('Set [{0}] deleted'.format(_set.filename), 4000) return fk.redirect('http://0.0.0.0:4000/sets/?result=success') # return core_response(200, 'Set [{0}] deleted'.format(_set.filename), 'The provided set has been deleted.') except: _set.delete() print traceback.print_exc() print "file deletion failed." # return core_response(500, 'Error occured', 'Failed to delete the set file.') # Materialize.toast('An error occured. Please try again later.', 4000) return fk.redirect('http://0.0.0.0:4000/sets/?result=failure') else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def home_reference_evaluate_data(): if fk.request.method == 'POST': refs = [r for r in ReferenceModel.objects()] _ref = refs[-1] if _ref is None: return core_response(404, 'Request suggested an empty response', 'Unable to find the newest reference.') else: if fk.request.files: file_obj = fk.request.files['file'] file_name = file_obj.filename _set, created = SetModel.objects.get_or_create(created_at=str(datetime.datetime.utcnow())) if created: _set.filename = '{0}-{1}'.format(str(_set.id), file_name) file_path = '/tmp/{0}'.format(_set.filename) try: with open(file_path, 'wb') as set_file: set_file.write(file_obj.read()) wb = load_workbook(file_path, read_only=True) ws = wb.active pressure = {'aliq1':{'run1':[], 'run2':[]}, 'aliq2':{'run1':[], 'run2':[]}} uptake = {'aliq1':{'run1':[], 'run2':[]}, 'aliq2':{'run1':[], 'run2':[]}} for odx, row in enumerate(ws.rows): if odx >= 2: # print "--- row ---" if row[0].value is not None: pressure['aliq1']['run1'].append(row[0].value) if row[1].value is not None: uptake['aliq1']['run1'].append(row[1].value) if row[3].value is not None: pressure['aliq1']['run2'].append(row[3].value) if row[4].value is not None: uptake['aliq1']['run2'].append(row[4].value) if row[7].value is not None: pressure['aliq2']['run1'].append(row[7].value) if row[8].value is not None: uptake['aliq2']['run1'].append(row[8].value) if row[10].value is not None: pressure['aliq2']['run2'].append(row[10].value) if row[11].value is not None: uptake['aliq2']['run2'].append(row[11].value) evaluation = Evaluation(eval_id=_set.filename, reference=_ref, pressure=pressure, uptake=uptake) evaluation.run() # print str(evals) os.remove(file_path) _set.delete() reslt_path = evaluation.error() file_buffer = None try: with open(reslt_path, 'r') as _file: file_buffer = StringIO(_file.read()) file_buffer.seek(0) except: print traceback.print_exc() if file_buffer != None: # os.remove(reslt_path) return fk.send_file(file_buffer, as_attachment=True, attachment_filename=reslt_path.split('/')[1], mimetype='image/png') else: return core_response(404, 'Request suggested an empty response', 'Unable to return plot image.') except: print traceback.print_exc() _set.delete() print "An error occured!!" return core_response(204, 'Nothing created', 'An error occured.') else: return core_response(204, 'Already exists', 'This should normaly never happened.') else: return core_response(204, 'Nothing created', 'You must a set file.') return """
def set_upload(): if fk.request.method == 'POST': if fk.request.files: file_obj = fk.request.files['file'] file_name = file_obj.filename _set, created = SetModel.objects.get_or_create( created_at=str(datetime.datetime.utcnow())) if created: _set.filename = '{0}-{1}'.format(str(_set.id), file_name) file_path = 'sets/{0}'.format(_set.filename) try: with open(file_path, 'wb') as set_file: set_file.write(file_obj.read()) wb = load_workbook(file_path, read_only=True) ws = wb.active pressure = { 'aliq1': { 'run1': [], 'run2': [] }, 'aliq2': { 'run1': [], 'run2': [] } } uptake = { 'aliq1': { 'run1': [], 'run2': [] }, 'aliq2': { 'run1': [], 'run2': [] } } for odx, row in enumerate(ws.rows): if odx >= 2: # print "--- row ---" if row[0].value is not None: pressure['aliq1']['run1'].append(row[0].value) if row[1].value is not None: uptake['aliq1']['run1'].append(row[1].value) if row[3].value is not None: pressure['aliq1']['run2'].append(row[3].value) if row[4].value is not None: uptake['aliq1']['run2'].append(row[4].value) if row[7].value is not None: pressure['aliq2']['run1'].append(row[7].value) if row[8].value is not None: uptake['aliq2']['run1'].append(row[8].value) if row[10].value is not None: pressure['aliq2']['run2'].append(row[10].value) if row[11].value is not None: uptake['aliq2']['run2'].append(row[11].value) _set.raw_pressure = pressure _set.raw_uptake = uptake print str(pressure) print str(uptake) _set.status = "new" _set.save() return core_response( 200, 'New set {0} uploaded.'.format(file_name), _set.info()) except: print traceback.print_exc() _set.delete() print "An error occured!!" return core_response(204, 'Nothing created', 'An error occured.') else: return core_response(204, 'Already exists', 'This should normaly never happened.') else: return core_response(204, 'Nothing created', 'You must a set file.') # else: # return core_response(405, 'Method not allowed', 'This endpoint supports only a POST method.') return """
def home_reference_next(): if fk.request.method == 'GET': new_ref = ReferenceModel.objects(status='new').first() if new_ref: sets = [] for _set_id in new_ref.sets['sets']: _set = SetModel.objects.with_id(_set_id['id']) if _set: sets.append(_set) try: # build # maybe run a daemon build = Build(new_ref) build.compute() build.plot_all() build.plot_stat() build.plot_bars() for _set in sets: _set.status = 'used' _set.save() new_ref.status = 'done' new_ref.save() return fk.redirect('http://0.0.0.0:4000/') # return core_response(200, 'Build done.', 'Reference [{0}] was successfully built.'.format(str(new_ref.id))) except: print traceback.print_exc() print "reference build failed." return core_response(500, 'Error occured', 'Failed to build this reference.') # return core_response(200, 'Existing new reference found','The reference [{0}] was already created and not used.'.format(str(new_ref.id))) else: _ref, created = ReferenceModel.objects.get_or_create(created_at=str(datetime.datetime.utcnow())) if created: sets = [] for _set in SetModel.objects(): if _set.status != 'excluded': if str(_set.id) not in sets: if _set.updated_from: sets.remove(_set.updated_from) sets.append(str(_set.id)) else: sets.append(str(_set.id)) if len(sets) > 0: _sets = [] for _set in sets: set_ = SetModel.objects.with_id(_set) set_.status = "locked" set_.save() if set_: _sets.append({'id':str(set_.id), 'filename':set_.filename}) _ref.sets = {'size':len(_sets), 'sets':_sets} _ref.save() sets = [] for _set_id in _ref.sets['sets']: _set = SetModel.objects.with_id(_set_id['id']) if _set: sets.append(_set) try: # build # maybe run a daemon build = Build(_ref) build.compute() build.plot_all() build.plot_stat() build.plot_bars() for _set in sets: _set.status = 'used' _set.save() _ref.status = 'done' _ref.save() return fk.redirect('http://0.0.0.0:4000/?reload=true') except: print traceback.print_exc() print "reference build failed." return core_response(500, 'Error occured', 'Failed to build this reference.') else: return core_response(204, 'Creation failed','No data sets provided.') else: return core_response(204, 'Creation failed','Could not create a new reference.') else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def reference_evaluate_data(ref_id): if fk.request.method == 'POST': _ref = ReferenceModel.objects.with_id(ref_id) if _ref is None: return core_response(404, 'Request suggested an empty response', 'Unable to find this reference.') else: if fk.request.files: file_obj = fk.request.files['file'] file_name = file_obj.filename _set, created = SetModel.objects.get_or_create( created_at=str(datetime.datetime.utcnow())) if created: _set.filename = '{0}-{1}'.format(str(_set.id), file_name) file_path = 'sets/tmp-{0}'.format(_set.filename) try: with open(file_path, 'wb') as set_file: set_file.write(file_obj.read()) wb = load_workbook(file_path, read_only=True) ws = wb.active pressure = { 'aliq1': { 'run1': [], 'run2': [] }, 'aliq2': { 'run1': [], 'run2': [] } } uptake = { 'aliq1': { 'run1': [], 'run2': [] }, 'aliq2': { 'run1': [], 'run2': [] } } for odx, row in enumerate(ws.rows): if odx >= 2: # print "--- row ---" if row[0].value is not None: pressure['aliq1']['run1'].append( row[0].value) if row[1].value is not None: uptake['aliq1']['run1'].append( row[1].value) if row[3].value is not None: pressure['aliq1']['run2'].append( row[3].value) if row[4].value is not None: uptake['aliq1']['run2'].append( row[4].value) if row[7].value is not None: pressure['aliq2']['run1'].append( row[7].value) if row[8].value is not None: uptake['aliq2']['run1'].append( row[8].value) if row[10].value is not None: pressure['aliq2']['run2'].append( row[10].value) if row[11].value is not None: uptake['aliq2']['run2'].append( row[11].value) evaluation = Evaluation(eval_id=_set.filename, reference=_ref, pressure=pressure, uptake=uptake) evaluation.run() os.remove(file_path) _set.delete() return core_response( 200, 'Results of new set {0} evaluated on reference[{1}].' .format(file_name, ref_id), evaluation.results) except: print traceback.print_exc() _set.delete() print "An error occured!!" return core_response(204, 'Nothing created', 'An error occured.') else: return core_response( 204, 'Already exists', 'This should normaly never happened.') else: return core_response(204, 'Nothing created', 'You must a set file.') # else: # return core_response(405, 'Method not allowed', 'This endpoint supports only a POST method.') return """
def home_sets(): if fk.request.method == 'GET': sets = [_set.summary() for _set in SetModel.objects()] return core_response(200, 'All sets.', sets) else: return core_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
def reference_evaluate_plot(ref_id, aliq): if fk.request.method == 'POST': _ref = ReferenceModel.objects.with_id(ref_id) if _ref is None: return core_response(404, 'Request suggested an empty response', 'Unable to find this reference.') else: if fk.request.files: file_obj = fk.request.files['file'] file_name = file_obj.filename _set, created = SetModel.objects.get_or_create( created_at=str(datetime.datetime.utcnow())) if created: _set.filename = '{0}-{1}'.format(str(_set.id), file_name) file_path = '/tmp/{0}'.format(_set.filename) try: with open(file_path, 'wb') as set_file: set_file.write(file_obj.read()) wb = load_workbook(file_path, read_only=True) ws = wb.active pressure = { 'aliq1': { 'run1': [], 'run2': [] }, 'aliq2': { 'run1': [], 'run2': [] } } uptake = { 'aliq1': { 'run1': [], 'run2': [] }, 'aliq2': { 'run1': [], 'run2': [] } } for odx, row in enumerate(ws.rows): if odx >= 2: # print "--- row ---" if row[0].value is not None: pressure['aliq1']['run1'].append( row[0].value) if row[1].value is not None: uptake['aliq1']['run1'].append( row[1].value) if row[3].value is not None: pressure['aliq1']['run2'].append( row[3].value) if row[4].value is not None: uptake['aliq1']['run2'].append( row[4].value) if row[7].value is not None: pressure['aliq2']['run1'].append( row[7].value) if row[8].value is not None: uptake['aliq2']['run1'].append( row[8].value) if row[10].value is not None: pressure['aliq2']['run2'].append( row[10].value) if row[11].value is not None: uptake['aliq2']['run2'].append( row[11].value) evaluation = Evaluation(eval_id=_set.filename, reference=_ref, pressure=pressure, uptake=uptake) evaluation.run() # print str(evals) os.remove(file_path) _set.delete() # reslt_path = evaluation.plot(aliq) reslt_path = evaluation.error(aliq) file_buffer = None try: with open(reslt_path, 'r') as _file: file_buffer = StringIO(_file.read()) file_buffer.seek(0) except: print traceback.print_exc() if file_buffer != None: os.remove(reslt_path) return fk.send_file( file_buffer, attachment_filename=reslt_path.split('/')[1], mimetype='image/png') else: return core_response( 404, 'Request suggested an empty response', 'Unable to return plot image.') except: print traceback.print_exc() _set.delete() print "An error occured!!" return core_response(204, 'Nothing created', 'An error occured.') else: return core_response( 204, 'Already exists', 'This should normaly never happened.') else: return core_response(204, 'Nothing created', 'You must a set file.') # else: # return core_response(405, 'Method not allowed', 'This endpoint supports only a POST method.') return """