88 lines
3.4 KiB
Python
88 lines
3.4 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__)
|
|
FILES_DIR = '/mnt/external'
|
|
|
|
|
|
@app.route('/')
|
|
def home():
|
|
# Liste les fichiers dans le répertoire monté
|
|
files = os.listdir('/mnt/external')
|
|
|
|
# Filtre pour obtenir uniquement les fichiers (pas les dossiers)
|
|
files = [f for f in files if os.path.isfile(os.path.join('/mnt/external', f))]
|
|
|
|
# Retourne le template avec la liste des fichiers
|
|
return render_template('index.html', files=files)
|
|
|
|
|
|
@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(FILES_DIR, 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):
|
|
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
|
|
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
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True, host='0.0.0.0', port=5000)
|