Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from flask import Flask, request, jsonify, render_template_string
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
# Simulación de la función de cálculo del KCDI
|
| 8 |
+
# Reemplaza esta función con tu lógica real de cálculo del KCDI
|
| 9 |
+
def calculate_kcdi(df):
|
| 10 |
+
"""
|
| 11 |
+
Calcula el KCDI a partir de un DataFrame de pandas.
|
| 12 |
+
"""
|
| 13 |
+
try:
|
| 14 |
+
# Aquí iría tu código para procesar el DataFrame de Scopus
|
| 15 |
+
# y calcular los valores de KCDI, entropía, etc.
|
| 16 |
+
# Por ejemplo, basándote en la estructura de tu análisis.
|
| 17 |
+
|
| 18 |
+
# Valores simulados para fines de demostración
|
| 19 |
+
kcdi = 0.916
|
| 20 |
+
h_shannon = 1.668
|
| 21 |
+
w_bar = 0.12
|
| 22 |
+
description = "Resultado simulado para demostración"
|
| 23 |
+
|
| 24 |
+
return {
|
| 25 |
+
'kcdi_value': kcdi,
|
| 26 |
+
'H_shannon': h_shannon,
|
| 27 |
+
'W_bar': w_bar,
|
| 28 |
+
'description': description
|
| 29 |
+
}
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return {'error': f'Error en el cálculo: {str(e)}'}
|
| 32 |
+
|
| 33 |
+
# Plantilla HTML y JavaScript directamente en el código de Python
|
| 34 |
+
html_template = """
|
| 35 |
+
<!DOCTYPE html>
|
| 36 |
+
<html>
|
| 37 |
+
<head>
|
| 38 |
+
<title>Calculadora KCDI</title>
|
| 39 |
+
<style>
|
| 40 |
+
body { font-family: 'Helvetica', 'Arial', sans-serif; line-height: 1.6; padding: 20px; max-width: 800px; margin: auto; }
|
| 41 |
+
h1 { color: #333; }
|
| 42 |
+
.container { background: #f9f9f9; padding: 20px; border-radius: 8px; border: 1px solid #ddd; }
|
| 43 |
+
input[type="file"] { margin-bottom: 10px; }
|
| 44 |
+
button { background-color: #4CAF50; color: white; padding: 10px 15px; border: none; border-radius: 5px; cursor: pointer; }
|
| 45 |
+
button:hover { background-color: #45a049; }
|
| 46 |
+
#results { margin-top: 20px; padding: 15px; border: 1px solid #ccc; border-radius: 5px; background: #fff; }
|
| 47 |
+
</style>
|
| 48 |
+
</head>
|
| 49 |
+
<body>
|
| 50 |
+
<div class="container">
|
| 51 |
+
<h1>Calculadora de Índice KCDI</h1>
|
| 52 |
+
<p>Sube tu archivo CSV exportado de Scopus para calcular el KCDI.</p>
|
| 53 |
+
<form id="upload-form" enctype="multipart/form-data">
|
| 54 |
+
<input type="file" name="file" accept=".csv">
|
| 55 |
+
<button type="submit">Calcular KCDI</button>
|
| 56 |
+
</form>
|
| 57 |
+
</div>
|
| 58 |
+
<div id="results">
|
| 59 |
+
</div>
|
| 60 |
+
|
| 61 |
+
<script>
|
| 62 |
+
document.getElementById('upload-form').addEventListener('submit', function(e) {
|
| 63 |
+
e.preventDefault();
|
| 64 |
+
|
| 65 |
+
const formData = new FormData(this);
|
| 66 |
+
const resultsDiv = document.getElementById('results');
|
| 67 |
+
resultsDiv.innerHTML = '<p>Calculando...</p>';
|
| 68 |
+
|
| 69 |
+
fetch('/calculate', {
|
| 70 |
+
method: 'POST',
|
| 71 |
+
body: formData
|
| 72 |
+
})
|
| 73 |
+
.then(response => response.json())
|
| 74 |
+
.then(data => {
|
| 75 |
+
if (data.error) {
|
| 76 |
+
resultsDiv.innerHTML = `<p style="color:red;">Error: ${data.error}</p>`;
|
| 77 |
+
} else {
|
| 78 |
+
resultsDiv.innerHTML = `
|
| 79 |
+
<h2>Resultados:</h2>
|
| 80 |
+
<p>KCDI: <strong>${data.kcdi_value}</strong></p>
|
| 81 |
+
<p>Entropía (H): <strong>${data.H_shannon}</strong></p>
|
| 82 |
+
<p>W Bar: <strong>${data.W_bar}</strong></p>
|
| 83 |
+
<p>Descripción: <strong>${data.description}</strong></p>
|
| 84 |
+
`;
|
| 85 |
+
}
|
| 86 |
+
})
|
| 87 |
+
.catch(error => {
|
| 88 |
+
resultsDiv.innerHTML = `<p style="color:red;">Error de conexión: ${error}</p>`;
|
| 89 |
+
});
|
| 90 |
+
});
|
| 91 |
+
</script>
|
| 92 |
+
</body>
|
| 93 |
+
</html>
|
| 94 |
+
"""
|
| 95 |
+
|
| 96 |
+
@app.route('/')
|
| 97 |
+
def index():
|
| 98 |
+
return render_template_string(html_template)
|
| 99 |
+
|
| 100 |
+
@app.route('/calculate', methods=['POST'])
|
| 101 |
+
def calculate():
|
| 102 |
+
if 'file' not in request.files:
|
| 103 |
+
return jsonify({'error': 'No se encontró el archivo'}), 400
|
| 104 |
+
|
| 105 |
+
file = request.files['file']
|
| 106 |
+
if file.filename == '':
|
| 107 |
+
return jsonify({'error': 'No se seleccionó ningún archivo'}), 400
|
| 108 |
+
|
| 109 |
+
if file:
|
| 110 |
+
try:
|
| 111 |
+
df = pd.read_csv(file)
|
| 112 |
+
results = calculate_kcdi(df)
|
| 113 |
+
return jsonify(results), 200
|
| 114 |
+
except Exception as e:
|
| 115 |
+
return jsonify({'error': f'Error en el procesamiento del archivo: {str(e)}'}), 500
|
| 116 |
+
|
| 117 |
+
if __name__ == '__main__':
|
| 118 |
+
app.run(debug=True)
|