Utllisation d'un serveur flask

This commit is contained in:
Alex 2024-07-25 14:19:08 +02:00
parent e897dc5ec3
commit 522bc216f5
5 changed files with 112 additions and 3 deletions

57
app.py Normal file
View File

@ -0,0 +1,57 @@
from flask import Flask, render_template, request, redirect, url_for, jsonify, flash
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
import json
from main import my_function, load_config
app = Flask(__name__)
app.secret_key = 'your_secret_key'
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
# Utilisateurs fictifs pour l'exemple
users = {'admin': {'password': 'password'}}
class User(UserMixin):
def __init__(self, username):
self.id = username
@login_manager.user_loader
def load_user(user_id):
return User(user_id)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if username in users and users[username]['password'] == password:
user = User(username)
login_user(user)
return redirect(url_for('index'))
else:
flash('Invalid username or password')
return render_template('login.html')
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
@app.route('/')
@login_required
def index():
return render_template('index.html')
@app.route('/run', methods=['POST'])
@login_required
def run():
filigramme = request.form['filigramme']
config = load_config()
result = my_function({}, filigramme)
return jsonify(result)
if __name__ == '__main__':
app.run(debug=True)

14
main.py
View File

@ -178,7 +178,8 @@ def zip_directory(directory, zip_file):
file_path = os.path.join(root, file)
zipf.write(file_path, os.path.relpath(file_path, directory))
if __name__ == '__main__':
def main():
# Creation du répertoire sources pour mettre les documents
create_original_files_directory()
@ -223,6 +224,13 @@ if __name__ == '__main__':
zip_directory(destination_directory, fichierZip)
# Ouverture du répertoire contenant toutes les images tagées
open_in_file_explorer(config["zipfileDirectory"])
# open_in_file_explorer(config["zipfileDirectory"])
return {"result": "processed data with filigramme: " + filigramme}
except:
print(f"Aucune image à taguer")
print(f"Aucune image à taguer")
return {"Aucune image à taguer"}
if __name__ == '__main__':
main()

View File

@ -1,3 +1,5 @@
Pillow
piexif
pdf2image
flask
flask-login

16
templates/index.html Normal file
View File

@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Run Script</title>
</head>
<body>
<h2>Enter Filigramme</h2>
<form method="POST" action="{{ url_for('run') }}">
<label for="filigramme">Filigramme:</label>
<input type="text" id="filigramme" name="filigramme"><br><br>
<input type="submit" value="Run">
</form>
<a href="{{ url_for('logout') }}">Logout</a>
</body>
</html>

26
templates/login.html Normal file
View File

@ -0,0 +1,26 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form method="POST" action="{{ url_for('login') }}">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
</body>
</html>