Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
149 lines (115 loc) · 4.9 KB
/
Copy pathapp.py
File metadata and controls
149 lines (115 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# import required libraries
import os
import sqlite3
from flask import (Flask, make_response, redirect, render_template, request,
send_from_directory)
from flask_babel import Babel, _, g, get_locale, gettext, lazy_gettext
from pdf2image import convert_from_path
# create a new Flask web app
app = Flask(__name__)
# initialize Flask-Babel
babel = Babel(app)
# set the folder to store uploaded PDF files
UPLOAD_FOLDER = 'static/uploads'
# set the folder to store generated images
OUTPUT_FOLDER = 'static/outputs'
# configure the app to store uploaded files in the specified folder
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# configure the app to store generated images in the specified folder
app.config['OUTPUT_FOLDER'] = OUTPUT_FOLDER
# configure the translations folder
app.config['BABEL_TRANSLATION_DIRECTORIES'] = 'translations'
# configure the languages that the app will support
app.config['LANGUAGES'] = ['en', 'ar']
# ensure the folders exist
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
# inject the locale into the template context
@app.context_processor
def inject_locale():
print(f"=========[{get_locale()}]=========")
return {'locale': get_locale()}
# get the locale from the browser's preferred language, or from a cookie
def get_locale():
lang = request.cookies.get('lang')
print(f"Current cookie language: {lang}")
if lang in app.config['LANGUAGES']:
return lang
return 'en' # Explicit default
# Initialize Flask-Babel
babel.init_app(app, locale_selector=get_locale)
# Define a route to set the language
@app.route('/set_language/<lang>')
def set_language(lang):
resp = make_response(redirect(request.referrer or '/'))
resp.set_cookie('lang', lang, max_age=60*60*24 *
30) # Cookie expires in 30 days
print(f"Setting language cookie to: {lang}") # Debug print
return resp
# Define the main route for the app
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
# Get the list of uploaded PDF files
files = request.files.getlist('pdf_files')
image_paths = [] # Store all generated image paths
for file in files:
if file:
# Save each PDF file
pdf_path = os.path.join(
app.config['UPLOAD_FOLDER'], file.filename)
file.save(pdf_path)
# Convert each PDF to images
images = convert_from_path(pdf_path)
for i, image in enumerate(images):
image_filename = f"{os.path.splitext(file.filename)[0]}_page_{
i + 1}.jpg"
image_path = os.path.join(
app.config['OUTPUT_FOLDER'], image_filename)
image.save(image_path, 'JPEG')
image_paths.append(f"outputs/{image_filename}")
return render_template('result.html', image_paths=image_paths)
return render_template('home.html')
# Define a route to handle the contact form
@app.route('/contact', methods=['GET', 'POST'])
def contact():
return render_template('contact.html')
# Define a route to handle the about page
@app.route('/about', methods=['GET', 'POST'])
def about():
return render_template('about.html')
# Define a route to serve the generated images
@app.route('/static/outputs/<filename>')
def serve_image(filename):
return send_from_directory(app.config['OUTPUT_FOLDER'], filename, as_attachment=True)
# Define a route to handle the contact form submissions
@app.route('/submit', methods=['POST'])
def proccess_contacts():
if request.method == 'POST':
contact_name = request.form['contact_name']
contact_email = request.form['contact_email']
contact_message = request.form['contact_message']
print(f"Name: {contact_name}")
print(f"Email: {contact_email}")
print(f"Message: {contact_message}")
try:
conn = sqlite3.connect('database.db')
cr = conn.cursor()
cr.execute(
"CREATE TABLE IF NOT EXISTS contacts (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, email TEXT, message TEXT,date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, UNIQUE(email))")
cr.execute("SELECT * FROM contacts WHERE email = ?",
(contact_email,))
existing_contact = cr.fetchone()
if existing_contact:
return render_template('dublicate.html')
else:
cr.execute("INSERT INTO contacts (name, email, message) VALUES (?, ?, ?)",
(contact_name, contact_email, contact_message))
conn.commit()
conn.close()
except conn.IntegrityError:
print("you already sent a message !")
print("Button clicked")
return render_template('submit.html')
if __name__ == '__main__':
app.run(debug=False)