Im a new coder and i dont know why even though there are no errors, nothing is appearing on my web browser! I am currently trying to create a program which can iterate through different pdf files and find keywords
this is all my code
search.py
<code>import fitz # PyMuPDF
def __init__(self, upload_folder):
self.upload_folder = upload_folder
self.allowed_extensions = {'pdf'}
def allowed_file(self, filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in self.allowed_extensions
def search_keyword_in_pdf(self, file_path, keyword):
doc = fitz.open(file_path)
for page_num in range(len(doc)):
page = doc.load_page(page_num)
if keyword.lower() in text.lower():
results.append((page_num + 1, text))
def save_file(self, file):
filename = os.path.join(self.upload_folder, file.filename)
<code>import fitz # PyMuPDF
import os
class PDFSearcher:
def __init__(self, upload_folder):
self.upload_folder = upload_folder
self.allowed_extensions = {'pdf'}
def allowed_file(self, filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in self.allowed_extensions
def search_keyword_in_pdf(self, file_path, keyword):
doc = fitz.open(file_path)
results = []
for page_num in range(len(doc)):
page = doc.load_page(page_num)
text = page.get_text()
if keyword.lower() in text.lower():
results.append((page_num + 1, text))
return results
def save_file(self, file):
filename = os.path.join(self.upload_folder, file.filename)
file.save(filename)
return filename
</code>
import fitz # PyMuPDF
import os
class PDFSearcher:
def __init__(self, upload_folder):
self.upload_folder = upload_folder
self.allowed_extensions = {'pdf'}
def allowed_file(self, filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in self.allowed_extensions
def search_keyword_in_pdf(self, file_path, keyword):
doc = fitz.open(file_path)
results = []
for page_num in range(len(doc)):
page = doc.load_page(page_num)
text = page.get_text()
if keyword.lower() in text.lower():
results.append((page_num + 1, text))
return results
def save_file(self, file):
filename = os.path.join(self.upload_folder, file.filename)
file.save(filename)
return filename
index.html
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF Search</title>
<h1>Search in PDF Files</h1>
<form method="POST" enctype="multipart/form-data">
<label for="keyword">Keyword:</label>
<input type="text" id="keyword" name="keyword" required>
<label for="files">Select PDF files:</label>
<input type="file" id="files" name="files" multiple required>
<input type="submit" value="Search">
<h2>Search Results for "{{ keyword }}"</h2>
{% for filename, results in search_results %}
{% for page_num, text in results %}
<h4>Page {{ page_num }}</h4>
<code><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF Search</title>
</head>
<body>
<h1>Search in PDF Files</h1>
<form method="POST" enctype="multipart/form-data">
<label for="keyword">Keyword:</label>
<input type="text" id="keyword" name="keyword" required>
<br><br>
<label for="files">Select PDF files:</label>
<input type="file" id="files" name="files" multiple required>
<br><br>
<input type="submit" value="Search">
</form>
{% if keyword %}
<h2>Search Results for "{{ keyword }}"</h2>
{% for filename, results in search_results %}
<h3>{{ filename }}</h3>
{% for page_num, text in results %}
<h4>Page {{ page_num }}</h4>
<p>{{ text }}</p>
{% endfor %}
{% endfor %}
{% endif %}
</body>
</html>
</code>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF Search</title>
</head>
<body>
<h1>Search in PDF Files</h1>
<form method="POST" enctype="multipart/form-data">
<label for="keyword">Keyword:</label>
<input type="text" id="keyword" name="keyword" required>
<br><br>
<label for="files">Select PDF files:</label>
<input type="file" id="files" name="files" multiple required>
<br><br>
<input type="submit" value="Search">
</form>
{% if keyword %}
<h2>Search Results for "{{ keyword }}"</h2>
{% for filename, results in search_results %}
<h3>{{ filename }}</h3>
{% for page_num, text in results %}
<h4>Page {{ page_num }}</h4>
<p>{{ text }}</p>
{% endfor %}
{% endfor %}
{% endif %}
</body>
</html>
app.py
<code>from flask import Flask, request, render_template
from pdf_search.search import PDFSearcher
app.config['UPLOAD_FOLDER'] = 'uploads/'
pdf_searcher = PDFSearcher(app.config['UPLOAD_FOLDER'])
@app.route('/', methods=['GET', 'POST'])
if request.method == 'POST':
keyword = request.form['keyword']
files = request.files.getlist('files')
if file and pdf_searcher.allowed_file(file.filename):
filename = pdf_searcher.save_file(file)
results = pdf_searcher.search_keyword_in_pdf(filename, keyword)
search_results.append((file.filename, results))
return render_template('index.html', keyword=keyword, search_results=search_results)
return render_template('index.html')
if __name__ == '__main__':
<code>from flask import Flask, request, render_template
import datetime
from pdf_search.search import PDFSearcher
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads/'
pdf_searcher = PDFSearcher(app.config['UPLOAD_FOLDER'])
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
keyword = request.form['keyword']
files = request.files.getlist('files')
search_results = []
for file in files:
if file and pdf_searcher.allowed_file(file.filename):
filename = pdf_searcher.save_file(file)
results = pdf_searcher.search_keyword_in_pdf(filename, keyword)
if results:
search_results.append((file.filename, results))
return render_template('index.html', keyword=keyword, search_results=search_results)
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
</code>
from flask import Flask, request, render_template
import datetime
from pdf_search.search import PDFSearcher
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads/'
pdf_searcher = PDFSearcher(app.config['UPLOAD_FOLDER'])
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
keyword = request.form['keyword']
files = request.files.getlist('files')
search_results = []
for file in files:
if file and pdf_searcher.allowed_file(file.filename):
filename = pdf_searcher.save_file(file)
results = pdf_searcher.search_keyword_in_pdf(filename, keyword)
if results:
search_results.append((file.filename, results))
return render_template('index.html', keyword=keyword, search_results=search_results)
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
like
This is what my workspace looks like
and the only thing that appears when the program runs is a blank screen. Ive asked all the AIs and nothing can fix it!!please help me
what was expected to happen was that it would display the html