Added pdf/txt support and quiz template

This commit is contained in:
user86 2020-10-06 19:22:32 +05:30
parent 04674ad88c
commit 33bf7d5264
7 changed files with 66 additions and 18 deletions

48
app.py
View File

@ -1,30 +1,56 @@
from flask import Flask, render_template import os
from flask import Flask, render_template, redirect, url_for
from flask.globals import request from flask.globals import request
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from PyPDF2 import PdfFileReader
# Constants
UPLOAD_FOLDER = './pdf/'
# Init an app object # Init an app object
app = Flask(__name__) app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
UPLOAD_PATH = './pdf/'
@app.route('/') @app.route('/')
def index(): def index():
""" The landing page for the app """ """ The landing page for the app """
return render_template('index.html') # render foe template from ./templates return render_template('index.html')
@app.route('/upload', methods=['GET', 'POST']) @app.route('/quiz', methods=['GET', 'POST'])
def file_upload(): def quiz():
UPLOAD_STATUS = False
if request.method == 'POST': if request.method == 'POST':
try: try:
uploaded_file = request.files['file'] uploaded_file = request.files['file']
uploaded_file.save( # Make directory to store uploaded files
UPLOAD_PATH + secure_filename(uploaded_file.filename)) if not os.path.isdir('./pdf'):
uploaded = True os.mkdir('./pdf')
return render_template('index.html', uploaded=uploaded) # Save uploaded file
uploaded_file.save(os.path.join(
app.config['UPLOAD_FOLDER'], secure_filename(uploaded_file.filename)))
UPLOAD_STATUS = True
# Identify file type and other stuff
uploaded_content = None
file_exten = uploaded_file.filename.rsplit('.', 1)[1].lower()
if file_exten == 'pdf':
# TODO: Move PDF2Text conversion to another file
print('PDF detected')
with open(os.path.join(
app.config['UPLOAD_FOLDER'], secure_filename(uploaded_file.filename)), 'rb') as pdf_file:
pdf_reader = PdfFileReader(pdf_file)
uploaded_content = pdf_reader.getPage(0).extractText()
print(uploaded_content)
else:
# Read text file and store contents
pass
except Exception as e: except Exception as e:
return 'failed to upload file' print(e)
return render_template('quiz.html', uploaded=UPLOAD_STATUS, pdftext=uploaded_content)
if __name__ == "__main__": if __name__ == "__main__":

1
info.txt Normal file
View File

@ -0,0 +1 @@
I have worked as a backend developer for creating REST API based based systems and worked with front-end integration for MEAN stack apps. For developing apps with a decoupled architecture. I have focused on security and performance to improve response time. I am well versed in Python, NodeJS and related tools like redis for building and maintaining a scalable architecture. I also have deployed production ready apps used by hundreds of people using AWS EC2, S3 and other cloud based tools. I frequently contribute to open source and support open source tools by creating issues, PRs for tools that I use frequently to try and give back to the community.

BIN
pdf/test.pdf Normal file

Binary file not shown.

View File

@ -1 +0,0 @@
This is a test file.

View File

@ -4,4 +4,5 @@ itsdangerous==1.1.0
Jinja2==2.11.2 Jinja2==2.11.2
MarkupSafe==1.1.1 MarkupSafe==1.1.1
pkg-resources==0.0.0 pkg-resources==0.0.0
PyPDF2==1.26.0
Werkzeug==1.0.1 Werkzeug==1.0.1

View File

@ -4,15 +4,13 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title> <title>Home</title>
</head> </head>
<body> <body>
{% if uploaded == true %} <form action="http://localhost:5000/quiz" method="POST" enctype="multipart/form-data">
<h1>Your file was uploaded successfully</h1> <!-- Accept only pdf/txt files -->
{% endif %} <input type="file" name="file" accept=".txt, application/pdf" />
<form action="http://localhost:5000/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" name="upload file" /> <input type="submit" name="upload file" />
</form> </form>
</body> </body>

23
templates/quiz.html Normal file
View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Your quiz will appear here</title>
</head>
<body>
{% if uploaded == true %}
<h1>Your file was uploaded successfully</h1>
<h1>Your quiz will appear here</h1>
<code>Contents of your file</code>
<br />
<br />
{{ pdftext }}
{% else %}
<h1>Could not upload file</h1>
{% endif %}
</body>
</html>