Flask jsonify print results on new lines

Viewed 4654

First time using Flask, I have created a very basic app and I am trying to print the results of a recommender system. The first set of code is from my python function (print_most_similar) and is creating a formatted string in hopes to print every REC on a new line. The second section of code is obviously my flask routing. You can see that the flask part calls the function, so it is returned 'y'. I believe the jsonify will not take the \n characters. I have tried using just '\n' in the string formatting, but it just appears as a string. As does '\t'.

for k in range(len(sugg)):
    x = str("REC {}: {}\\n".format(k+1, sugg[k]))
    y += x
return y

@app.route("/getrecomm",methods=['GET','POST'])
def getrecomm():
    restname = request.args.get('restname', type=str)
    number = request.args.get('number', type=int)
    i = getBusIndex(restname, names)
return make_response(jsonify(result=(print_most_similar(rating, names, i, number))),200)

Currently, the results print like this: REC 1: Harbor House Cafe & Lounge\nREC 2: Starbucks\nREC 3: McDonald's\nREC 4: Taco Bell\nREC 5: Panda Express\n

I would like them to print like this: REC 1: Harbor House Cafe & Lounge REC 2: Starbucks REC 3: McDonald's REC 4: Taco Bell REC 5: Panda Express

I'm using python 3, fyi. Any suggestions would be super appreciated!

2 Answers

Finally I have found the solutions.

It seems like that jsonify() function doesn't apply the "new lines" situation.

You can use Response()

from flask import Flask, Response

statement = """
        try
        try
        try
        """
@app.route('/**/api/v1/**', methods=['GET'])
def get_statement():
    return Response(statement, mimetype='text/plain')

I'm also new learner for flask, Response()function works on my app.

Related