diff --git a/src/backend/api.py b/src/backend/api.py index 00460b6..bae46a0 100644 --- a/src/backend/api.py +++ b/src/backend/api.py @@ -170,15 +170,15 @@ def gather_data(lat, long, ocean): ocean["ocean_data"] = ocean_data ocean["uv_index"] = uv_index spot_forecast = forecast(lat, long, ocean["decimal"], 7) - json_forecast = helper.forecast_to_json(spot_forecast) + json_forecast = helper.forecast_to_json(spot_forecast, ocean["decimal"]) data_dict = { - "Location : ": ocean["city"], - "Height: ": ocean_data[0], - "Direction: ": ocean_data[1], - "Period: ": ocean_data[2], - "UV Index: ": uv_index, - "Forecast: " : json_forecast + "Location": ocean["city"], + "Height": ocean_data[0], + "Direction": ocean_data[1], + "Period": ocean_data[2], + "UV Index": uv_index, + "Forecast": json_forecast } return ocean_data, uv_index, data_dict diff --git a/src/backend/helper.py b/src/backend/helper.py index 3ba8a72..d18bb1b 100644 --- a/src/backend/helper.py +++ b/src/backend/helper.py @@ -173,9 +173,9 @@ def json_output(data_dict): """ If JSON=TRUE in .args, we print and return the JSON data """ - json_output = json.dumps(data_dict, indent=4) - print(json_output) - return json_output + json_out = json.dumps(data_dict, indent=4) + print(json_out) + return data_dict def print_outputs(lat, long, coordinates, ocean_data, arguments): """ @@ -203,7 +203,7 @@ def set_location(location): lat, long = location["lat"], location["long"] return coordinates, city, lat, long -def forecast_to_json(data): +def forecast_to_json(data, decimal): """ Takes forecast() as input and returns it in JSON format """ @@ -214,13 +214,10 @@ def forecast_to_json(data): for i in range(len(dates)): forecast = { "date": str(dates[i].date()), - "surf height": surf_height[i], - "swell direction": swell_direction[i], - "swell period": swell_period[i] + "surf height": round(float(surf_height[i]), decimal), + "swell direction": round(float(swell_direction[i]), decimal), + "swell period": round(float(swell_period[i]), decimal) } forecasts.append(forecast) - - output = {"forecasts": forecasts} - # Converting to JSON string - output_json = json.dumps(str(output), indent=4) - return output_json \ No newline at end of file + + return forecasts \ No newline at end of file diff --git a/src/backend/server.py b/src/backend/server.py index 43d5236..d68bdbe 100644 --- a/src/backend/server.py +++ b/src/backend/server.py @@ -50,13 +50,19 @@ def default_route(): args = ','.join(parsed_parameters) async def run_subprocess(): - result = subprocess.run( - ["python3", "main.py", args], - capture_output=True, - text=True, - check=True, - ) - return result.stdout + try: + result = subprocess.run( + ["python3", "main.py", args], + capture_output=True, + text=True, + check=True, + ) + return result.stdout + except subprocess.CalledProcessError as e: + # Print the error message from the subprocess + print("Error message from subprocess:", e.stderr) + # Raise the error again to propagate it + raise e # Run subprocess asynchronously loop = asyncio.new_event_loop()