5

I'm looking to return a multi-line string as part of a REST Get request.

I have the following list:

items = [1,2,3,4,5]

I want to convert it into a string with the line breaks in between. So i did this:

items = "\n".join(items)

Here is the code block I have:

from flask import Flask, jsonify, request, Response, make_response

app = Flask(__name__)

@app.route('/test', methods=['GET'])
def test():
   items = [1,2,3,4,5]
   items = "\n".join(items)
   return items

The response that I get is:

1 2 3 4 5

However, I'm expecting:

1
2
3
4
5

What can I do to fix this?

kryogenic1
  • 166
  • 1
  • 2
  • 15

3 Answers3

3

using this

items = "<br/>".join(map(str, items))

should work. Basically html will ignore new lines as such.That's what <br/> is for

ted
  • 13,596
  • 9
  • 65
  • 107
0

This should work:

Inserting a "br" tag will only display results as new lines while parsing in flask

from flask import Flask, jsonify, request, Response, make_response

app = Flask(__name__)

@app.route('/test', methods=['GET'])
def test():
   items = [1,2,3,4,5]
   items2 = '<br>'.join(str(e) for e in items)
   return items2

Output:

1
2
3
4
5
Karthick Mohanraj
  • 1,565
  • 2
  • 13
  • 28
0

this is the code it works

from flask import Flask
app = Flask(__name__)
@app.route("/")
def test():
    items = ["1","2","3","4","5"]
    items = "<br />".join(items)
    return items

if __name__ =="__main__":
    app.run()
Laurence
  • 1
  • 1