Added pdf/txt support and quiz template
This commit is contained in:
parent
04674ad88c
commit
33bf7d5264
48
app.py
48
app.py
@ -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 werkzeug.utils import secure_filename
|
||||
from PyPDF2 import PdfFileReader
|
||||
|
||||
# Constants
|
||||
UPLOAD_FOLDER = './pdf/'
|
||||
|
||||
|
||||
# Init an app object
|
||||
app = Flask(__name__)
|
||||
|
||||
UPLOAD_PATH = './pdf/'
|
||||
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
""" 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'])
|
||||
def file_upload():
|
||||
@app.route('/quiz', methods=['GET', 'POST'])
|
||||
def quiz():
|
||||
UPLOAD_STATUS = False
|
||||
if request.method == 'POST':
|
||||
try:
|
||||
uploaded_file = request.files['file']
|
||||
uploaded_file.save(
|
||||
UPLOAD_PATH + secure_filename(uploaded_file.filename))
|
||||
uploaded = True
|
||||
return render_template('index.html', uploaded=uploaded)
|
||||
# Make directory to store uploaded files
|
||||
if not os.path.isdir('./pdf'):
|
||||
os.mkdir('./pdf')
|
||||
# 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:
|
||||
return 'failed to upload file'
|
||||
print(e)
|
||||
return render_template('quiz.html', uploaded=UPLOAD_STATUS, pdftext=uploaded_content)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
1
info.txt
Normal file
1
info.txt
Normal 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
BIN
pdf/test.pdf
Normal file
Binary file not shown.
@ -1 +0,0 @@
|
||||
This is a test file.
|
||||
@ -4,4 +4,5 @@ itsdangerous==1.1.0
|
||||
Jinja2==2.11.2
|
||||
MarkupSafe==1.1.1
|
||||
pkg-resources==0.0.0
|
||||
PyPDF2==1.26.0
|
||||
Werkzeug==1.0.1
|
||||
|
||||
@ -4,15 +4,13 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
<title>Home</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
{% if uploaded == true %}
|
||||
<h1>Your file was uploaded successfully</h1>
|
||||
{% endif %}
|
||||
<form action="http://localhost:5000/upload" method="POST" enctype="multipart/form-data">
|
||||
<input type="file" name="file" />
|
||||
<form action="http://localhost:5000/quiz" method="POST" enctype="multipart/form-data">
|
||||
<!-- Accept only pdf/txt files -->
|
||||
<input type="file" name="file" accept=".txt, application/pdf" />
|
||||
<input type="submit" name="upload file" />
|
||||
</form>
|
||||
</body>
|
||||
|
||||
23
templates/quiz.html
Normal file
23
templates/quiz.html
Normal 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>
|
||||
Loading…
x
Reference in New Issue
Block a user