116 lines
4.9 KiB
Python
116 lines
4.9 KiB
Python
from flask import Flask, jsonify, abort, render_template, send_from_directory
|
||
import pandas as pd
|
||
import json
|
||
import zipfile
|
||
import os
|
||
import pickle
|
||
import joblib
|
||
from io import TextIOWrapper
|
||
|
||
|
||
app = Flask(__name__)
|
||
FREQTRADE_USERDATA_DIR = '/mnt/external'
|
||
|
||
|
||
@app.route('/')
|
||
def home():
|
||
# Liste les fichiers dans le répertoire monté
|
||
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))]
|
||
|
||
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))]
|
||
|
||
# Retourne le template avec la liste des fichiers
|
||
return render_template('index.html', files=files, files2=files2)
|
||
|
||
|
||
@app.route('/process', methods=['POST'])
|
||
def process():
|
||
# Traitez ici les données envoyées par l'utilisateur
|
||
return "Données traitées !"
|
||
|
||
|
||
@app.route('/read_json/<path:filename>')
|
||
def read_json(filename):
|
||
full_path = os.path.join(FREQTRADE_USERDATA_DIR + "/backtest_results", filename)
|
||
|
||
if filename.endswith('.json'):
|
||
with open(full_path) as f:
|
||
return f.read(), 200, {'Content-Type': 'application/json'}
|
||
|
||
if filename.endswith('.pkl'):
|
||
try:
|
||
data = joblib.load(full_path)
|
||
if isinstance(data, pd.DataFrame):
|
||
return data.to_json(orient='split'), 200, {'Content-Type': 'application/json'}
|
||
if isinstance(data, dict):
|
||
df = pd.DataFrame.from_dict(data)
|
||
return df.to_json(orient='split'), 200, {'Content-Type': 'application/json'}
|
||
if isinstance(data, list):
|
||
df = pd.DataFrame(data)
|
||
return df.to_json(orient='split'), 200, {'Content-Type': 'application/json'}
|
||
return json.dumps({"error": f"Type {type(data)} non géré."}), 200
|
||
except Exception as e:
|
||
return json.dumps({"error": str(e)}), 500
|
||
|
||
elif filename.endswith('.zip'):
|
||
try:
|
||
with zipfile.ZipFile(full_path) as z:
|
||
zip_contents = {}
|
||
for name in z.namelist():
|
||
if name.endswith('.json'):
|
||
with z.open(name) as f:
|
||
zip_contents[name] = json.load(f)
|
||
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'}
|
||
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):
|
||
# 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'}
|
||
elif isinstance(data, list):
|
||
print('list')
|
||
df = pd.DataFrame(data)
|
||
zip_contents[name] = df.to_html() #orient='split'), 200, {'Content-Type': 'application/json'}
|
||
else:
|
||
zip_contents[name] = json.dumps({"error": f"Type {type(data)} non géré."}), 200
|
||
except Exception as e:
|
||
zip_contents[name] = json.dumps({"error": str(e)}), 500
|
||
return json.dumps(zip_contents)
|
||
except Exception as e:
|
||
return json.dumps({"error": str(e)}), 500
|
||
|
||
return json.dumps({"error": "Fichier non pris en charge"}), 400
|
||
|
||
|
||
@app.route('/read_feather/<path:filename>')
|
||
def read_feather(filename):
|
||
path = os.path.join(FREQTRADE_USERDATA_DIR + "/data/binance/", filename)
|
||
try:
|
||
print(path)
|
||
df = pd.read_feather(path)
|
||
print(df)
|
||
return df.to_json(orient="records")
|
||
except Exception as e:
|
||
print(e)
|
||
return jsonify({"error": str(e)}), 500
|
||
|
||
|
||
if __name__ == '__main__':
|
||
app.run(debug=True, host='0.0.0.0', port=5000)
|