From e6f9ac6edea9b2423ab6f1ef18aaaa6068d1bf16 Mon Sep 17 00:00:00 2001 From: c22090044 <LuZ29@cardiff.ac.uk> Date: Thu, 16 Mar 2023 20:25:14 +0000 Subject: [PATCH] ziyan --- README.md => CMT313_assess_3/README.md | 0 CMT313_assess_3/app.py | 50 ++++++++++++++++++++++ CMT313_assess_3/static/style.css | 59 ++++++++++++++++++++++++++ CMT313_assess_3/templates/analyze.html | 16 +++++++ CMT313_assess_3/templates/index.html | 16 +++++++ CMT313_assess_3/templates/layout.html | 46 ++++++++++++++++++++ CMT313_assess_3/uploads/txt | 1 + 7 files changed, 188 insertions(+) rename README.md => CMT313_assess_3/README.md (100%) create mode 100644 CMT313_assess_3/app.py create mode 100644 CMT313_assess_3/static/style.css create mode 100644 CMT313_assess_3/templates/analyze.html create mode 100644 CMT313_assess_3/templates/index.html create mode 100644 CMT313_assess_3/templates/layout.html create mode 100644 CMT313_assess_3/uploads/txt diff --git a/README.md b/CMT313_assess_3/README.md similarity index 100% rename from README.md rename to CMT313_assess_3/README.md diff --git a/CMT313_assess_3/app.py b/CMT313_assess_3/app.py new file mode 100644 index 0000000..e449e8b --- /dev/null +++ b/CMT313_assess_3/app.py @@ -0,0 +1,50 @@ +from flask import Flask, render_template, request, redirect, flash +from werkzeug.utils import secure_filename +import os + +app = Flask(__name__) +app.secret_key = 'some_secret_key' +app.config['UPLOAD_DIRECTORY'] = 'uploads/' +app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 * 1024 +app.config['ALLOWED_EXTENSIONS'] = ['.txt', '.py', '.word'] + +@app.route('/') +def index(): + return render_template('index.html') + +@app.route('/upload', methods = ['POST']) +def upload(): + file = request.files['file'] + extension = os.path.splitext(file.filename)[1].lower() + + if file: + + if extension not in app.config['ALLOWED_EXTENSIONS']: + flash("Please upload word file or py file") + else: + file.save(os.path.join(app.config['UPLOAD_DIRECTORY'], secure_filename(file.filename))) + + return redirect('/') + +@app.route('/analyze', methods = ['GET']) +def analyze(): + file_list = os.listdir(app.config['UPLOAD_DIRECTORY']) + contents = [] + + for file_name in file_list: + with open(os.path.join(app.config['UPLOAD_DIRECTORY'], file_name), 'r') as file: + content = file.read() + words = content.split() + word_counts = {} + for word in words: + if word not in word_counts: + word_counts[word] = 1 + else: + word_counts[word] += 1 + contents.append((file_name, word_counts)) + + return render_template('analyze.html', contents=contents) + + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file diff --git a/CMT313_assess_3/static/style.css b/CMT313_assess_3/static/style.css new file mode 100644 index 0000000..9431aba --- /dev/null +++ b/CMT313_assess_3/static/style.css @@ -0,0 +1,59 @@ +body { + background-color: #f4f4f4; + font-family: Arial, sans-serif; +} + +.app { + max-width: 800px; + margin: 0 auto; + padding: 20px; +} + +.form-section { + background-color: #fff; + border-radius: 5px; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); + padding: 20px; + margin-bottom: 20px; +} + +form { + height: 100px; + display: flex; + flex-direction: column; + align-items: center; +} + +input[type="file"] { + padding: 10px; + margin-bottom: 10px; + border-radius: 5px; + border: none; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); +} + +input[type="submit"] { + background-color: #4CAF50; + color: #fff; + padding: 10px 20px; + border: none; + border-radius: 5px; + cursor: pointer; + transition: background-color 0.3s; +} + +input[type="submit"]:hover { + background-color: #3e8e41; +} + +input[type="file"] { + width: 100%; + height: 50px; + background-color: #fff; + border: 1px solid #ccc; + border-radius: 5px; + color: #333; + padding: 10px; + font-size: 16px; + font-weight: bold; + } \ No newline at end of file diff --git a/CMT313_assess_3/templates/analyze.html b/CMT313_assess_3/templates/analyze.html new file mode 100644 index 0000000..7026bc0 --- /dev/null +++ b/CMT313_assess_3/templates/analyze.html @@ -0,0 +1,16 @@ +{% extends "layout.html" %} +{% block content %} + <h1>Analysis Results</h1> + <ul> + {% for content in contents %} + <li> + <h3>{{ content[0] }}</h3> + <ul> + {% for word, count in content[1].items() %} + <li>{{ word }} : {{count}}</li> + {% endfor %} + </ul> + </li> + {% endfor %} + </ul> +{%endblock%} \ No newline at end of file diff --git a/CMT313_assess_3/templates/index.html b/CMT313_assess_3/templates/index.html new file mode 100644 index 0000000..565c6fa --- /dev/null +++ b/CMT313_assess_3/templates/index.html @@ -0,0 +1,16 @@ +{% extends "layout.html" %} +{% block content %} +<div class = "app"> + <div class="form-section"> + <form action="/upload" method = "post" enctype="multipart/form-data"> + <input type="file" name = "file" id = "file-input"> + <input type="submit" value = "Upload"> + </form> + </div> + + <div class="images-section"> + + </div> +</div> + +{%endblock%} \ No newline at end of file diff --git a/CMT313_assess_3/templates/layout.html b/CMT313_assess_3/templates/layout.html new file mode 100644 index 0000000..29a104c --- /dev/null +++ b/CMT313_assess_3/templates/layout.html @@ -0,0 +1,46 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta http-equiv="X-UA-Compatible" content="IE=edge"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <link + rel="stylesheet" + href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" + integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" + crossorigin="anonymous" + /> + <link + rel="stylesheet" + href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" + crossorigin="anonymous" + /> + <title>File Uploads</title> + <link rel="stylesheet" href="{{ url_for('static', filename = 'style.css') }}"> +</head> +<body> + <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> + <div class="collapse navbar-collapse" id="navbar"> + <div class="navbar-nav"> + <a class="nav-item nav-link" id="home" href="/">Home</a> + <a class="nav-item nav-link" id="logout" href="/analyze">analyze</a> + </div> + </div> + </nav> + {% with messages = get_flashed_messages() %} + {% if messages %} + <ul class="flashes"> + {% for message in messages %} + <li>{{ message }}</li> + {% endfor %} + </ul> + {% endif %} + {% endwith %} + <div id="content"> + {% block content %} + {% endblock %} + </div> + + +</body> +</html> \ No newline at end of file diff --git a/CMT313_assess_3/uploads/txt b/CMT313_assess_3/uploads/txt new file mode 100644 index 0000000..72deff1 --- /dev/null +++ b/CMT313_assess_3/uploads/txt @@ -0,0 +1 @@ +My name is Ziyan.Lu and Fuck you \ No newline at end of file -- GitLab