Marthee commited on
Commit
46886f1
·
verified ·
1 Parent(s): 1abd7ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -11
app.py CHANGED
@@ -5,12 +5,18 @@ import fitz # PyMuPDF
5
 
6
  app = Flask(__name__)
7
 
8
- # Route to render the main page
9
  @app.route("/", methods=["GET", "POST"])
10
- def getInfotoMeasure(pagenum):
11
- return render_template("gui.html", page=pagenum)
 
 
 
 
 
 
12
 
13
- # Function to highlight text in PDF
14
  def highlight_text_from_pdf(pdfshareablelinks, keyword):
15
  print('PDF Links:', pdfshareablelinks)
16
  for link in pdfshareablelinks:
@@ -21,11 +27,11 @@ def highlight_text_from_pdf(pdfshareablelinks, keyword):
21
  if 'dl=0' in link:
22
  link = link.replace('dl=0', 'dl=1')
23
 
24
- # Download the PDF content from the shareable link
25
  response = requests.get(link)
26
  if response.status_code == 200:
27
  pdf_content = BytesIO(response.content)
28
- print('Downloaded from shareable link.')
29
 
30
  if pdf_content is None:
31
  raise ValueError("No valid PDF content found.")
@@ -40,11 +46,11 @@ def highlight_text_from_pdf(pdfshareablelinks, keyword):
40
  if matched:
41
  for word in matched:
42
  page.add_highlight_annot(word)
43
- pageNumTextFound = page_num + 1 # 1-indexed pages
44
 
45
  return pdf_document, pageNumTextFound
46
 
47
- # Route to serve PDF with a specified page
48
  @app.route('/view-pdf', methods=['GET'])
49
  def download_pdf():
50
  pdf_link = [
@@ -55,9 +61,7 @@ def download_pdf():
55
  # Call function to highlight text and get PDF page
56
  pdf_content, pageNumTextFound = highlight_text_from_pdf(pdf_link, keyword)
57
 
58
- # Render the main GUI with the detected page number
59
- getInfotoMeasure(pageNumTextFound)
60
-
61
  pdf_bytes = BytesIO()
62
  pdf_content.save(pdf_bytes)
63
  pdf_bytes.seek(0)
 
5
 
6
  app = Flask(__name__)
7
 
8
+ # Route to render the main page with page number
9
  @app.route("/", methods=["GET", "POST"])
10
+ def getInfotoMeasure():
11
+ # Call the function to highlight text and get the page number
12
+ pdf_link = ['https://www.dropbox.com/scl/fi/fjykwhhn9gu9t3kqrflxd/LA002-NOR-ZZ-ZZ-T-A-2403_Architectural-Specification-F10-Brick-and-Block-Walling_A4-_C01.pdf?rlkey=ek9i66i79m0hwp8z5yjs6rp5p&st=jh05a6qs&dl=0']
13
+ keyword = "To be read with preliminaries/ general conditions"
14
+ pdf_content, pageNumTextFound = highlight_text_from_pdf(pdf_link, keyword)
15
+
16
+ # Pass pageNumTextFound to the template for rendering
17
+ return render_template("gui.html", page=pageNumTextFound)
18
 
19
+ # Function to highlight text in PDF and return the page number
20
  def highlight_text_from_pdf(pdfshareablelinks, keyword):
21
  print('PDF Links:', pdfshareablelinks)
22
  for link in pdfshareablelinks:
 
27
  if 'dl=0' in link:
28
  link = link.replace('dl=0', 'dl=1')
29
 
30
+ # Download PDF content from the link
31
  response = requests.get(link)
32
  if response.status_code == 200:
33
  pdf_content = BytesIO(response.content)
34
+ print('Downloaded PDF content.')
35
 
36
  if pdf_content is None:
37
  raise ValueError("No valid PDF content found.")
 
46
  if matched:
47
  for word in matched:
48
  page.add_highlight_annot(word)
49
+ pageNumTextFound = page_num + 1 # 1-indexed
50
 
51
  return pdf_document, pageNumTextFound
52
 
53
+ # Flask route to serve the highlighted PDF
54
  @app.route('/view-pdf', methods=['GET'])
55
  def download_pdf():
56
  pdf_link = [
 
61
  # Call function to highlight text and get PDF page
62
  pdf_content, pageNumTextFound = highlight_text_from_pdf(pdf_link, keyword)
63
 
64
+ # Create a BytesIO object from the modified PDF content
 
 
65
  pdf_bytes = BytesIO()
66
  pdf_content.save(pdf_bytes)
67
  pdf_bytes.seek(0)