🌐🥾🎒 Analyse des traces GPX issus d'un GPS (Application python web flask+uwsgi)
Web - Analyse des traces GPX
Procédure complète Debian avec Flask + uWSGI + nginx + systemd. Le schéma suit le déploiement classique où systemd lance uWSGI, nginx sert les fichiers statiques et reverse-proxy les requêtes dynamiques vers le socket UNIX.
Déploiement sur machine 🖥⚙️CWWK - Serveur Debian 13
Installer paquets système
Exécutez ces commandes sur le serveur CWWK
1
2
sudo apt update
sudo apt install -y nginx python3 python3-venv python3-dev build-essential
Projet Python flask + uwsgi
Dossier /sharenfs/rnmkcy/web-traces-gpx
Créer le dossier projet et le venv :
1
2
3
4
5
6
7
8
9
10
mkdir -p /sharenfs/rnmkcy/web-traces-gpx
mkdir -p /sharenfs/rnmkcy/web-traces-gpx/templates
chown -R $USER:$USER /sharenfs/rnmkcy/web-traces-gpx
cd /sharenfs/rnmkcy/web-traces-gpx
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip wheel
pip install flask uwsgi
# modules pour importation et traitement gpx
pip install gpxpy geopy
L’installation de Python/venv puis Flask/uWSGI dans un environnement isolé correspond aux pratiques de déploiement habituelles
Les fichiers
- app.py — l’application Flask (lecture config.json, endpoints /, /api/traces, /api/copy-gpx, option /gpx/ pour dev).
- config.json — votre fichier adapté (chemins inchangés).
- templates/index.html — UI utilisant DataTables (tri, recherche, sélection) et bouton “Copier GPX sélection” (crée l’appel vers /api/copy-gpx).
- static/ — fichiers JS/CSS nécessaires (je fournis des wrappers et instructions pour utiliser les fichiers locaux ou CDN).
- README.md — instructions de déploiement (venv, uwsgi, nginx) et notes permissions.
- Makefile (optionnel) pour créer l’environnement et lancer en dev.
Création app.py
/sharenfs/rnmkcy/web-traces-gpx/app.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import json
import os
import shutil
import threading
import uuid
from pathlib import Path
from flask import Flask, jsonify, request, send_from_directory, render_template, abort
from gpx_importer import process_gpx_folder
import io
import zipfile
from flask import Response
BASE_DIR = Path(__file__).parent
CFG_PATH = BASE_DIR / "config.json"
def load_config(path=CFG_PATH):
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
cfg = load_config()
JSON_PATH = Path(cfg["json_path"])
JS_PATH = Path(cfg["js_path"])
GPX_DIR = Path(cfg["gpx_dir"])
TEMP_DIR = Path(cfg.get("temp_dir", "/tmp/gpx-studio-drop"))
app = Flask(
__name__,
static_folder=str(BASE_DIR / "static"),
template_folder=str(BASE_DIR / "templates"),
)
import_lock = threading.Lock()
tasks = {}
@app.route("/")
def index():
return render_template("index.html", title="Table JSON - tri + recherche")
@app.route("/api/traces")
def api_traces():
try:
with open(JSON_PATH, "r", encoding="utf-8") as f:
data = json.load(f)
except Exception as e:
return jsonify({"error": str(e)}), 500
return jsonify(data)
@app.route("/api/config/visible-columns", methods=["GET", "POST"])
def api_visible_columns():
global cfg
if request.method == "GET":
return jsonify({"visible_columns": cfg.get("visible_columns", [])})
payload = request.get_json() or {}
visible = payload.get("visible_columns", [])
if not isinstance(visible, list) or not all(isinstance(x, str) for x in visible):
return jsonify({"error": "invalid payload"}), 400
allowed = ["id", "latitude", "longitude", "lieu", "commune", "cp", "distance", "vitesse", "niveau", "jour", "gpx"]
visible = [x for x in visible if x in allowed]
cfg["visible_columns"] = visible
try:
with open(CFG_PATH, "w", encoding="utf-8") as f:
json.dump(cfg, f, ensure_ascii=False, indent=2)
except Exception as e:
return jsonify({"error": f"could not write config: {e}"}), 500
return jsonify({"visible_columns": visible})
@app.route("/api/copy-gpx", methods=["POST"])
def api_copy_gpx():
payload = request.get_json() or {}
names = payload.get("names", [])
if not isinstance(names, list):
return jsonify({"error": "invalid payload"}), 400
os.makedirs(TEMP_DIR, exist_ok=True)
copied = []
missing = []
for name in names:
src = GPX_DIR / name
if src.exists() and src.is_file():
dst = TEMP_DIR / src.name
try:
shutil.copy2(src, dst)
copied.append(str(dst))
except Exception as e:
app.logger.error("copy error %s -> %s : %s", src, dst, e)
else:
missing.append(name)
return jsonify({"copied": copied, "missing": missing})
@app.route("/api/import-gpx", methods=["POST"])
def api_import_gpx():
if "files" not in request.files:
return jsonify({"ok": False, "error": "Aucun fichier reçu (champ 'files')."}), 400
files = request.files.getlist("files")
files = [f for f in files if f and f.filename]
if not files:
return jsonify({"ok": False, "error": "Sélection vide."}), 400
job_id = str(uuid.uuid4())
job_folder = TEMP_DIR / f"import_{job_id}"
job_folder.mkdir(parents=True, exist_ok=True)
saved_names = []
for f in files:
name = os.path.basename(f.filename)
if not name.lower().endswith(".gpx"):
continue
target = job_folder / name
f.save(str(target))
saved_names.append(name)
if not saved_names:
shutil.rmtree(job_folder, ignore_errors=True)
return jsonify({"ok": False, "error": "Aucun fichier GPX (*.gpx) valide."}), 400
task_id = job_id
tasks[task_id] = {
"state": "running",
"treated": 0,
"errors": [],
"files": saved_names,
}
def worker():
with import_lock:
try:
result = process_gpx_folder(
input_folder=str(job_folder),
json_path=str(JSON_PATH),
js_path=str(JS_PATH),
temp_dir=str(TEMP_DIR),
gpx_dir=str(GPX_DIR),
pause_s=2.0,
pattern="*.gpx",
)
tasks[task_id]["state"] = "done"
tasks[task_id]["treated"] = result.get("treated", 0)
tasks[task_id]["errors"] = result.get("errors", [])
except Exception as e:
tasks[task_id]["state"] = "error"
tasks[task_id]["errors"] = [str(e)]
finally:
shutil.rmtree(job_folder, ignore_errors=True)
threading.Thread(target=worker, daemon=True).start()
return jsonify({"ok": True, "task_id": task_id}), 202
@app.route("/api/import-gpx-status/<task_id>", methods=["GET"])
def api_import_gpx_status(task_id):
st = tasks.get(task_id)
if not st:
return jsonify({"ok": False, "error": "task introuvable"}), 404
return jsonify({"ok": True, "task": st})
@app.route("/gpx/<path:filename>")
def gpx_serve(filename):
if not (GPX_DIR / filename).exists():
abort(404)
return send_from_directory(str(GPX_DIR), filename, as_attachment=False)
@app.route("/api/download-gpx-zip", methods=["POST"])
def api_download_gpx_zip():
payload = request.get_json() or {}
names = payload.get("names", [])
if not isinstance(names, list) or not all(isinstance(x, str) for x in names):
return jsonify({"error": "names doit être une liste de strings."}), 400
# Filtrage + nettoyage
safe_names = []
for n in names:
n = os.path.basename(n)
if n.lower().endswith(".gpx"):
safe_names.append(n)
if not safe_names:
return jsonify({"error": "Aucun fichier GPX valide."}), 400
mem = io.BytesIO()
with zipfile.ZipFile(mem, mode="w", compression=zipfile.ZIP_DEFLATED) as zf:
missing = []
for name in safe_names:
src = GPX_DIR / name
if src.exists() and src.is_file():
zf.write(str(src), arcname=name)
else:
missing.append(name)
mem.seek(0)
# (Optionnel) tu peux gérer missing côté front si tu veux
headers = {
"Content-Disposition": 'attachment; filename="gpx_download.zip"'
}
return Response(mem.getvalue(), mimetype="application/zip", headers=headers)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)
Création gpx_importer.py
Crée un fichier gpx_importer.py (dans le même dossier que app.py )
/sharenfs/rnmkcy/web-traces-gpx/gpx_importer.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import datetime
import fnmatch
import json
import logging as mod_logging
import os
import re
import shutil
import sys
import time
import gpxpy as mod_gpxpy
import geopy.geocoders
from geopy.geocoders import Nominatim
_geo_cache = {}
def format_speed_num(speed):
if not speed:
speed = 0
return "{:.2f}".format(speed * 3600.0 / 1000.0)
def format_short_length_num(length):
return "{:.2f}".format(length / 1000.0)
def strip_trailing_closer(path, closers=("]", "];")):
with open(path, "rb+") as fh:
fh.seek(0, os.SEEK_END)
pos = fh.tell()
if pos == 0:
return
while pos > 0:
pos -= 1
fh.seek(pos)
c = fh.read(1)
if c not in b" \t\r\n":
break
for closer in closers:
bcloser = closer.encode("utf-8")
start = pos - len(bcloser) + 1
if start >= 0:
fh.seek(start)
if fh.read(len(bcloser)) == bcloser:
fh.seek(start)
fh.truncate()
return
def _load_geo_cache(path=None):
global _geo_cache
if path and os.path.exists(path):
try:
with open(path, "r", encoding="utf-8") as fh:
_geo_cache = json.load(fh)
except Exception:
_geo_cache = {}
def _save_geo_cache(path=None):
global _geo_cache
if path:
try:
with open(path, "w", encoding="utf-8") as fh:
json.dump(_geo_cache, fh, ensure_ascii=False, indent=2)
except Exception:
pass
def _esc(s):
return str(s).replace('"', '\\"')
def get_last_id(json_path):
if not os.path.exists(json_path):
return 0
try:
txt = open(json_path, "r", encoding="utf-8").read()
ids = [int(x) for x in re.findall(r'"id"\s*:\s*(\d+)', txt)]
return (ids[-1] + 1) if ids else 0
except Exception:
return 0
def load_existing_gpx_names(json_path):
if not os.path.exists(json_path):
return set()
try:
txt = open(json_path, "r", encoding="utf-8").read()
return set(re.findall(r'"gpx"\s*:\s*"([^"]+)"', txt))
except Exception:
return set()
def print_gpx_part_info(gpx_part, jsTraces, dataTraces, fichier, dataIndex, json_first):
ok = True
try:
points = gpx_part.tracks[0].segments[0].points
if not points:
raise ValueError("No points in gpx part")
lati = points[0].latitude
longi = points[0].longitude
except Exception as e:
print(f"ERROR: cannot read points for {fichier}: {e}")
sys.stdout.flush()
return json_first, False
cache_key = f"{round(lati, 6)},{round(longi, 6)}"
address_parts = []
if cache_key in _geo_cache:
address = _geo_cache[cache_key]
if address:
address_parts = address.split(",")
else:
print(f"--> geocode reverse for fichier={fichier} lat={lati} lon={longi}")
sys.stdout.flush()
geopy.geocoders.options.default_user_agent = "gxinforep-tracesgpx"
geopy.geocoders.options.default_timeout = 30
geolocator = Nominatim()
location = None
geo_error = None
for attempt in range(1, 4):
try:
location = geolocator.reverse(f"{lati},{longi}")
geo_error = None
break
except Exception as e:
geo_error = str(e)
print(f"WARNING: geocode attempt {attempt} failed for {fichier}: {e}")
sys.stdout.flush()
if attempt < 3:
wait = 2 * (2 ** (attempt - 1))
time.sleep(wait)
if location and getattr(location, "address", None):
address = location.address
address_parts = address.split(",")
_geo_cache[cache_key] = address
else:
_geo_cache[cache_key] = ""
ok = False
print(f"ERROR: geocode failed for {fichier}: {geo_error}")
z = len(address_parts)
def part(idx, default=""):
try:
return address_parts[idx].strip()
except Exception:
return default
lieu = part(z - 8)
commune = part(z - 7)
cposte = part(z - 2)
try:
length_2d = gpx_part.length_2d() or 0.0
except Exception:
length_2d = 0.0
try:
moving_time, stopped_time, moving_distance, stopped_distance, max_speed = gpx_part.get_moving_data()
except Exception:
moving_time = stopped_time = moving_distance = stopped_distance = max_speed = 0.0
try:
uphill, downhill = gpx_part.get_uphill_downhill()
except Exception:
uphill = downhill = 0.0
try:
start_time, end_time = gpx_part.get_time_bounds()
except Exception:
start_time = end_time = None
vmoy_num = format_speed_num(moving_distance / moving_time) if moving_time and moving_time > 0 else "0.00"
niveau_str = f"{uphill:.2f}"
distance_str = "{:.3f}".format(length_2d / 1000.0)
jour_str = start_time.strftime("%Y-%m-%d") if start_time else "n/a"
json_entry = (
"{\n"
f' "id": {dataIndex},'
f' "latitude": {lati},'
f' "longitude": {longi},'
f' "lieu": "{_esc(lieu)}",'
f' "commune": "{_esc(commune)}",'
f' "cp": "{_esc(cposte)}",'
f' "distance": "{distance_str}",'
f' "vitesse": "{vmoy_num}",'
f' "niveau": "{niveau_str}",'
f' "jour": "{_esc(jour_str)}",'
f' "gpx": "{_esc(fichier)}"\n'
"}"
)
if json_first:
jsTraces.write(json_entry)
json_first = False
else:
jsTraces.write(",\n" + json_entry)
js_entry = (
"[" + str(lati) + "," + str(longi) + ',"' + _esc(lieu) + '","' + _esc(commune) +
'","' + _esc(cposte) + '","' + distance_str + '","' + vmoy_num + '","' + niveau_str +
'","' + _esc(jour_str) + '","' + _esc(fichier) + '",' + str(dataIndex) + "],\n"
)
dataTraces.write(js_entry)
return json_first, ok
def ensure_file_start(path, start_text):
if not os.path.exists(path):
with open(path, "w", encoding="utf-8") as fh:
fh.write(start_text)
return True
return False
def process_gpx_folder(
input_folder,
json_path,
js_path,
temp_dir,
gpx_dir,
pause_s=2.0,
pattern="*.gpx",
):
garmin = os.path.join(input_folder, "")
dossier = os.path.dirname(json_path)
if not os.path.isdir(garmin):
raise FileNotFoundError(f"Dossier GPX inexistant: {garmin}")
os.makedirs(os.path.dirname(json_path), exist_ok=True)
os.makedirs(os.path.dirname(js_path), exist_ok=True)
os.makedirs(temp_dir, exist_ok=True)
os.makedirs(gpx_dir, exist_ok=True)
json_new = ensure_file_start(json_path, "[\n")
data_new = ensure_file_start(js_path, "var addressPoints = [\n")
if not json_new:
strip_trailing_closer(json_path, ("]",))
if not data_new:
strip_trailing_closer(js_path, ("];",))
jsTraces = open(json_path, "a", encoding="utf-8")
dataTraces = open(js_path, "a", encoding="utf-8")
json_first = json_new
existing_gpx = load_existing_gpx_names(json_path)
dataIndex = 0 if json_new else get_last_id(json_path)
_load_geo_cache(os.path.join(dossier, "geo_cache.json"))
erreurs = []
traites = 0
listOfFiles = sorted(os.listdir(garmin))
matches = [f for f in listOfFiles if fnmatch.fnmatch(f, pattern)]
if not matches:
jsTraces.close()
dataTraces.close()
return {"ok": True, "treated": 0, "errors": []}
for fichier in matches:
if fichier in existing_gpx:
print(f"--> skip already imported fichier={fichier}")
sys.stdout.flush()
continue
src_path = os.path.join(garmin, fichier)
try:
with open(src_path, "r", encoding="utf-8") as gf:
gpx = mod_gpxpy.parse(gf)
json_first, ok = print_gpx_part_info(
gpx, jsTraces, dataTraces, fichier, dataIndex, json_first
)
if ok:
traites += 1
dataIndex += 1
existing_gpx.add(fichier)
try:
shutil.copy2(src_path, os.path.join(gpx_dir, fichier))
except Exception as e:
erreurs.append(f"{fichier}: copy_to_gpx_dir_failed: {e}")
else:
erreurs.append(fichier)
time.sleep(pause_s)
except Exception as e:
mod_logging.exception(e)
erreurs.append(fichier)
time.sleep(pause_s)
jsTraces.write(']\n')
jsTraces.close()
dataTraces.write('];\n')
dataTraces.close()
_save_geo_cache(os.path.join(dossier, "geo_cache.json"))
try:
for f in matches:
os.remove(os.path.join(garmin, f))
except Exception:
pass
return {"ok": True, "treated": traites, "errors": erreurs}
Configuration config.json
/sharenfs/rnmkcy/web-traces-gpx/config.json
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
{
"default_sort": {
"column": "jour",
"descending": true
},
"json_path": "/sharenfs/traces-gpx/tracestableau.json",
"js_path": "/sharenfs/traces-gpx/tracesdataset.js",
"gpx_dir": "/sharenfs/traces-gpx/",
"temp_dir": "/tmp/gpx-studio-drop",
"visible_columns": [
"lieu",
"commune",
"cp",
"distance",
"vitesse",
"niveau",
"jour"
],
"window": {
"width": 1200,
"height": 700,
"icon_path": "/sharenfs/traces-gpx/tableau-gpx.png"
},
"theme": {
"mode": "light"
}
}
Le process uwsgi tourne en yick. Il faut donner à yick le droit d’écrire config.json (et idéalement le dossier).
Template index.html
/sharenfs/rnmkcy/web-traces-gpx/templates/index.html
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/select/1.7.0/css/select.dataTables.min.css">
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/select/1.7.0/js/dataTables.select.min.js"></script>
</head>
<body>
<div>
<label>Recherche: <input id="search" /></label>
<button id="copy-gpx">Copier GPX sélection</button>
<button id="toggle-selected-view">Afficher sélection</button>
</div>
<div style="margin-top:12px;">
<div style="margin-bottom:6px;">
Importation GPX :
<input id="gpxFiles" type="file" accept=".gpx" multiple />
<button id="importBtn" type="button">Importation</button>
</div>
<details id="importStatusDetails">
<summary>Développer / Réduire</summary>
<pre id="importStatus" style="white-space: pre-wrap; margin: 0;"></pre>
</details>
</div>
<div style="margin-top:10px;">
<div>Colonnes visibles :</div>
<div id="column-toggles"></div>
</div>
<table id="tbl" class="display" style="width:100%; margin-top:10px;">
<thead>
<tr>
<th></th>
<th>id</th><th>latitude</th><th>longitude</th><th>lieu</th><th>commune</th><th>cp</th>
<th>distance</th><th>vitesse</th><th>niveau</th><th>jour</th><th>gpx</th>
</tr>
</thead>
</table>
<script>
$(document).ready(function(){
const allColumns = [
{ label: 'id', data: 'id' },
{ label: 'latitude', data: 'latitude' },
{ label: 'longitude', data: 'longitude' },
{ label: 'lieu', data: 'lieu' },
{ label: 'commune', data: 'commune' },
{ label: 'cp', data: 'cp' },
{ label: 'distance', data: 'distance' },
{ label: 'vitesse', data: 'vitesse' },
{ label: 'niveau', data: 'niveau' },
{ label: 'jour', data: 'jour' },
{ label: 'gpx', data: 'gpx' }
];
const colIndexByData = {};
allColumns.forEach((c, i) => { colIndexByData[c.data] = i + 1; });
const toggleRoot = $('#column-toggles');
toggleRoot.empty();
allColumns.forEach(c => {
toggleRoot.append(`
<label style="margin-right:12px;">
<input type="checkbox" data-col="${c.data}">
${c.label}
</label>
`);
});
let showSelectedOnly = false;
const selectedIds = new Set();
const table = $('#tbl').DataTable({
ajax: { url: '/api/traces', dataSrc: '' },
pageLength: -1,
lengthMenu: [[10, 25, 100, -1], [10, 25, 100, 'ALL']],
// pageLength définit la taille initiale affichée, et -1 signifie “tout afficher”.
columns: [
{
data: null,
orderable: false,
searchable: false,
className: 'select-checkbox',
defaultContent: '',
render: function(){ return '<input type="checkbox" />'; }
},
{ data: 'id' },
{ data: 'latitude' },
{ data: 'longitude' },
{ data: 'lieu' },
{ data: 'commune' },
{ data: 'cp' },
{ data: 'distance' },
{ data: 'vitesse' },
{ data: 'niveau' },
{ data: 'jour' },
{
data: 'gpx',
render: function(d){
if(!d) return '';
return '<a href="/gpx/'+d+'" target="_blank">'+d+'</a>';
}
}
],
select: { style: 'multi', selector: 'td.select-checkbox' },
order: [[colIndexByData['jour'], 'desc']],
rowId: function(row){ return 'row_' + row.id; }
});
function refreshSelectedIds() {
selectedIds.clear();
table.rows({ selected: true }).data().toArray().forEach(r => {
if (r && r.id !== undefined && r.id !== null) {
selectedIds.add(String(r.id));
}
});
}
function syncCheckboxes() {
table.rows().every(function () {
const node = this.node();
if (!node) return;
$(node).find('td.select-checkbox input[type="checkbox"]').prop('checked', this.selected());
});
}
$.fn.dataTable.ext.search.push(function(settings, data, dataIndex){
if (settings.nTable !== table.table().node()) return true;
if (!showSelectedOnly) return true;
const rowData = table.row(dataIndex).data();
if (!rowData) return false;
return selectedIds.has(String(rowData.id));
});
$('#search').on('input', function(){
table.search(this.value).draw();
});
$('#tbl tbody').on('change', 'td.select-checkbox input[type="checkbox"]', function(e){
e.stopPropagation();
const row = table.row($(this).closest('tr'));
if (!row.length) return;
if (this.checked) row.select();
else row.deselect();
});
table.on('select deselect', function(){
refreshSelectedIds();
syncCheckboxes();
if (showSelectedOnly) table.draw(false);
});
$('#toggle-selected-view').on('click', function(){
showSelectedOnly = !showSelectedOnly;
$(this).text(showSelectedOnly ? 'Afficher tout' : 'Afficher sélection');
refreshSelectedIds();
table.draw(false);
});
table.on('draw', function(){
syncCheckboxes();
});
$('#copy-gpx').off('click').on('click', function () {
const rowsData = table.rows({ selected: true }).data().toArray();
const names = rowsData.map(r => r.gpx).filter(Boolean);
if (!names.length) {
alert('Aucune sélection');
return;
}
$.ajax({
url: '/api/download-gpx-zip',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify({ names: names }),
xhrFields: { responseType: 'blob' } // IMPORTANT
}).done(function (blob, status, xhr) {
// Déclencher le téléchargement
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
// Nom de fichier depuis Content-Disposition si présent
let filename = 'gpx_download.zip';
const cd = xhr.getResponseHeader('Content-Disposition');
if (cd) {
const m = cd.match(/filename="?([^"]+)"?/);
if (m) filename = m[1];
}
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
alert('Téléchargement ZIP démarré.');
}).fail(function (xhr) {
alert('Erreur: ' + (xhr.responseText || xhr.statusText));
});
});
function applyVisibility(visibleSet){
allColumns.forEach(c => {
const idx = colIndexByData[c.data];
table.column(idx).visible(visibleSet.has(c.data), false);
});
table.columns.adjust().draw(false);
}
function getCurrentVisibleSet(){
const set = new Set();
$('#column-toggles input[type="checkbox"]').each(function(){
const col = $(this).data('col');
if ($(this).is(':checked')) set.add(col);
});
return set;
}
function applyFromJson(){
return $.getJSON('/api/config/visible-columns')
.done(function(resp){
const visible = new Set(resp.visible_columns || []);
$('#column-toggles input[type="checkbox"]').each(function(){
const col = $(this).data('col');
$(this).prop('checked', visible.has(col));
});
if (visible.size === 0) {
$('#column-toggles input[type="checkbox"][data-col="jour"]').prop('checked', true);
visible.add('jour');
}
applyVisibility(visible);
})
.fail(function(xhr){
alert('Erreur chargement visible_columns: ' + (xhr.responseText || xhr.statusText));
});
}
$('#column-toggles input[type="checkbox"]').prop('checked', false);
applyVisibility(new Set());
applyFromJson();
$('#column-toggles').on('change', 'input[type="checkbox"]', function(){
const visible = getCurrentVisibleSet();
if (visible.size === 0) {
$(this).prop('checked', true);
visible.add($(this).data('col'));
}
applyVisibility(visible);
$.ajax({
url: '/api/config/visible-columns',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify({ visible_columns: Array.from(visible) })
}).fail(function(xhr){
alert('Erreur sauvegarde config: ' + (xhr.responseText || xhr.statusText));
});
});
// --- Importation GPX ---
const importFilesEl = document.getElementById('gpxFiles');
const importBtnEl = document.getElementById('importBtn');
const importStatusEl = document.getElementById('importStatus');
function pollImport(taskId) {
fetch(`/api/import-gpx-status/${taskId}`)
.then(r => r.json())
.then(j => {
if (!j.ok) {
importStatusEl.textContent = "Erreur: " + (j.error || "task introuvable");
return;
}
importStatusEl.textContent = JSON.stringify(j.task, null, 2);
if (j.task.state === "running") {
setTimeout(() => pollImport(taskId), 1000);
return;
}
if (j.task.state === "done") {
// Rafraîchir le tableau et remettre le statut
table.ajax.reload(null, false);
}
})
.catch(err => {
importStatusEl.textContent = "Erreur fetch: " + err;
});
}
importBtnEl.addEventListener('click', async () => {
const files = importFilesEl.files;
if (!files || files.length === 0) {
importStatusEl.textContent = "Choisis au moins un fichier .gpx";
return;
}
const fd = new FormData();
for (const f of files) fd.append('files', f);
importStatusEl.textContent = "Envoi...";
importBtnEl.disabled = true;
try {
const resp = await fetch('/api/import-gpx', { method: 'POST', body: fd });
const j = await resp.json();
if (!j.ok) {
importStatusEl.textContent = "Erreur: " + (j.error || 'import échoué');
importBtnEl.disabled = false;
return;
}
importStatusEl.textContent = `Tâche lancée: ${j.task_id}`;
pollImport(j.task_id);
} catch (e) {
importStatusEl.textContent = "Erreur: " + e;
} finally {
importBtnEl.disabled = false;
}
});
});
</script>
</body>
</html>
uwsgi.ini
/sharenfs/rnmkcy/web-traces-gpx/uwsgi.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[uwsgi]
chdir = /sharenfs/rnmkcy/web-traces-gpx
module = app:app
master = true
processes = 1
threads = 2
vacuum = true
die-on-term = true
socket = /run/uwsgi/gpx-web.sock
chmod-socket = 660
uid = 1000
gid = 1000
disable-logging = false
Ce modèle correspond aux configurations uWSGI standards avec socket UNIX, permissions et mode daemonisé pour nginx
/sharenfs/rnmkcy/web-traces-gpx/README.md
1
2
3
4
5
6
7
8
9
10
11
# gpx-web
Application Flask de consultation du tableau JSON avec accès aux GPX.
## Installation
```bash
cd /srv/gpx-web
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip wheel
pip install flask uwsgi
Droits
1
2
3
sudo chown -R $USER:$USER /sharenfs/rnmkcy/web-traces-gpx
sudo chmod -R 775 /sharenfs/rnmkcy/web-traces-gpx
sudo chmod 664 /sharenfs/rnmkcy/web-traces-gpx/config.json
Activer systemd
Création /etc/systemd/system/web-traces-gpx.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[Unit]
Description=uWSGI instance for web-traces-gpx
After=network.target
[Service]
Type=notify
User=yick
Group=yick
WorkingDirectory=/sharenfs/rnmkcy/web-traces-gpx
RuntimeDirectory=uwsgi
RuntimeDirectoryMode=0755
Environment="PATH=/sharenfs/rnmkcy/web-traces-gpx/venv/bin"
ExecStart=/sharenfs/rnmkcy/web-traces-gpx/venv/bin/uwsgi --ini /sharenfs/rnmkcy/web-traces-gpx/uwsgi.ini
Restart=always
KillSignal=SIGQUIT
TimeoutStartSec=30
[Install]
WantedBy=multi-user.target
Activer, lancer et vérifier le service
1
2
3
sudo systemctl daemon-reload
sudo systemctl enable web-traces-gpx --now
sudo systemctl status web-traces-gpx
Status
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
Created symlink '/etc/systemd/system/multi-user.target.wants/web-traces-gpx.service' → '/etc/systemd/system/web-traces-gpx.service'.
● web-traces-gpx.service - uWSGI instance for web-traces-gpx
Loaded: loaded (/etc/systemd/system/web-traces-gpx.service; enabled; preset: enabled)
Active: active (running) since Fri 2026-07-10 11:59:25 CEST; 18ms ago
Invocation: c0cc4d3f8aee4779bf5e4bb252df156d
Main PID: 1324735 (uwsgi)
Status: "uWSGI is ready"
Tasks: 9 (limit: 38029)
Memory: 28.8M (peak: 28.9M)
CPU: 180ms
CGroup: /system.slice/web-traces-gpx.service
├─1324735 /sharenfs/rnmkcy/web-traces-gpx/venv/bin/uwsgi --ini /sharenfs/rnmkcy/web-traces-gpx/uwsgi.ini
├─1324737 /sharenfs/rnmkcy/web-traces-gpx/venv/bin/uwsgi --ini /sharenfs/rnmkcy/web-traces-gpx/uwsgi.ini
├─1324738 /sharenfs/rnmkcy/web-traces-gpx/venv/bin/uwsgi --ini /sharenfs/rnmkcy/web-traces-gpx/uwsgi.ini
├─1324740 /sharenfs/rnmkcy/web-traces-gpx/venv/bin/uwsgi --ini /sharenfs/rnmkcy/web-traces-gpx/uwsgi.ini
└─1324742 /sharenfs/rnmkcy/web-traces-gpx/venv/bin/uwsgi --ini /sharenfs/rnmkcy/web-traces-gpx/uwsgi.ini
juil. 10 11:59:25 alder uwsgi[1324735]: mapped 416720 bytes (406 KB) for 8 cores
juil. 10 11:59:25 alder uwsgi[1324735]: *** Operational MODE: preforking+threaded ***
juil. 10 11:59:25 alder uwsgi[1324735]: WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x7f7deed29610 pid: 1324735 (default app)
juil. 10 11:59:25 alder uwsgi[1324735]: *** uWSGI is running in multiple interpreter mode ***
juil. 10 11:59:25 alder uwsgi[1324735]: spawned uWSGI master process (pid: 1324735)
juil. 10 11:59:25 alder systemd[1]: Started web-traces-gpx.service - uWSGI instance for web-traces-gpx.
juil. 10 11:59:25 alder uwsgi[1324735]: spawned uWSGI worker 1 (pid: 1324737, cores: 2)
juil. 10 11:59:25 alder uwsgi[1324735]: spawned uWSGI worker 2 (pid: 1324738, cores: 2)
juil. 10 11:59:25 alder uwsgi[1324735]: spawned uWSGI worker 3 (pid: 1324740, cores: 2)
juil. 10 11:59:25 alder uwsgi[1324735]: spawned uWSGI worker 4 (pid: 1324742, cores: 2)
Activer nginx
Création fichier de configuration nginx /etc/nginx/conf.d//traces.rnmkcy.eu.conf pour traces.rnmkcy.eu
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
sudo tee /etc/nginx/conf.d//traces.rnmkcy.eu.conf > /dev/null <<'EOF'
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name traces.rnmkcy.eu;
include /etc/nginx/conf.d/ssl-modern.inc;
charset utf-8;
client_max_body_size 20M;
access_log /var/log/nginx/gpx-web.access.log;
error_log /var/log/nginx/gpx-web.error.log;
location /static/ {
alias /sharenfs/rnmkcy/web-traces-gpx/static/;
expires 7d;
add_header Cache-Control "public, max-age=604800";
access_log off;
}
location /gpx/ {
alias /sharenfs/traces-gpx/;
autoindex off;
expires 1h;
add_header Cache-Control "public, max-age=3600";
access_log off;
}
location = /healthz {
access_log off;
return 200 "ok\n";
add_header Content-Type text/plain;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/gpx-web.sock;
uwsgi_read_timeout 120s;
uwsgi_connect_timeout 30s;
}
}
EOF
Vérifier et recharger nginx
1
2
sudo nginx -t
sudo systemctl reload nginx
Vérifications
1
curl -I https://traces.rnmkcy.eu/
Réponse
1
2
3
4
5
HTTP/2 200
server: nginx
date: Thu, 16 Jul 2026 07:21:41 GMT
content-type: text/html; charset=utf-8
content-length: 10080
1
curl https://traces.rnmkcy.eu/healthz
Renvoie OK
traces.rnmkcy.eu
Le lien https://traces.rnmkcy.eu
Archive ZIP ou tar.gz
Quand tout est en place, vous pouvez créer un ZIP avec zip -r gpx-web.zip /srv/gpx-web.
1
2
cd /sharenfs/rnmkcy/
zip -r web-traces-gpx.zip web-traces-gpx
ou :
1
tar -czf web-traces-gpx.tar.gz -C /sharenfs/rnmkcy web-traces-gpx
Maintenance
Redémarrage service
Avec `sudo systemctl restart web-traces-gpx.service`, on demande à systemd de gérer le cycle de vie du service. Tant que systemd n’a pas terminé l’arrêt/redémarrage, la commande reste active et attend le résultat. Si le service prend du temps à s’arrêter ou à repartir, on peut avoir l’impression que le terminal ne “rend pas la main” immédiatement `Ctrl+C` envoie une interruption au processus au premier plan de ton terminal, pas à tout le travail déjà pris en charge par systemd. Si `systemctl` est déjà en train de parler au gestionnaire de services, l’effet peut être limité ou arriver trop tard pour annuler proprement l’actionComment vérifier sans attendre?
Lancer le redémarrage puis suivre l’état dans un autre terminal :
1
sudo systemctl restart web-traces-gpx.service
Puis dans un autre terminal
1
2
sudo systemctl status web-traces-gpx.service
sudo journalctl -u web-traces-gpx.service -f
Si le service est lent à arrêter, c’est parfois lié au comportement de l’unité systemd, au
TimeoutStartSec, ou au fait que le processus enfant ne se termine pas tout de suite. Avec uWSGI, systemd attend aussi la bonne séquence d’arrêt et de redémarrage, donc un léger délai est normal.
Erreur 502
Suite changement utilisateur www-data –> yick
Le 502 vient du socket Unix : Nginx n’a pas le droit de se connecter à /run/uwsgi/gpx-web.sock. Le message Permission denied indique presque toujours un problème de propriétaire, groupe ou droits sur le socket ou sur un dossier parent
Corrections
donner à Nginx l’accès au groupe yick.
1
sudo usermod -aG yick www-data
Après l’ajout de www-data au groupe yick, il faut redémarrer Nginx pour qu’il prenne en compte les nouveaux groupes :
1
sudo systemctl restart nginx
uid = yick et gid = yick font tourner uWSGI sous le bon utilisateur et socket = /run/uwsgi/gpx-web.sock avec chmod-socket = 660 permet l’accès au socket au propriétaire et au groupe.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[uwsgi]
chdir = /sharenfs/rnmkcy/web-traces-gpx
module = app:app
master = true
processes = 4
threads = 2
vacuum = true
die-on-term = true
socket = /run/uwsgi/gpx-web.sock
chmod-socket = 660
uid = yick
gid = yick
disable-logging = false
Redémarrer uWSGI :
1
sudo systemctl restart web-traces-gpx.service
Le socket doit idéalement apparaître comme ceci :
1
srw-rw---- 1 yick yick /run/uwsgi/gpx-web.sock
Et comme www-data est maintenant membre de yick, Nginx devrait pouvoir s’y connecter.


