Update main.py
Browse files
main.py
CHANGED
|
@@ -1,12 +1,20 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from argopy import DataFetcher
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
@app.get("/plot_trajectory")
|
| 11 |
def plot_trajectory(lon_min: float, lon_max: float, lat_min: float, lat_max: float,
|
| 12 |
depth_min: float, depth_max: float,
|
|
@@ -18,17 +26,18 @@ def plot_trajectory(lon_min: float, lon_max: float, lat_min: float, lat_max: flo
|
|
| 18 |
# Plotting
|
| 19 |
plt.figure()
|
| 20 |
f.plot('trajectory', add_legend=False)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
plt.close()
|
| 25 |
-
StreamingResponse(buf, media_type="image/png")
|
| 26 |
-
|
| 27 |
-
image_url = f"{base_url}plot_trajectory?lon_min={lon_min}&lon_max={lon_max}&lat_min={lat_min}&lat_max={lat_max}&depth_min={depth_min}&depth_max={depth_max}&date_start={date_start}&date_end={date_end}"
|
| 28 |
|
|
|
|
|
|
|
| 29 |
|
| 30 |
return {"image_url": image_url}
|
| 31 |
-
|
| 32 |
except Exception as e:
|
| 33 |
raise HTTPException(status_code=500, detail=str(e))
|
| 34 |
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from argopy import DataFetcher
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
+
import uuid
|
| 5 |
+
import os
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# Directory where images will be saved
|
| 10 |
+
# This directory must be publicly accessible through your web server
|
| 11 |
+
image_directory = "/images"
|
| 12 |
+
os.makedirs(image_directory, exist_ok=True)
|
| 13 |
+
|
| 14 |
+
# Base URL for the images
|
| 15 |
+
# Replace with your server's base URL
|
| 16 |
+
base_url = "https://zliang-argoapi.hf.space/images"
|
| 17 |
+
|
| 18 |
@app.get("/plot_trajectory")
|
| 19 |
def plot_trajectory(lon_min: float, lon_max: float, lat_min: float, lat_max: float,
|
| 20 |
depth_min: float, depth_max: float,
|
|
|
|
| 26 |
# Plotting
|
| 27 |
plt.figure()
|
| 28 |
f.plot('trajectory', add_legend=False)
|
| 29 |
+
|
| 30 |
+
# Save the plot to a file with a unique name
|
| 31 |
+
filename = str(uuid.uuid4()) + ".png"
|
| 32 |
+
file_path = os.path.join(image_directory, filename)
|
| 33 |
+
plt.savefig(file_path)
|
| 34 |
plt.close()
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Construct the URL for the saved image
|
| 37 |
+
image_url = f"{base_url}/{filename}"
|
| 38 |
|
| 39 |
return {"image_url": image_url}
|
| 40 |
+
|
| 41 |
except Exception as e:
|
| 42 |
raise HTTPException(status_code=500, detail=str(e))
|
| 43 |
|