1
+ #API
2
+
3
+ import os , sys
4
+ from flask import Flask , render_template , redirect , request , jsonify
5
+ import json
6
+ import pandas as pd
7
+
8
+ # --------------------------
9
+ # $$$$$$$ API (FLASK GET) $$$$$$$$
10
+ # --------------------------
11
+
12
+ # Para obtener la url: ipconfig
13
+ # Para obtener los datos: url:6060/datos?cantidad=número entre 1 y 584
14
+ # Debe devolver el json con el número de refranes/datos que se indique como cantidad
15
+
16
+ app = Flask (__name__ )
17
+
18
+ @app .route ('/' , methods = ['GET' ])
19
+ def default ():
20
+ return '''<h1>API DEL RETO TRIPULACIONES ALZHEIMER</h1>
21
+ <p>Para conseguir el json: url:6060/datos?cantidad=(cantidad de datos a devolver, máximo 585)</p>'''
22
+
23
+ @app .route ('/datos' , methods = ['GET' ])
24
+ def get_json ():
25
+
26
+ #cantidad=None
27
+
28
+ def cargar_datos (num , csv , archivo ):
29
+ df = (pd .read_csv (csv ,sep = ";" ).T ).sample (n = num , axis = 1 )
30
+ df .to_json (archivo + ".json" )
31
+ #datos=os.path.dirname(__file__)+"\\"+archivo+".json"
32
+ datos = archivo + ".json"
33
+ with open (datos ,"r" ) as json_file_readed :
34
+ peticion = json .load (json_file_readed )
35
+ return json .dumps (peticion )
36
+
37
+ if "cantidad" in request .args :
38
+ cant = int (request .args ["cantidad" ])
39
+ return cargar_datos (num = cant , csv = "refranes_manipulado.csv" , archivo = "refranes" )
40
+ else :
41
+ return ("Introduce la cantidad de datos que quieres" )
42
+
43
+
44
+ # -------------------------------
45
+ # $$$$$$$ SERVIDOR FLASK $$$$$$$$
46
+ # -------------------------------
47
+
48
+ def main ():
49
+
50
+ print ("STARTING PROCESS" )
51
+ print (os .path .dirname (__file__ ))
52
+
53
+
54
+ settings_file = os .path .dirname (__file__ ) + "\\ settings.json" # puede que esto sea sin barras
55
+ with open (settings_file , "r" ) as json_file_readed :
56
+ json_readed = json .load (json_file_readed )
57
+
58
+ SERVER_RUNNING = json_readed ["server_running" ]
59
+
60
+ if SERVER_RUNNING :
61
+ DEBUG = json_readed ["debug" ]
62
+ HOST = json_readed ["host" ]
63
+ PORT_NUM = json_readed ["port" ]
64
+ app .run (debug = DEBUG , host = HOST , port = PORT_NUM )
65
+ else :
66
+ print ("Server settings.json doesn't allow to start server. " +
67
+ "Please, allow it to run it." )
68
+
69
+ if __name__ == "__main__" :
70
+ main ()
0 commit comments