Update main.py
Browse files
main.py
CHANGED
|
@@ -1,18 +1,15 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from argopy import DataFetcher
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
import matplotlib
|
| 7 |
|
| 8 |
-
# Set MPLCONFIGDIR to a writable directory
|
| 9 |
-
os.environ['MPLCONFIGDIR'] = '/tmp/matplotlib_config'
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
@app.get("/plot_trajectory")
|
| 14 |
def plot_trajectory(lon_min: float, lon_max: float, lat_min: float, lat_max: float,
|
| 15 |
-
depth_min:
|
| 16 |
date_start: str, date_end: str):
|
| 17 |
try:
|
| 18 |
# Create data fetcher with user inputs
|
|
@@ -21,20 +18,17 @@ def plot_trajectory(lon_min: float, lon_max: float, lat_min: float, lat_max: flo
|
|
| 21 |
# Plotting
|
| 22 |
plt.figure()
|
| 23 |
f.plot('trajectory', add_legend=False)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
file_path = "/path/to/public/directory/" + filename # Adjust this path
|
| 28 |
-
plt.savefig(file_path)
|
| 29 |
plt.close()
|
| 30 |
|
| 31 |
-
|
| 32 |
-
public_url = "http://yourserver.com/" + filename # Replace with your server's URL
|
| 33 |
|
| 34 |
-
return {"image_url": public_url}
|
| 35 |
except Exception as e:
|
| 36 |
raise HTTPException(status_code=500, detail=str(e))
|
| 37 |
|
|
|
|
| 38 |
if __name__ == "__main__":
|
| 39 |
import uvicorn
|
| 40 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from argopy import DataFetcher
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
+
from fastapi.responses import StreamingResponse
|
| 5 |
+
import io
|
|
|
|
| 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,
|
| 13 |
date_start: str, date_end: str):
|
| 14 |
try:
|
| 15 |
# Create data fetcher with user inputs
|
|
|
|
| 18 |
# Plotting
|
| 19 |
plt.figure()
|
| 20 |
f.plot('trajectory', add_legend=False)
|
| 21 |
+
buf = io.BytesIO()
|
| 22 |
+
plt.savefig(buf, format='png')
|
| 23 |
+
buf.seek(0)
|
|
|
|
|
|
|
| 24 |
plt.close()
|
| 25 |
|
| 26 |
+
return StreamingResponse(buf, media_type="image/png")
|
|
|
|
| 27 |
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
raise HTTPException(status_code=500, detail=str(e))
|
| 30 |
|
| 31 |
+
|
| 32 |
if __name__ == "__main__":
|
| 33 |
import uvicorn
|
| 34 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|