0

Here's my code written in Python:

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 render_template('index.html', value=test)

api.add_resource(Tracks, '/tracks') 

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

Here's my index.html code:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
    <title>Hello World!</title>
</head>
<body>
    <div>
         <p>
            Hello {{value|safe}}
         </p>
    </div>
</body>
</html>

The problem is that when I run the server and access the appropriate url page I get html code in browser. It looks like this: enter image description here

I'd like it to display the dictionary which I pass. How can I solve it?

This question has been marked as a duplicated one but answers from the given link wasn't useful for my problem. I had to create a new question and here's a solution: RESTful API - display returned json in html table

4
  • where you are importing Flask and Api from ? Commented Sep 15, 2017 at 6:12
  • 1
    Well, you are using an Api object, which is intended to be used for creating an API (Application Programming Interface), hence the name. API is an interface intended to be used by other programs, so there is no surpruse the result you got from it is not exactly human-readable. Why not use Flask app object for human-readable output? Commented Sep 15, 2017 at 6:14
  • Here are my imports: from flask import Flask, jsonify, request, render_template from flask_restful import Api, Resource from sqlalchemy import create_engine Commented Sep 15, 2017 at 6:25
  • Actually, I am new to flask and I don't know any other possible ways how to write it Commented Sep 15, 2017 at 6:29

2 Answers 2

1

The API stuff is for, well, writing an API. You don't need it if you're writing a normal web site.

You should remove the Api wrapper, the Resource inheritance, and the call to add_resoutce.

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

4 Comments

When I do as you said then I can't access my page like this: http://localhost:5002/tracks
I am trying to do a rest API.. e.g. I want to access an appropriate url page and display in a table data from database.
So why are you rendering a template?
Yesterday I just came across to a solution using render template and I thought it would work in my api..
0

get rid of the api. use app function only. your using flask not a restful api. Heres an example from the docs

5 Comments

Actually, I has to be a rest api.. probably render template should be replaced with something different.. but I am new to flask and don't know any other solution.. as I said above I want to access an appropriate url page and display in a table data from database..
You should specify what kind of data is your api returning? is it json or what?
My api is returning json
the return should be like this ` def get(self): test = { "name": "json2html", "description": "Converts JSON to HTML tabular representation" } return test
read the docs for proper application of your code :) here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.