jcmachicao commited on
Commit
8b65f87
verified
1 Parent(s): be58766

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -7
app.py CHANGED
@@ -4,13 +4,14 @@ import matplotlib.pyplot as plt
4
  import uuid
5
  import os
6
  import textwrap
 
7
 
8
  api_key = os.getenv("AIRT_KEY")
9
  AIRT_DBASEx = os.getenv("AIRT_DBASE")
10
  AIRT_TABLEx = os.getenv("AIRT_TABLE")
11
 
12
- # Diccionario para almacenar los aportes y sus conexiones
13
- G = nx.Graph()
14
 
15
  def guardar_en_airtable(nombre, enfoque, norma, texto):
16
  url = f"https://api.airtable.com/v0/{AIRT_DBASEx}/{AIRT_TABLEx}"
@@ -26,16 +27,26 @@ def wrap_text(text, width=10):
26
 
27
  def agregar_aporte(nombre, enfoque, norma, texto):
28
  textox = wrap_text(f"{nombre}: {texto}")
29
- G.add_node(norma, color='gray')
30
- G.add_edge(norma, enfoque, label=textox)
31
- guardar_en_airtable(norma, nombre, enfoque, texto)
 
 
 
 
 
32
  return visualizar_grafo()
33
 
34
  def visualizar_grafo():
35
  plt.figure(figsize=(8, 8))
36
  pos = nx.spring_layout(G)
37
- labels = nx.get_node_attributes(G, 'label')
38
- nx.draw(G, pos, with_labels=True, labels=labels, node_color='lightblue', edge_color='gray', node_size=2000, font_size=10)
 
 
 
 
 
39
  plt.title("Red de Aportes")
40
  plt.savefig("graph.png")
41
  plt.close()
@@ -47,3 +58,4 @@ iface = gr.Interface(
47
  outputs="image",
48
  title="Foro Din谩mico con Visualizaci贸n de Red")
49
  iface.launch(share=True)
 
 
4
  import uuid
5
  import os
6
  import textwrap
7
+ import requests # Fix: Import requests
8
 
9
  api_key = os.getenv("AIRT_KEY")
10
  AIRT_DBASEx = os.getenv("AIRT_DBASE")
11
  AIRT_TABLEx = os.getenv("AIRT_TABLE")
12
 
13
+ # Use Directed Graph to represent relationships
14
+ G = nx.DiGraph() # Fix: Use DiGraph instead of Graph
15
 
16
  def guardar_en_airtable(nombre, enfoque, norma, texto):
17
  url = f"https://api.airtable.com/v0/{AIRT_DBASEx}/{AIRT_TABLEx}"
 
27
 
28
  def agregar_aporte(nombre, enfoque, norma, texto):
29
  textox = wrap_text(f"{nombre}: {texto}")
30
+
31
+ # Fix: Prevent duplicate nodes/edges
32
+ if not G.has_node(norma):
33
+ G.add_node(norma, color='gray')
34
+ if not G.has_edge(norma, enfoque):
35
+ G.add_edge(norma, enfoque, label=textox)
36
+
37
+ guardar_en_airtable(nombre, enfoque, norma, texto) # Fix: Correct argument order
38
  return visualizar_grafo()
39
 
40
  def visualizar_grafo():
41
  plt.figure(figsize=(8, 8))
42
  pos = nx.spring_layout(G)
43
+
44
+ # Fix: Use edge labels instead of node labels
45
+ edge_labels = nx.get_edge_attributes(G, 'label')
46
+
47
+ nx.draw(G, pos, with_labels=True, node_color='lightblue', edge_color='gray', node_size=2000, font_size=10)
48
+ nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=8) # Fix: Display correct labels
49
+
50
  plt.title("Red de Aportes")
51
  plt.savefig("graph.png")
52
  plt.close()
 
58
  outputs="image",
59
  title="Foro Din谩mico con Visualizaci贸n de Red")
60
  iface.launch(share=True)
61
+