Hi , first thank for all your work on SANIC
I want a return a BIG "file" which is in reality a StringIO
but I get TypeError: expected str, bytes or os.PathLike object, not _io.StringIO
@app.route('/test')
async def test(request):
strIO = StringIO()
strIO.write('Hello from Dan Jacob and Stephane Wirtel !')
strIO.seek(0)
return await response.file_stream(strIO)
am I doing an error or StringIO is not compatible with SANIC ?
thank you
it expects first param to be Location of file on system.
http://sanic.readthedocs.io/en/latest/sanic/api_reference.html#sanic.response.file_stream
https://github.com/channelcat/sanic/blob/master/sanic/response.py#L274
Thank you , could I propose a PR for that pls ?
@raphaelauv - You should check out the sanic.response.stream function.
@app.route("/")`
async def test(request):
output = io.StringIO()
output.write('First, line.\n')
output.write('First, line.\n')
output.write('First, line.\n')
output.seek(0)
async def sample_streaming_fn(response):
while True:
rst = output.readline()
if not rst:
break
else:
response.write(rst)
return stream(sample_streaming_fn, content_type='text/csv')
Yes it work , sorry for that boring question
Thank you !
For someone who came here after trying to return _io.BytesIO as a file response (i.e 1st argument to response.file) and getting an error saying - TypeError: expected str, bytes or os.PathLike object, not _io.BytesIO.
Just use response.raw(_io.BytesIO, headers={}) and it'll work. (Setting headers is optional, obviously)
Most helpful comment
For someone who came here after trying to return _io.BytesIO as a file response (i.e 1st argument to response.file) and getting an error saying -
TypeError: expected str, bytes or os.PathLike object, not _io.BytesIO.Just use
response.raw(_io.BytesIO, headers={})and it'll work. (Setting headers is optional, obviously)