jcmachicao commited on
Commit
72dbbd7
verified
1 Parent(s): 7c6998d

Create utils_gdmk.py

Browse files
Files changed (1) hide show
  1. utils_gdmk.py +118 -0
utils_gdmk.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import networkx as nx
2
+ import matplotlib.pyplot as plt
3
+ import json
4
+ import textwrap
5
+ import requests
6
+ import pandas as pd
7
+ import os
8
+
9
+ api_key = os.getenv("AIRT_KEY")
10
+ AIRT_DBASEx = os.getenv("AIRT_DBASE")
11
+ AIRT_TABLEx = os.getenv("AIRT_TABLE")
12
+
13
+ G = nx.DiGraph()
14
+
15
+ url = f"https://api.airtable.com/v0/{AIRT_DBASEx}/{AIRT_TABLEx}"
16
+ headers = {
17
+ "Authorization": f"Bearer {api_key}",
18
+ "Content-Type": "application/json"
19
+ }
20
+
21
+ def cargar_normativas():
22
+ with open('normativas.json', 'r', encoding='utf-8') as f:
23
+ return json.load(f)
24
+
25
+ def cargar_estudiantes():
26
+ with open('estudiantes.json', 'r', encoding='utf-8') as f:
27
+ return json.load(f)
28
+
29
+ def crear_html(normativa):
30
+ return f"""
31
+ <div style="padding: 16px; border: 1px solid #ddd; border-radius: 8px;">
32
+ <h2>{normativa["nombre"]}</h2>
33
+ <h3>{normativa["titulo"]} ({normativa["a帽o"]})</h3>
34
+ <p><strong>ID:</strong> {normativa["id"]}</p>
35
+ <p><strong>Descripci贸n:</strong> {normativa["descripcion"]}</p>
36
+ <p><strong>Enfoque de Gesti贸n:</strong> {normativa["enfoque_riesgo"]}</p>
37
+ <p><strong>Opini贸n Docente:</strong> {normativa["opinion_docente"]}</p>
38
+ <p><strong>M谩s informaci贸n: <a href='{normativa["url"]}' target='_blank'>Sitio Web</a></strong></p>
39
+ </div>
40
+ """
41
+
42
+ def mostrar_detalles(normativa_seleccionada):
43
+ if not normativa_seleccionada:
44
+ return "<p>Por favor selecciona una normativa</p>", ""
45
+
46
+ normativas = cargar_normativas()
47
+ id_normativa = normativa_seleccionada
48
+
49
+ for normativa in normativas["normativa_peruana_gestion_riesgos"]:
50
+ if id_normativa == normativa['nombre']:
51
+ return crear_html(normativa), normativa["nombre"]
52
+
53
+ return "<p>No se encontr贸 la normativa</p>", ""
54
+
55
+ def cargar_desde_airtable():
56
+ response = requests.get(url, headers=headers)
57
+
58
+ if response.status_code != 200:
59
+ print(f"Error: {response.status_code} - {response.text}")
60
+ return pd.DataFrame(columns=["Nombre", "Enfoque", "Norma", "Texto_HF"])
61
+
62
+ records = response.json().get("records", [])
63
+
64
+ aportes = [
65
+ [record["fields"].get("Nombre", ""),
66
+ record["fields"].get("Enfoque", ""),
67
+ record["fields"].get("Norma", ""),
68
+ record["fields"].get("Texto_HF", "")] for record in records
69
+ ]
70
+
71
+ return pd.DataFrame(aportes, columns=["Nombre", "Enfoque", "Norma", "Texto_HF"])
72
+
73
+ def wrap_text(text, width=10):
74
+ return "\n".join(textwrap.wrap(text, width=width))
75
+
76
+ def inicializar_grafo():
77
+ df = cargar_desde_airtable()
78
+
79
+ G.add_node("Determinista", color='red')
80
+ G.add_node("Sist茅mico", color='blue')
81
+
82
+ for _, row in df.iterrows():
83
+ nombre, enfoque, norma, texto = row["Nombre"], row["Enfoque"], row["Norma"], row["Texto_HF"]
84
+ textox = wrap_text(f"{nombre}: {texto}")
85
+
86
+ if not G.has_node(norma):
87
+ G.add_node(norma, color='gray')
88
+ if not G.has_edge(norma, enfoque):
89
+ G.add_edge(norma, enfoque, label=textox)
90
+
91
+ def guardar_en_airtable(nombre, enfoque, norma, texto):
92
+ data = {"fields": {"Nombre": nombre, "Enfoque": enfoque, "Norma": norma, "Texto_HF": texto}}
93
+ requests.post(url, headers=headers, json=data)
94
+
95
+ def agregar_aporte(nombre, enfoque, norma, texto):
96
+ textox = wrap_text(f"{nombre}: {texto}")
97
+
98
+ if not G.has_node(norma):
99
+ G.add_node(norma, color='gray')
100
+ if not G.has_edge(norma, enfoque):
101
+ G.add_edge(norma, enfoque, label=textox)
102
+
103
+ guardar_en_airtable(nombre, enfoque, norma, texto)
104
+ return visualizar_grafo()
105
+
106
+ def visualizar_grafo():
107
+ plt.figure(figsize=(8, 8))
108
+ pos = nx.spring_layout(G)
109
+
110
+ edge_labels = nx.get_edge_attributes(G, 'label')
111
+
112
+ nx.draw(G, pos, with_labels=True, node_color='lightblue', edge_color='gray', node_size=2000, font_size=10)
113
+ nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=8)
114
+
115
+ plt.title("Red de Aportes")
116
+ plt.savefig("graph.png")
117
+ plt.close()
118
+ return "graph.png"