Affichage json et pkl
This commit is contained in:
66
src/app.py
66
src/app.py
@@ -1,7 +1,16 @@
|
||||
from flask import Flask, jsonify, abort, render_template, send_from_directory
|
||||
import pandas as pd
|
||||
import json
|
||||
import zipfile
|
||||
import os
|
||||
from flask import Flask, render_template
|
||||
import pickle
|
||||
import joblib
|
||||
from io import TextIOWrapper
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
FILES_DIR = '/mnt/external'
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def home():
|
||||
@@ -14,10 +23,65 @@ def home():
|
||||
# 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)
|
||||
|
||||
Reference in New Issue
Block a user