-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
61 lines (49 loc) · 1.65 KB
/
Copy pathapp.py
File metadata and controls
61 lines (49 loc) · 1.65 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
from flask import Flask, render_template, request, jsonify
import json
import os
app = Flask(__name__)
DATA_FILE = "data/apps.json"
ICONS_DIR = "static/icons/"
# Vérifier si le fichier JSON et le dossier d'icônes existent
if not os.path.exists("data"):
os.makedirs("data")
if not os.path.exists(ICONS_DIR):
os.makedirs(ICONS_DIR)
if not os.path.exists(DATA_FILE):
with open(DATA_FILE, "w") as f:
json.dump([], f)
else:
with open(DATA_FILE, "r") as f:
try:
json.load(f) # Vérifier si le fichier JSON est valide
except json.JSONDecodeError:
with open(DATA_FILE, "w") as f:
json.dump([], f) # Réinitialiser si corrompu
# Route principale
@app.route('/')
def home():
with open(DATA_FILE, "r") as f:
apps = json.load(f)
return render_template('index.html', apps=apps)
# Ajouter une application
@app.route('/add_app', methods=['POST'])
def add_app():
name = request.form.get('name')
url = request.form.get('url')
icon = request.files.get('icon')
if not name or not url:
return jsonify({"error": "Nom et URL requis"}), 400
icon_path = "static/icons/default.png"
if icon:
icon_filename = f"{ICONS_DIR}{icon.filename}"
icon.save(icon_filename)
icon_path = icon_filename
new_app = {"name": name, "url": url, "icon": icon_path}
with open(DATA_FILE, "r+") as f:
apps = json.load(f)
apps.append(new_app)
f.seek(0)
json.dump(apps, f, indent=4)
return jsonify({"message": "Application ajoutée avec succès"}), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=3000, debug=True)