I could not find any example in docs that send a html file as response. for now I wrote a funtion that do it for me:
def send_file(file_path):
with open(file_path, 'r') as f:
return html(f.read())
I wonder if there is a way to do this that I'm not aware of?
There is no way to have sanic open the file and read it right now. It could be worth adding.
Regarding your function, I would just read the html file directly without the context manager:
def send_file(file_path):
return open(file_path).read()
Or do that in your route:
from sanic import Sanic
from sanic.response import html
app = Sanic()
@app.route('/')
async def test(request):
return html(open(file_path).read())
Closing per @r0fls awesome response.
Most helpful comment
There is no way to have sanic open the file and read it right now. It could be worth adding.
Regarding your function, I would just read the html file directly without the context manager:
Or do that in your route: