aboutsummaryrefslogtreecommitdiffstats
path: root/Web/Server/Server.py
blob: 607040601100431a8f16daeae90127cecb35cfce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
try: #python3
    from urllib.request import urlopen
except: #python2
    from urllib2 import urlopen
from flask import Flask, flash, request, redirect, render_template, Response
from werkzeug.utils import secure_filename
import requests
import json

app = Flask(__name__)
app.secret_key = "ski u mah"
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024

ALLOWED_EXTENSIONS = set(['py'])

PI_URL = 'http://localhost:8000'

# UNCOMMENT IF RUNNING PI.PY ON PI
#PI_URL = 'http://192.168.1.10:8000'

RESULTS_DESTINATION = "Results/results.csv"

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
    
@app.route('/')
def home():
    return render_template('index.html')

@app.route('/upload', methods=['POST'])
def upload_file():
    if request.method == 'POST':
        # Check if the post request has the file part
        if 'file' not in request.files:
            print("hi")
            return render_template('index.html', error = 'No file part')

        # Grab the file
        file = request.files['file']
        if file.filename == '':
            print("hi")
            return render_template('index.html', error = 'No file selected for uploading')

        # Send the file content as a post to the PI
        if file and allowed_file(file.filename):

            dictToSend = {'filename':file.filename, 'file_content':file.read()}
            file.close()
            print('Running test')
            response = requests.post(PI_URL + '/tests/endpoint', json=dictToSend)
            
            results_filename = json.loads(response.text)[u'results_filename']
            results_content = json.loads(response.text)[u'results_content']
            flash('Results file:' + results_filename)
            flash('Response from server:' + results_content)
            
            results = open(RESULTS_DESTINATION, "w")
            results.write(results_content)
            results.close()

            return render_template('results.html')
        else:
            print("hi")
            return render_template('index.html', error = 'Allowed file types are .py')
    return render_template('index.html', error = 'No file selected')

@app.route('/results', methods=['GET'])
def download():
    # Grab content from results file
    with open(RESULTS_DESTINATION, 'r') as results:
        results_content = results.read()
        results.close()

    # Put content as a download file 
    return Response(
        results_content,
        mimetype="text/csv",
        headers={"Content-disposition":
                    "attachment; filename=results.csv"})

if __name__ == "__main__":
    app.run(host="localhost", port=5000)