Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,8 @@ import matplotlib.pyplot as plt
|
|
4 |
import uuid
|
5 |
import os
|
6 |
import textwrap
|
7 |
-
import requests
|
|
|
8 |
|
9 |
api_key = os.getenv("AIRT_KEY")
|
10 |
AIRT_DBASEx = os.getenv("AIRT_DBASE")
|
@@ -20,26 +21,47 @@ headers = {
|
|
20 |
}
|
21 |
|
22 |
def cargar_desde_airtable():
|
23 |
-
"""Recupera los datos almacenados en AirTable y los grafica."""
|
24 |
-
aportes = []
|
25 |
response = requests.get(url, headers=headers)
|
26 |
|
27 |
-
if response.status_code
|
28 |
-
|
29 |
-
|
30 |
-
node_id = record["id"]
|
31 |
-
nombre = record["fields"].get("Nombre", "")
|
32 |
-
enfoque = record["fields"].get("Enfoque", "")
|
33 |
-
norma = record['fields'].get("Norma", "")
|
34 |
-
texto = record["fields"].get("Texto_HF", "")
|
35 |
-
|
36 |
-
label = f"{norma}"
|
37 |
-
aportes.append([nombre, enfoque, norma, texto])
|
38 |
-
aportes_df = pd.DataFrame(aportes, columns=["Nombre", "Enfoque", "Norma", "Texto_HF"])
|
39 |
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
def guardar_en_airtable(nombre, enfoque, norma, texto):
|
|
|
|
|
|
|
|
|
|
|
43 |
data = {"fields": {"Nombre": nombre, "Enfoque": enfoque, "Norma": norma, "Texto_HF": texto}}
|
44 |
requests.post(url, headers=headers, json=data)
|
45 |
|
@@ -49,33 +71,35 @@ def wrap_text(text, width=10):
|
|
49 |
def agregar_aporte(nombre, enfoque, norma, texto):
|
50 |
textox = wrap_text(f"{nombre}: {texto}")
|
51 |
|
52 |
-
# Fix: Prevent duplicate nodes/edges
|
53 |
if not G.has_node(norma):
|
54 |
G.add_node(norma, color='gray')
|
55 |
if not G.has_edge(norma, enfoque):
|
56 |
G.add_edge(norma, enfoque, label=textox)
|
57 |
|
58 |
-
guardar_en_airtable(nombre, enfoque, norma, texto)
|
59 |
return visualizar_grafo()
|
60 |
|
61 |
def visualizar_grafo():
|
62 |
plt.figure(figsize=(8, 8))
|
63 |
pos = nx.spring_layout(G)
|
64 |
-
|
65 |
-
# Fix: Use edge labels instead of node labels
|
66 |
edge_labels = nx.get_edge_attributes(G, 'label')
|
67 |
|
68 |
nx.draw(G, pos, with_labels=True, node_color='lightblue', edge_color='gray', node_size=2000, font_size=10)
|
69 |
-
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=8)
|
70 |
|
71 |
plt.title("Red de Aportes")
|
72 |
plt.savefig("graph.png")
|
73 |
plt.close()
|
74 |
return "graph.png"
|
75 |
|
|
|
|
|
76 |
iface = gr.Interface(
|
77 |
fn=agregar_aporte,
|
78 |
inputs=["text", "text", "text", "text"],
|
79 |
outputs="image",
|
80 |
-
title="Foro Din谩mico con Visualizaci贸n de Red"
|
|
|
|
|
81 |
iface.launch(share=True)
|
|
|
4 |
import uuid
|
5 |
import os
|
6 |
import textwrap
|
7 |
+
import requests
|
8 |
+
import pandas as pd
|
9 |
|
10 |
api_key = os.getenv("AIRT_KEY")
|
11 |
AIRT_DBASEx = os.getenv("AIRT_DBASE")
|
|
|
21 |
}
|
22 |
|
23 |
def cargar_desde_airtable():
|
|
|
|
|
24 |
response = requests.get(url, headers=headers)
|
25 |
|
26 |
+
if response.status_code != 200:
|
27 |
+
print(f"Error: {response.status_code} - {response.text}") # Debugging info
|
28 |
+
return pd.DataFrame(columns=["Nombre", "Enfoque", "Norma", "Texto_HF"]) # Return empty DataFrame
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
records = response.json().get("records", [])
|
31 |
+
|
32 |
+
# Compact list comprehension to extract data
|
33 |
+
aportes = [
|
34 |
+
[record["fields"].get("Nombre", ""),
|
35 |
+
record["fields"].get("Enfoque", ""),
|
36 |
+
record["fields"].get("Norma", ""),
|
37 |
+
record["fields"].get("Texto_HF", "")] for record in records
|
38 |
+
]
|
39 |
+
|
40 |
+
return pd.DataFrame(aportes, columns=["Nombre", "Enfoque", "Norma", "Texto_HF"])
|
41 |
+
|
42 |
+
def inicializar_grafo():
|
43 |
+
df = cargar_desde_airtable()
|
44 |
+
|
45 |
+
# Add base nodes for categories
|
46 |
+
G.add_node("Determinista", color='red')
|
47 |
+
G.add_node("Sist茅mico", color='blue')
|
48 |
+
|
49 |
+
# Process each row and add nodes/edges
|
50 |
+
for _, row in df.iterrows():
|
51 |
+
nombre, enfoque, norma, texto = row["Nombre"], row["Enfoque"], row["Norma"], row["Texto_HF"]
|
52 |
+
textox = wrap_text(f"{nombre}: {texto}")
|
53 |
+
|
54 |
+
if not G.has_node(norma):
|
55 |
+
G.add_node(norma, color='gray')
|
56 |
+
if not G.has_edge(norma, enfoque):
|
57 |
+
G.add_edge(norma, enfoque, label=textox)
|
58 |
|
59 |
def guardar_en_airtable(nombre, enfoque, norma, texto):
|
60 |
+
url = f"https://api.airtable.com/v0/{BASE_ID_2}/{TABLE_NAME_2}"
|
61 |
+
headers = {
|
62 |
+
"Authorization": f"Bearer {AIRTABLE_API_KEY}",
|
63 |
+
"Content-Type": "application/json"
|
64 |
+
}
|
65 |
data = {"fields": {"Nombre": nombre, "Enfoque": enfoque, "Norma": norma, "Texto_HF": texto}}
|
66 |
requests.post(url, headers=headers, json=data)
|
67 |
|
|
|
71 |
def agregar_aporte(nombre, enfoque, norma, texto):
|
72 |
textox = wrap_text(f"{nombre}: {texto}")
|
73 |
|
|
|
74 |
if not G.has_node(norma):
|
75 |
G.add_node(norma, color='gray')
|
76 |
if not G.has_edge(norma, enfoque):
|
77 |
G.add_edge(norma, enfoque, label=textox)
|
78 |
|
79 |
+
guardar_en_airtable(nombre, enfoque, norma, texto)
|
80 |
return visualizar_grafo()
|
81 |
|
82 |
def visualizar_grafo():
|
83 |
plt.figure(figsize=(8, 8))
|
84 |
pos = nx.spring_layout(G)
|
85 |
+
|
|
|
86 |
edge_labels = nx.get_edge_attributes(G, 'label')
|
87 |
|
88 |
nx.draw(G, pos, with_labels=True, node_color='lightblue', edge_color='gray', node_size=2000, font_size=10)
|
89 |
+
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=8)
|
90 |
|
91 |
plt.title("Red de Aportes")
|
92 |
plt.savefig("graph.png")
|
93 |
plt.close()
|
94 |
return "graph.png"
|
95 |
|
96 |
+
inicializar_grafo()
|
97 |
+
|
98 |
iface = gr.Interface(
|
99 |
fn=agregar_aporte,
|
100 |
inputs=["text", "text", "text", "text"],
|
101 |
outputs="image",
|
102 |
+
title="Foro Din谩mico con Visualizaci贸n de Red"
|
103 |
+
)
|
104 |
+
|
105 |
iface.launch(share=True)
|