import os from flask import ( Blueprint, flash, g, redirect, render_template, request, url_for, current_app ) from werkzeug.exceptions import abort from werkzeug.utils import secure_filename from ustayml.views.auth import login_required from ustayml.db import get_db, get_paginated_rows, get_row bp = Blueprint('load_data', __name__, url_prefix='/load_data') ALLOWED_EXTENSIONS = ['txt', 'csv'] @bp.route('/', methods=('GET', 'POST')) @login_required def index(): if request.method == 'POST': # check if the post request has the file part if 'diccionario' not in request.files: flash('No file part') return redirect(request.url) file = request.files['diccionario'] # If the user does not select a file, the browser submits an # empty file without a filename. if file.filename == '': flash('Por favor, suba el diccionario de variables') return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(current_app.config['DATASET_PATH'], filename)) return redirect(url_for('load_data.success', name=filename)) return render_template( 'load_data/index.html' ) @bp.route('/success', methods=('GET', 'POST')) @login_required def success(): return render_template( 'load_data/success.html' ) # Helper functions def allowed_file(filename): return '.' in filename and \ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS