Marthee commited on
Commit
4dbe79f
·
verified ·
1 Parent(s): 881451a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -16
app.py CHANGED
@@ -1,40 +1,42 @@
1
- from flask import Flask, send_file, render_template, request, session, jsonify
2
  import requests
3
  from io import BytesIO
4
  import fitz # PyMuPDF
5
 
6
- # Flask App
 
 
 
7
  app = Flask(__name__)
8
- app.secret_key = 'your_secret_key' # Flask session requires a secret key
9
 
10
  @app.route("/", methods=["GET", "POST"])
11
  def getInfotoMeasure():
12
- pdf_link = [
13
- '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'
14
- ]
15
  keyword = "To be read with preliminaries/ general conditions"
16
 
17
  # Call the function to process the PDF
18
- session['pdf_content'], session['pageNumTextFound'] = highlight_text_from_pdf(pdf_link, keyword)
19
 
20
  # Render the GUI with the current page number
21
- return render_template("gui.html", page=session.get('pageNumTextFound', 0))
22
-
23
 
24
  @app.route('/view-pdf', methods=['GET'])
25
  def download_pdf():
26
- if 'pdf_content' not in session or session['pdf_content'] is None:
 
 
27
  return "PDF content not found.", 404
28
 
29
- pdf_bytes = BytesIO(session['pdf_content'])
30
  return send_file(
31
  pdf_bytes,
32
  mimetype='application/pdf',
33
  as_attachment=False,
34
- download_name=f"highlighted_page_{session.get('pageNumTextFound', 0)}.pdf"
35
  )
36
 
37
-
38
  def highlight_text_from_pdf(pdfshareablelinks, keyword):
39
  print('PDF Links:', pdfshareablelinks)
40
 
@@ -63,16 +65,15 @@ def highlight_text_from_pdf(pdfshareablelinks, keyword):
63
  if matched:
64
  for word in matched:
65
  page.add_highlight_annot(word)
66
-
67
  pageNumTextFound = page_num + 1
68
 
 
69
  pdf_bytes = BytesIO()
70
  pdf_document.save(pdf_bytes)
71
  pdf_document.close()
72
 
73
- # Save PDF content and page number to Flask session
74
  return pdf_bytes.getvalue(), pageNumTextFound
75
 
76
-
77
  if __name__ == '__main__':
78
  app.run(host='0.0.0.0', port=7860)
 
1
+ from flask import Flask, send_file, render_template, request
2
  import requests
3
  from io import BytesIO
4
  import fitz # PyMuPDF
5
 
6
+ # Define local variables to retain the PDF content across function calls
7
+ pdf_content = None
8
+ pageNumTextFound = 0
9
+
10
  app = Flask(__name__)
 
11
 
12
  @app.route("/", methods=["GET", "POST"])
13
  def getInfotoMeasure():
14
+ global pdf_content, pageNumTextFound
15
+
16
+ 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']
17
  keyword = "To be read with preliminaries/ general conditions"
18
 
19
  # Call the function to process the PDF
20
+ pdf_content, pageNumTextFound = highlight_text_from_pdf(pdf_link, keyword)
21
 
22
  # Render the GUI with the current page number
23
+ return render_template("gui.html", page=pageNumTextFound)
 
24
 
25
  @app.route('/view-pdf', methods=['GET'])
26
  def download_pdf():
27
+ global pdf_content, pageNumTextFound
28
+
29
+ if pdf_content is None:
30
  return "PDF content not found.", 404
31
 
32
+ pdf_bytes = BytesIO(pdf_content)
33
  return send_file(
34
  pdf_bytes,
35
  mimetype='application/pdf',
36
  as_attachment=False,
37
+ download_name=f"highlighted_page_{pageNumTextFound}.pdf"
38
  )
39
 
 
40
  def highlight_text_from_pdf(pdfshareablelinks, keyword):
41
  print('PDF Links:', pdfshareablelinks)
42
 
 
65
  if matched:
66
  for word in matched:
67
  page.add_highlight_annot(word)
68
+
69
  pageNumTextFound = page_num + 1
70
 
71
+ # Save PDF content to memory and return it along with the page number
72
  pdf_bytes = BytesIO()
73
  pdf_document.save(pdf_bytes)
74
  pdf_document.close()
75
 
 
76
  return pdf_bytes.getvalue(), pageNumTextFound
77
 
 
78
  if __name__ == '__main__':
79
  app.run(host='0.0.0.0', port=7860)