Skip to content
Snippets Groups Projects
Commit 73236823 authored by Geno Jayadi's avatar Geno Jayadi
Browse files

added some extra comments

parent 3d8ce1e0
No related branches found
No related tags found
No related merge requests found
"""
The main app. This handles both the backend and the frontend side.
render_template just renders the html from the "template" folder.
redirect_url redirects towards a certain url, but does NOT render a template if the pointed url also does not.
"""
from flask import Flask, render_template, url_for, request, redirect, flash from flask import Flask, render_template, url_for, request, redirect, flash
from datetime import datetime from datetime import datetime
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
import os, time, sys import os, time, sys
""" """
The following are the import for the backend. Just write the name of the script without .py The following are the import for the backend. Just write the name of the script without .py.
Then you can immediately use the function from the script directly in the app.
""" """
sys.path.append(os.path.join(os.path.dirname(sys.path[0]),'backend')) sys.path.append(os.path.join(os.path.dirname(sys.path[0]),'backend'))
import input import input
...@@ -11,7 +18,7 @@ import input ...@@ -11,7 +18,7 @@ import input
#define the app #define the app
app = Flask(__name__) app = Flask(__name__)
#the secret key :) #the super secret key that allows browser based flashes :)
app.secret_key = '54321' app.secret_key = '54321'
#specifies where the upload directory is and the file type available for uploads #specifies where the upload directory is and the file type available for uploads
...@@ -24,13 +31,17 @@ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER ...@@ -24,13 +31,17 @@ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def index(): def index():
return render_template('index.html') return render_template('index.html')
#the upload functionality. It then redirects to the case labeling page """
The following is the upload functionality. It uploads directly into the "upload" directory.
It redirects towards the case ID input page.
"""
@app.route('/', methods=['POST']) @app.route('/', methods=['POST'])
def upload_file(): def upload_file():
uploaded_file = request.files['file'] uploaded_file = request.files['file']
if uploaded_file.filename != '': if uploaded_file.filename != '':
uploaded_file.save(os.path.join(app.config['UPLOAD_FOLDER'],uploaded_file.filename)) uploaded_file.save(os.path.join(app.config['UPLOAD_FOLDER'],uploaded_file.filename))
elif uploaded_file.filename == '': elif uploaded_file.filename == '':
#if no file has been selected, flashes a warning
flash('No files selected! Please select a file!') flash('No files selected! Please select a file!')
return redirect(url_for('index')) return redirect(url_for('index'))
return redirect(url_for('case')) return redirect(url_for('case'))
...@@ -50,22 +61,35 @@ def signup(): ...@@ -50,22 +61,35 @@ def signup():
def case(): def case():
return render_template('case.html') return render_template('case.html')
#eventually to input the case ID, case ID is then sent to backend """
Route for inputting case ID. For now it redirects towards "recommendation" alongside "id".
"""
@app.route('/case.html', methods=['POST']) @app.route('/case.html', methods=['POST'])
def get_caseid(): def get_caseid():
caseid = request.form['caseId'] caseid = request.form['caseId']
if caseid != '': if caseid == '':
return redirect(url_for('recommendation',id=caseid)) #if no caseID has been inputted, flashes a warning.
else:
flash('Please input a case ID!') flash('Please input a case ID!')
return redirect(url_for('case')) return redirect(url_for('case'))
elif not caseid.isdigit():
#if the input is not an integer, flashes a warning.
flash('Please input an integer!')
return redirect(url_for('case'))
else:
#else it will redirect to ../recommendation/id
return redirect(url_for('recommendation',id=caseid))
#placeholder result page route. Will eventually be removed and is now defunct. #placeholder result page route. Will eventually be removed and is now defunct.
@app.route('/result.html') @app.route('/result.html')
def result(): def result():
return render_template('result.html') return render_template('result.html')
#the actual result page route, accepts result from backend but for now it's specifically from input.py """
The result page.
It processes "id" (for now it uses the "foo" function from input.py) using backend functions
and prints out the result immediately in the page.
"""
@app.route('/result.html/<id>', methods=['GET']) @app.route('/result.html/<id>', methods=['GET'])
def recommendation(id): def recommendation(id):
res = input.foo(id) res = input.foo(id)
......
<!--
The base of the website. Includes: Navigation bar, warning flash, and also the head of the site.
-->
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
......
<!--
Here is the case ID input page. Really straightforward as it is just one form and one input.
-->
{% extends 'base.html' %} {% extends 'base.html' %}
{%block head%} {%block head%}
...@@ -9,7 +12,7 @@ ...@@ -9,7 +12,7 @@
<h1>Choose the case you want to optimize</h1> <h1>Choose the case you want to optimize</h1>
<form method="post" id="caseid" action="{{ url_for('case')}}"> <form method="post" id="caseid" action="{{ url_for('case')}}">
<label for="caseId"> Please enter the case ID:</label><br> <label for="caseId"> Please enter the case ID:</label><br>
<input type="text" id="caseId" name="caseId"><br> <input type="text" id="caseId" name="caseId" type="number"><br>
</form> </form>
<button onclick="window.location.href='result.html'" type="submit" class="button" form="caseid" value="casevalue"> Optimize now!</button> <button onclick="window.location.href='result.html'" type="submit" class="button" form="caseid" value="casevalue"> Optimize now!</button>
</div> </div>
......
<!--
The result. For now it outputs a placeholder image moyai.jpg.
"res" is the result specified by the Flask app. Use that to print out the result in the bracket.
-->
{% extends 'base.html' %} {% extends 'base.html' %}
{%block head%} {%block head%}
...@@ -8,6 +12,7 @@ ...@@ -8,6 +12,7 @@
<div style="text-align: center;"> <div style="text-align: center;">
<h1>Here are the results of the optimization!</h1> <h1>Here are the results of the optimization!</h1>
<h2>We recommend you to do the following activity for this specific case:</h2> <h2>We recommend you to do the following activity for this specific case:</h2>
<!--res is the result-->
<h3>Placeholder result: {{ res }}</h3> <h3>Placeholder result: {{ res }}</h3>
<img src="{{url_for('static',filename='moyai.jpg')}}" alt="Placeholder"> <br> <img src="{{url_for('static',filename='moyai.jpg')}}" alt="Placeholder"> <br>
<button onclick="window.location.href='/'" class="button"> Optimize your next case NOW!</button> <button onclick="window.location.href='/'" class="button"> Optimize your next case NOW!</button>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment