Я пытаюсь создать простое веб-приложение с использованием Flask, в котором выбираются два документа, а затем выполняется функция (функция dif()
, которая вычисляет лексические различия и результаты в изображении), а затем изображение будет показаноПользователь.
Мне удалось показать изображение, если в функцию были жестко запрограммированы два документа, но не с помощью выбора файла пользователя. Я знаю, что мой upload.html
файл неполон, но я не уверен, что нужно удалить / добавить / изменить.
rout.py
import io
from app import app
from app.forms import LoginForm
from pdf_diff.document_differences import dif
from flask import request
from flask import send_file
#converts PIL image into a JPEG
def serve_pil_image(pil_img):
img_io = io.BytesIO()
pil_img.save(img_io, 'JPEG')
img_io.seek(0)
return send_file(img_io, mimetype='image/jpeg')
#shows the image from the dif function, using serve_pil_image
@app.route('/test')
def serve_img():
file1 = "path/to/pdf1"
file2 = "path/to/pdf2"
result = dif(file1, file2)
item = serve_pil_image(result)
return item
#trying to make user input work
@app.route("/upload", methods=["GET","POST"])
def uploading():
item = ''
if request.method =="POST":
file1 = request.form['file1']
file2 = request.form['file2']
if request.form['differences']:
result = dif(file1, file2)
item = serve_pil_image(result)
return item
return render_template('upload.html')
upload.html
<html>
<head>
<title>Simple file upload using Python Flask</title>
</head>
<body>
<form action="." method="post" enctype="multipart/form-data">
Choose the first file: <input type="file" name="file1" value="file one"/><BR>
Choose the first file: <input type="file" name="file2" value="file two"/><BR>
<input type="button" name="differences" value="Find Differences">
<input type="submit" name="similarity" value="Similarity Score (Percentage)">
</form>
</body>
</html>
document_differences.py
import difflib
import io
from tika import parser
import imgkit
import docxpy
from PIL import Image
#function which finds lexical differences between two documents after converting them into text files
def dif(filea, fileb):
if filea.endswith('.pdf'):
texta = pdf_to_text(filea)
if fileb.endswith('.pdf'):
textb = pdf_to_text(fileb)
difference = difflib.HtmlDiff().make_file(texta, textb)
difference_report = open('diff_table.html', 'w')
difference_report.write(difference)
difference_report.close()
image = imgkit.from_file('diff_table.html', False)
im = Image.open(io.BytesIO(image))
return im
Ничего не происходит, когда я нажимаю кнопку Find Differences , но я почти уверен, что это потому, что мой HTML-файл неверен. Будем весьма благодарны за любые рекомендации о том, как правильно отформатировать его, чтобы он правильно совпадал со скриптом Python:)