From 522bc216f5fd8d98984706b1e2a0e0dd36f31c3b Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 25 Jul 2024 14:19:08 +0200 Subject: [PATCH] Utllisation d'un serveur flask --- app.py | 57 ++++++++++++++++++++++++++++++++++++++++++++ main.py | 14 ++++++++--- requirements.txt | 2 ++ templates/index.html | 16 +++++++++++++ templates/login.html | 26 ++++++++++++++++++++ 5 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 app.py create mode 100644 templates/index.html create mode 100644 templates/login.html diff --git a/app.py b/app.py new file mode 100644 index 0000000..6e3256c --- /dev/null +++ b/app.py @@ -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) diff --git a/main.py b/main.py index 0f2963b..d70b57a 100644 --- a/main.py +++ b/main.py @@ -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") \ No newline at end of file + print(f"Aucune image à taguer") + return {"Aucune image à taguer"} + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index c11816a..ae7172a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ Pillow piexif pdf2image +flask +flask-login \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..e2f0a89 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,16 @@ + + + + + Run Script + + +

Enter Filigramme

+
+ +

+ +
+ Logout + + diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..d15fd13 --- /dev/null +++ b/templates/login.html @@ -0,0 +1,26 @@ + + + + + Login + + +

Login

+
+ +

+ +

+ +
+ {% with messages = get_flashed_messages() %} + {% if messages %} + + {% endif %} + {% endwith %} + +