Padmanabhan
Padmanabhan

Reputation: 158

flask - python How to read a file and output to browser line by line and formats

I am developing a flask-python application. I want to read a file and out put the content with line-breaks and white spaces intact. Below is the code I have now. But it outputs all the lines as a single stream without any line break. What is the correct way to do it?

with open(anupath, 'r')as f:
    content=""
    for line in f:
        content += line + '\n'

Upvotes: 0

Views: 1455

Answers (1)

tzaman
tzaman

Reputation: 47840

Your response is likely being treated as HTML by the browser, which collapses line breaks (among plenty of other things). If you want your file to be displayed as-is, set the parameter mimetype='text/plain' on your flask Response object before returning it:

return Response(content, mimetype='text/plain')

Upvotes: 1

Related Questions