22
33import pandas as pd
44from flask import Flask , jsonify
5+ from tabulate import tabulate
56
67app = Flask (__name__ )
78number_stations = pd .read_csv ("./data/number_stations.csv" )
@@ -30,6 +31,8 @@ def station_names():
3031def is_active_station ():
3132 # Pass the parameter from the URL and check if it matches against an active station
3233 active_df = number_stations [number_stations ['Status' ] == 'Active' ]
34+ # Use tabulate to print the response to the terminal
35+ print (tabulate (active_df , tablefmt = 'simple' , headers = ["ID" , "Name" , "Country" , "Active Counterparts" , "Inactive Counterparts" , "Nickname" , "Status" , "Frequency" , "Voice" , "Emission Mode" , "Location" ], showindex = 'never' ))
3336 r = active_df .to_json ()
3437 return json .loads (r )
3538
@@ -39,6 +42,8 @@ def is_active_station():
3942def is_inactive_station ():
4043 # Pass the parameter from the URL and check if it matches against an inactive station
4144 inactive_df = number_stations [number_stations ['Status' ] == 'Inactive' ]
45+ # Use tabulate to print the response to the terminal
46+ print (tabulate (inactive_df , tablefmt = 'simple' , headers = ["ID" , "Name" , "Country" , "Active Counterparts" , "Inactive Counterparts" , "Nickname" , "Status" , "Frequency" , "Voice" , "Emission Mode" , "Location" ], showindex = 'never' ))
4247 r = inactive_df .to_json ()
4348 return json .loads (r )
4449
@@ -50,6 +55,8 @@ def filer_station_names(name):
5055 return jsonify ({ 'error' : 'Station name cannot be blank' })
5156 # Pass the parameter from the URL and check if it matches against a known station name
5257 f_name = number_stations [number_stations ['Name' ] == name ]
58+ # Use tabulate to print the response to the terminal
59+ print (tabulate (f_name , tablefmt = 'simple' , headers = ["ID" , "Name" , "Country" , "Active Counterparts" , "Inactive Counterparts" , "Nickname" , "Status" , "Frequency" , "Voice" , "Emission Mode" , "Location" ], showindex = 'never' ))
5360 # If the DataFrame returns empty, send an error back to the client
5461 if f_name .empty :
5562 return jsonify ({ 'error' : 'That station does not exist' })
@@ -66,6 +73,8 @@ def filer_station_nickname(nickname):
6673 return jsonify ({ 'error' : 'Station nickname cannot be blank' })
6774 # Pass the parameter from the URL and check if it matches against a known station nickname
6875 f_nickname = number_stations [number_stations ['Nickname' ].str .contains (nickname )]
76+ # Use tabulate to print the response to the terminal
77+ print (tabulate (f_nickname , tablefmt = 'simple' , headers = ["ID" , "Name" , "Country" , "Active Counterparts" , "Inactive Counterparts" , "Nickname" , "Status" , "Frequency" , "Voice" , "Emission Mode" , "Location" ], showindex = 'never' ))
6978 # If the DataFrame returns empty, send an error back to the client
7079 if f_nickname .empty :
7180 return jsonify ({ 'error' : 'That station does not exist' })
@@ -82,6 +91,8 @@ def filter_by_id(id):
8291 return jsonify ({ 'error' : 'ID cannot be empty' })
8392 # Pass the parameter from the URL and check if it matches against a known station id
8493 f_id = number_stations [number_stations ['ID' ] == id ]
94+ # Use tabulate to print the response to the terminal
95+ print (tabulate (f_id , tablefmt = 'simple' , headers = ["ID" , "Name" , "Country" , "Active Counterparts" , "Inactive Counterparts" , "Nickname" , "Status" , "Frequency" , "Voice" , "Emission Mode" , "Location" ], showindex = 'never' ))
8596 # If the DataFrame returns empty, send an error back to the client
8697 if f_id .empty :
8798 return jsonify ({ 'error' : 'That ID does not exist' })
@@ -94,11 +105,12 @@ def filter_by_id(id):
94105# Filter by station locations
95106@app .route ('/filter_by_location/<loc>' )
96107def filter_by_location (loc ):
97- print (loc )
98108 if loc is None :
99109 return jsonify ({ 'error' : 'Location cannot be empty' })
100110 # Pass the parameter from the URL and check if it matches against a known location - 'na=False' ignores any possibly empty values in the column to avoid throwing an error
101111 f_loc = number_stations [number_stations ['Location' ].str .contains (loc , na = False )]
112+ # Use tabulate to print the response to the terminal
113+ print (tabulate (f_loc , tablefmt = 'simple' , headers = ["ID" , "Name" , "Country" , "Active Counterparts" , "Inactive Counterparts" , "Nickname" , "Status" , "Frequency" , "Voice" , "Emission Mode" , "Location" ], showindex = 'never' ))
102114 # If the DataFrame returns empty, send an error back to the client
103115 if f_loc .empty :
104116 return jsonify ({ 'error' : 'That location does not exist' })
0 commit comments