Select Git revision
Modul_01.ipynb
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
app.py 2.36 KiB
import sys
from pathlib import Path
import pdfplumber
from flask import Flask, render_template, request, redirect, send_file
from werkzeug.utils import secure_filename
from search_index import SearchIndex
import os
app = Flask(__name__)
SLIDE_DIR = "slides"
IMAGE_DIR = "img_cache"
@app.route('/')
def index():
ll = os.listdir(SLIDE_DIR)
return render_template('index.html', ll=ll)
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ["pdf"]
@app.route('/upload', methods=['POST'])
def upload():
# check if the post request has the file part
if 'file' in request.files:
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename != '':
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(Path(SLIDE_DIR), filename)
file.save(file_path)
pdf = pdfplumber.open(file_path)
index = SearchIndex()
for i, page in enumerate(pdf.pages):
text = page.extract_text()
index.add(str(file_path), i, text)
img_name = str(file_path)[7:] + "_" + str(i) + ".jpg"
img_path = os.path.join(IMAGE_DIR, img_name)
page.to_image().save(img_path)
del index
return redirect('/')
@app.route("/search")
def query():
try:
index = SearchIndex()
query = request.args.get("term")
result = index.search(query)
#pdf = pdfplumber.open(result["path"])
#page = pdf.pages[result["page"]]
img_name = result["path"][7:] + "_" + str(result["page"]) + ".jpg"
#img_path = os.path.join(IMAGE_DIR, img_name)
#page.to_image().save(img_path)
return "slide/" + img_name
except:
return str(sys.exc_info()[0]).replace("<", "-").replace(">", "-")
@app.route("/slide/<img_name>")
def slide(img_name):
path = os.path.join(IMAGE_DIR, img_name)
if "/" in img_name or "\\" in img_name:
return "bad image name", 400
if not os.path.exists(path):
return "image not present", 404
return send_file(path, mimetype='image/jpg')
if __name__ == '__main__':
app.debug = True
app.run(host="0.0.0.0")