Skip to content

Commit

Permalink
Update app.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkas-Overgold authored Nov 22, 2024
1 parent 5531ab5 commit da59d7f
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
from flask import Flask, request, jsonify, send_file
from flask import Flask, request, jsonify, send_from_directory
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
import os
import sqlite3
import json

app = Flask(__name__)

# Configuración de directorios
STATIC_DIR = os.path.join(app.root_path, "graphs")
STATIC_DIR = os.path.join(app.root_path, "static", "graphs")
os.makedirs(STATIC_DIR, exist_ok=True)

# Configuración de la base de datos
DB_PATH = os.path.join(app.root_path, "file_uploads.sql")

# Inicializar base de datos
Expand All @@ -30,23 +28,19 @@ def init_db():

init_db()

# Procesar archivo y crear grafo
def procesar_archivo(file):
try:
# Leer archivo
if file.filename.endswith('.csv'):
df = pd.read_csv(file)
elif file.filename.endswith('.xlsx'):
df = pd.read_excel(file)
else:
raise ValueError("Formato no compatible")

# Crear grafo
G = nx.Graph()
for _, row in df.iterrows():
G.add_edge(row['nodo 1'], row['nodo 2'], weight=row['costo (usd)'])

# Generar gráficos
pos = nx.spring_layout(G)
grafo_path = os.path.join(STATIC_DIR, "grafo_completo.png")
mst_path = os.path.join(STATIC_DIR, "mst.png")
Expand All @@ -66,7 +60,15 @@ def procesar_archivo(file):

@app.route("/")
def index():
return send_file("index.html")
return send_from_directory('.', 'index.html')

@app.route("/styles.css")
def styles():
return send_from_directory('.', 'styles.css')

@app.route("/grafo.png")
def favicon():
return send_from_directory('.', 'grafo.png')

@app.route("/upload", methods=["POST"])
def upload_file():
Expand All @@ -84,8 +86,8 @@ def upload_file():
return jsonify(result), 400

return jsonify({
"grafo": f"/graphs/{os.path.basename(result['grafo'])}",
"mst": f"/graphs/{os.path.basename(result['mst'])}"
"grafo": f"/static/graphs/{os.path.basename(result['grafo'])}",
"mst": f"/static/graphs/{os.path.basename(result['mst'])}"
})

if __name__ == "__main__":
Expand Down

0 comments on commit da59d7f

Please sign in to comment.