Sanic: Is there any way to send html file as response?

Created on 31 Dec 2016  路  2Comments  路  Source: sanic-org/sanic

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?

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:

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())

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mobdim picture mobdim  路  4Comments

jasonab picture jasonab  路  3Comments

litelife picture litelife  路  3Comments

davidtgq picture davidtgq  路  3Comments

geekpy picture geekpy  路  4Comments