59 lines
2.3 KiB
Python
59 lines
2.3 KiB
Python
"""Pages HTML du poste de dev : bascule sur .venv si plotly manque, ouverture dans le navigateur, zoom (D-013).
|
|||
|
|
|
||
|
|
À importer avant plotly : si plotly n'est pas installé, l'import relance le script sous .venv/bin/python.
|
||
|
|
"""
|
||
|
|
import importlib.util
|
||
|
|
import os
|
||
|
|
import shutil
|
||
|
|
import subprocess
|
||
|
|
import sys
|
||
|
|
import webbrowser
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
ROOT = Path(__file__).resolve().parents[2]
|
||
|
|
VENV = ROOT / ".venv" / "bin" / "python"
|
||
|
|
if importlib.util.find_spec("plotly") is None and VENV.exists():
|
||
|
|
os.execv(VENV, [str(VENV), *sys.argv]) # plotly n'est installé que dans .venv
|
||
|
|
|
||
|
|
# Plotly ne recale pas l'ordonnée sur la plage visible : après chaque zoom ou déplacement horizontal,
|
||
|
|
# ce script recalcule l'axe vertical de chaque graphique sur les points visibles (5 % de marge),
|
||
|
|
# seuil compris (c'est une courbe). Double-clic : retour à la vue complète.
|
||
|
|
AUTOSCALE_Y_JS = """
|
||
|
|
const gd = document.getElementById('{plot_id}');
|
||
|
|
const name = (t, a) => a + 'axis' + (t[a + 'axis'] || a).slice(1);
|
||
|
|
const xs = gd.data.map(t => t.x.map(v => gd._fullLayout[name(t, 'x')].d2l(v)));
|
||
|
|
gd.on('plotly_relayout', ev => {
|
||
|
|
const keys = Object.keys(ev);
|
||
|
|
if (!keys.some(k => k.startsWith('xaxis'))) return;
|
||
|
|
const upd = {};
|
||
|
|
if (keys.some(k => k.endsWith('autorange'))) {
|
||
|
|
gd.data.forEach(t => { upd[name(t, 'y') + '.autorange'] = true; });
|
||
|
|
return Plotly.relayout(gd, upd);
|
||
|
|
}
|
||
|
|
const box = {};
|
||
|
|
gd.data.forEach((t, i) => {
|
||
|
|
if (t.visible === 'legendonly' || t.visible === false) return;
|
||
|
|
const xa = gd._fullLayout[name(t, 'x')];
|
||
|
|
const [a, b] = xa.range.map(r => xa.r2l(r));
|
||
|
|
const r = box[name(t, 'y')] || (box[name(t, 'y')] = [Infinity, -Infinity]);
|
||
|
|
t.y.forEach((y, j) => {
|
||
|
|
if (y === null || !isFinite(y) || xs[i][j] < a || xs[i][j] > b) return;
|
||
|
|
r[0] = Math.min(r[0], y);
|
||
|
|
r[1] = Math.max(r[1], y);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
for (const [ya, [lo, hi]] of Object.entries(box)) {
|
||
|
|
if (lo > hi) continue;
|
||
|
|
const pad = (hi - lo) * 0.05 || Math.abs(hi) * 0.01 || 1;
|
||
|
|
upd[ya + '.range'] = [lo - pad, hi + pad];
|
||
|
|
}
|
||
|
|
Plotly.relayout(gd, upd);
|
||
|
|
});
|
||
|
|
"""
|
||
|
|
|
||
|
|
|
||
|
|
def open_html(path):
|
||
|
|
if shutil.which("explorer.exe") and shutil.which("wslpath"): # WSL : navigateur Windows
|
||
|
|
subprocess.run(["explorer.exe", subprocess.check_output(["wslpath", "-w", path], text=True).strip()])
|
||
|
|
else:
|
||
|
|
webbrowser.open(Path(path).resolve().as_uri())
|