diff --git a/src/app.py b/src/app.py index 6617420..9089614 100644 --- a/src/app.py +++ b/src/app.py @@ -18,10 +18,10 @@ def home(): files = os.listdir(FREQTRADE_USERDATA_DIR + "/backtest_results") # Filtre pour obtenir uniquement les fichiers (pas les dossiers) - files = [f for f in files if os.path.isfile(os.path.join(FREQTRADE_USERDATA_DIR + "/backtest_results", f))] + files = [f for f in files if os.path.isfile(os.path.join(FREQTRADE_USERDATA_DIR + "/backtest_results", f)) and f.lower().endswith('.zip')] files2 = os.listdir(FREQTRADE_USERDATA_DIR + "/data/binance") - files2 = [f for f in files2 if os.path.isfile(os.path.join(FREQTRADE_USERDATA_DIR + "/data/binance", f))] + files2 = [f for f in files2 if os.path.isfile(os.path.join(FREQTRADE_USERDATA_DIR + "/data/binance", f)) and f.lower().endswith('.feather')] # Retourne le template avec la liste des fichiers return render_template('index.html', files=files, files2=files2) @@ -63,30 +63,41 @@ def read_json(filename): for name in z.namelist(): if name.endswith('.json'): with z.open(name) as f: + print(f"load_json {name}") zip_contents[name] = json.load(f) + elif name.endswith('.feather'): + with z.open(name) as f: + dataframe = pd.read_feather(f) + zip_contents[name] = dataframe.to_json(orient="records") + elif name.endswith('.pkl'): with z.open(name) as f: try: data = joblib.load(f) if isinstance(data, pd.DataFrame): - print("dataframe") - zip_contents[name] = data.to_csv() #orient='split'), 200, {'Content-Type': 'application/json'} + print(f"dataframe {name}") + zip_contents[name] = data.to_json(orient='records') #, 200, {'Content-Type': 'application/json'} elif isinstance(data, dict): # On suppose qu’il y a un seul modèle/clé au premier niveau outer_key = list(data.keys())[0] inner_dict = data[outer_key] if isinstance(inner_dict, dict): + print(f"dict {name}") # On suppose qu’il y a une seule paire (ex: 'BTC/USDT') inner_key = list(inner_dict.keys())[0] df = inner_dict[inner_key] - print(df) if isinstance(df, pd.DataFrame): - zip_contents[name] = df.to_html() #json(orient='split'), 200, {'Content-Type': 'application/json'} + type = name + # if "signals" in name: + # type = 'signals' + # elif "exited" in name: + # type = "exited" + zip_contents[type] = df.to_json(orient='records') #, 200, {'Content-Type': 'application/json'} elif isinstance(data, list): - print('list') + print(f"list {name}") df = pd.DataFrame(data) - zip_contents[name] = df.to_html() #orient='split'), 200, {'Content-Type': 'application/json'} + zip_contents[name] = df.to_json(orient='records') # , 200, {'Content-Type': 'application/json'} else: zip_contents[name] = json.dumps({"error": f"Type {type(data)} non géré."}), 200 except Exception as e: diff --git a/src/static/js/functions.js b/src/static/js/functions.js index f8c5aa9..de82351 100644 --- a/src/static/js/functions.js +++ b/src/static/js/functions.js @@ -10,22 +10,32 @@ function loadJson(filename) { const isMulti = typeof data === 'object' && !Array.isArray(data); if (isMulti) { + let count = 0 Object.entries(data).forEach(([name, content], index) => { - const tabId = `tab-${index}`; - const li = document.createElement('li'); - const btn = document.createElement('button'); - btn.textContent = name; - btn.onclick = () => showTab(tabId); - li.appendChild(btn); - tabButtons.appendChild(li); + count = count + 1 + if (name.includes("signals")) { + sessionStorage.setItem('signals', content); + addTab('signals', count, content) + } + if (name.includes("exited")) { + sessionStorage.setItem('exited', content); + addTab('exited', count, content) + } + if (name.includes("json")) { + sessionStorage.setItem('result_strategy', JSON.stringify(content.strategy)); + sessionStorage.setItem('strategy_comparison', JSON.stringify(content.strategy_comparison)); + addTab('result', count, JSON.stringify(content.strategy)) + count = count + 1 + addTab('comparaison', count, JSON.stringify(content.strategy_comparison)) + } + if (name.includes("market")) { + sessionStorage.setItem('market', content); + addTab('market', count, content) + } - const div = document.createElement('div'); - div.id = tabId; - div.className = 'tab-content'; - div.style.display = index === 0 ? 'block' : 'none'; - div.innerHTML = Array.isArray(content) ? renderTable(content) : `
${JSON.stringify(content, null, 2)}`;
- tabContents.appendChild(div);
});
+// renderChart(data, '', true)
+
} else {
const div = document.createElement('div');
div.innerHTML = `${JSON.stringify(data, null, 2)}`;
@@ -35,6 +45,152 @@ function loadJson(filename) {
.catch(err => alert("Erreur : " + err.message));
}
+function jsonToUl(json) {
+ if (typeof json !== 'object' || json === null) return document.createTextNode(json);
+
+ const ul = document.createElement('ul');
+
+ for (const key in json) {
+ const li = document.createElement('li');
+ const value = json[key];
+
+ if (typeof value === 'object' && value !== null) {
+ li.textContent = key;
+ li.appendChild(jsonToUl(value)); // recurse
+ } else {
+ li.textContent = `${key}: ${value}`;
+ }
+
+ ul.appendChild(li);
+ }
+
+ return ul;
+}
+//function jsonToTable(obj, depth = 0) {
+// const table = document.createElement('table');
+// table.border = 1;
+//
+// for (const key in obj) {
+// const row = document.createElement('tr');
+//
+// const cellKey = document.createElement('td');
+// cellKey.textContent = key;
+// row.appendChild(cellKey);
+//
+// const cellValue = document.createElement('td');
+// const value = obj[key];
+//
+// if (typeof value === 'object' && value !== null && depth < 4) {
+// cellValue.appendChild(jsonToTable(value, depth + 1)); // recurse
+// } else {
+// cellValue.textContent = value;
+// }
+//
+// row.appendChild(cellValue);
+// table.appendChild(row);
+// }
+//
+// return table;
+//}
+
+function jsonToTable(data, depth = 0) {
+ if (Array.isArray(data)) {
+ // Cas: tableau d'objets
+ if (data.length > 0 && typeof data[0] === 'object' && !Array.isArray(data[0])) {
+ const table = document.createElement('table');
+ table.border = 1;
+
+ // En-tête
+ const headerRow = document.createElement('tr');
+ Object.keys(data[0]).forEach(key => {
+ const th = document.createElement('th');
+ th.textContent = key;
+ headerRow.appendChild(th);
+ });
+ table.appendChild(headerRow);
+
+ // Lignes de données
+ data.forEach(item => {
+ const row = document.createElement('tr');
+ Object.values(item).forEach(value => {
+ const td = document.createElement('td');
+ td.textContent = (typeof value === 'object') ? JSON.stringify(value) : value;
+ row.appendChild(td);
+ });
+ table.appendChild(row);
+ });
+
+ return table;
+ } else {
+ // Liste simple : afficher horizontalement
+ const table = document.createElement('table');
+ table.border = 1;
+ const row = document.createElement('tr');
+ data.forEach(value => {
+ const td = document.createElement('td');
+ td.textContent = value;
+ row.appendChild(td);
+ });
+ table.appendChild(row);
+ return table;
+ }
+
+ } else if (typeof data === 'object' && data !== null) {
+ // Cas: objet clé/valeur
+ const table = document.createElement('table');
+ table.border = 1;
+
+ for (const key in data) {
+ const row = document.createElement('tr');
+
+ const tdKey = document.createElement('td');
+ tdKey.textContent = key;
+ row.appendChild(tdKey);
+
+ const tdValue = document.createElement('td');
+ const value = data[key];
+
+ if (typeof value === 'object') {
+ tdValue.appendChild(jsonToTable(value, depth + 1));
+ } else {
+ tdValue.textContent = value;
+ }
+
+ row.appendChild(tdValue);
+ table.appendChild(row);
+ }
+
+ return table;
+ } else {
+ const span = document.createElement('span');
+ span.textContent = data;
+ return span;
+ }
+}
+
+
+
+function addTab(name, index, content) {
+ const tabButtons = document.getElementById('tab-buttons');
+ const tabContents = document.getElementById('tab-contents');
+ const tabId = `tab-${index}`;
+ const li = document.createElement('li');
+ const btn = document.createElement('button');
+ btn.textContent = name;
+ btn.onclick = () => showTab(tabId);
+ li.appendChild(btn);
+ tabButtons.appendChild(li);
+
+ const div = document.createElement('div');
+ div.id = tabId;
+ div.className = 'tab-content';
+ div.style.display = index === 0 ? 'block' : 'none';
+// div.innerHTML = Array.isArray(content) ? renderTable(content) : jsonToUl(JSON.parse(content));
+ ul = jsonToTable(JSON.parse(content))
+ div.appendChild(ul);
+ tabContents.appendChild(div);
+}
+
function renderTable(rows) {
if (!rows.length) return "Aucune donnée
"; const headers = Object.keys(rows[0]); @@ -88,6 +244,15 @@ function renderChart(data, filename, create_columns) { // let rows = data.map(row => '