I'm writing an API using fastapi in which there is an endpoint for plotting an arbitrary graph. The client posts the graph equation to the server and the server returns the plot. This is my current implementation:
import fastapi
import uvicorn
from sympy import plot, parse_expr
from pydantic import BaseModel
api = fastapi.FastAPI()
class Eq(BaseModel):
eq: str
@api.post('/plot/')
async def plotGraph(eq: Eq):
exp = parse_expr(eq.eq)
p = plot(exp, show=False)
p.save('./plot.png')
return fastapi.responses.FileResponse('./plot.png')
uvicorn.run(api, port=3006, host="127.0.0.1")
The thing is here i'm saving the plot on the hard drive then reading it again using FileResponse
which is kind of redundant.
How to return the underlying image object to the client without the need to writing it to the hard drive first?