diff options
| author | Mitsuo Tokumori <rtokumori@pucp.edu.pe> | 2023-11-13 10:55:39 -0500 |
|---|---|---|
| committer | Mitsuo Tokumori <rtokumori@pucp.edu.pe> | 2023-11-13 10:55:39 -0500 |
| commit | 5a16046eddc753b752c5ab1fdb91595adf588a6b (patch) | |
| tree | 870192e1f576754c6fd669cd4e3dad6f70a7caf7 /ustayml/views/load_data.py | |
| parent | f6fcf9cc3ae3d93d59391b3f12843fba3297f0b2 (diff) | |
Add sample file upload for "load_data" blueprint
Also add a success page that redirects to the Dashboard
Diffstat (limited to 'ustayml/views/load_data.py')
| -rw-r--r-- | ustayml/views/load_data.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/ustayml/views/load_data.py b/ustayml/views/load_data.py new file mode 100644 index 0000000..e57f40c --- /dev/null +++ b/ustayml/views/load_data.py @@ -0,0 +1,50 @@ +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('No selected file') + 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
\ No newline at end of file |
