1

I'm trying to do a restful api in flask and python. Here's my code:

from flask import Flask, jsonify, request, render_template
from flask_restful import Api, Resource

app = Flask(__name__)
api = Api(app)

class Tracks(Resource):
    @app.route('/')
    def get(self):
        test = {
            "name": "json2html",
            "description": "Converts JSON to HTML tabular representation"
        }
        return jsonify(test)

api.add_resource(Tracks, '/tracks') 

if __name__ == '__main__':
    app.run(port='5002')

My question is how should I modify it to be able to display my returned value in table and add e.g. some CSS. Thanks!

1 Answer 1

2

in your application return the following:

return Response(render_template('test.html', result=test, mimetype='text/html'))

and in test.html

<!DOCTYPE html>
<html>
<head></head>
<body>
    <table>
        {% for key, value in result.iteritems() %}

        <tr>
            <th> {{ key }} </th>
            <td> {{ value }} </td>
        </tr>
        {% endfor %}
    </table>
</body>
</html>

this was my output: enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I did as you said but I receive the html code written in browser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.