def data_inserted(): abs_notif = [] times = [] # usual issue of int changed to unicode str in session dict... pop = {int(k): session['pop'][k] for k in session['pop']} # we also want to check that there is enough recent data recent_data = 0 for y in session['hist_years']: for i in session['columns']: if ut.is_int(request.form["%s_%s" %(y, i)]) and pop[y] != 0: times.append((int(y), i)) abs_notif.append(int(request.form["%s_%s" %(y, i)])) if y >= SEC_LAST: recent_data += 1 # Check if enough recent data: # e.g. for quarterly it means at least 4 quarters after SEC_LAST year if recent_data < session['periods'] or len(times) < T: return render_template("error.html", error=e.error_not_recent(T, SEC_LAST)) # arrange the data in lists periods = session['periods'] data = {'years': times, 'abs_notif': abs_notif} session['data'] = ed.arrange_data(data, pop, periods=periods) abs_notif = [int(n) for n in session['data']['abs_notif']] # plot it figdata = ed._plot_time_series(session['data'], periods=periods) # Display notification data to_display = zip(session['data']['times'], abs_notif) # Prepare empty rows for all years and then fill them hist = {y: [' ' for p in range(session['periods'])] for y in range(to_display[0][0][0], to_display[-1][0][0]+1)} for t, n in to_display: hist[t[0]][t[1]-1] = n #column headers if periods == 1: columns = ['Notifications'] elif periods == 4: columns = ['%d' %i for i in range(1,5)] elif periods == 12: columns = ['%d' %i for i in range(1,13)] return render_template("inserted_data.html", fig=figdata, hist=hist, columns=columns)
def data_inserted_2(): abs_notif = [] times = [] # usual issue of int changed to unicode str in session dict... pop = {int(k): session['pop'][k] for k in session['pop']} # we also want to check that there is enough recent data recent_data = 0 # Get the name of the uploaded file f = request.files['file'] # Check if the file is one of the allowed types/extensions if allowed_file(f.filename): # Make the filename safe, remove unsupported chars filename = secure_filename(f.filename) # Save the file f.save(filename) else: return render_template('error.html', error = 'The uploaded file does \ not have csv extension.') try: with open(f.filename, 'r') as g: df = pd.read_csv(g) except: #for all possible errors... return render_template('error.html', error = 'No file loaded or \ incorrect format.') if session['periods'] == 1: headers = ['year', '1'] if session['periods'] == 4: headers = ['year', '1', '2', '3', '4'] if session['periods'] == 12: headers = ['year', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'] df_headers = list(df.columns) if len(headers) != len(df_headers): return render_template('error.html', error = 'Unexpected number of \ columns in the uploaded file!') for i in range(len(headers)): if headers[i] != df_headers[i]: return render_template('error.html', error = 'Some column header \ does not have the right format!') df_years = list(df.year) for y in session['hist_years']: for i in session['columns']: if y in df_years and ut.is_int(df[df['year']==y].iloc[0]['%s' %i]) and pop[y] != 0: times.append((int(y), i)) abs_notif.append(int(df[df['year']==y].iloc[0]['%s' %i])) if y >= SEC_LAST: recent_data += 1 os.remove(filename) # Check if enough recent data: # e.g. for quarterly it means at least 4 quarters after SEC_LAST year if recent_data < session['periods'] or len(times) < T: return render_template("error.html", error=e.error_not_recent(T, SEC_LAST)) # arrange the data in lists periods = session['periods'] data = {'years': times, 'abs_notif': abs_notif} session['data'] = ed.arrange_data(data, pop, periods=periods) abs_notif = [int(n) for n in session['data']['abs_notif']] # plot it figdata = ed._plot_time_series(session['data'], periods=periods) # Display notification data to_display = zip(session['data']['times'], abs_notif) # Prepare empty rows for all years and then fill them hist = {y: [' ' for p in range(session['periods'])] for y in range(to_display[0][0][0], to_display[-1][0][0]+1)} for t, n in to_display: hist[t[0]][t[1]-1] = n #column headers if periods == 1: columns = ['Notifications'] elif periods == 4: columns = ['%d' %i for i in range(1,5)] elif periods == 12: columns = ['%d' %i for i in range(1,13)] return render_template("inserted_data.html", fig=figdata, hist=hist, columns=columns)