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

pingwin850
  • 299
  • 1
  • 7
  • 24
  • where you are importing Flask and Api from ? – bhansa Sep 15 '17 at 06: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? – Andrew Che Sep 15 '17 at 06:14
  • Here are my imports: `from flask import Flask, jsonify, request, render_template from flask_restful import Api, Resource from sqlalchemy import create_engine` – pingwin850 Sep 15 '17 at 06:25
  • Actually, I am new to flask and I don't know any other possible ways how to write it – pingwin850 Sep 15 '17 at 06:29

2 Answers2

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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

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

Jay
  • 93
  • 8
  • 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.. – pingwin850 Sep 15 '17 at 08:00
  • You should specify what kind of data is your api returning? is it json or what? – Jay Sep 16 '17 at 05:30
  • My api is returning json – pingwin850 Sep 16 '17 at 06:26
  • the return should be like this ` def get(self): test = { "name": "json2html", "description": "Converts JSON to HTML tabular representation" } return test – Jay Sep 16 '17 at 07:10
  • read the docs for proper application of your code :) [here](http://flask-restful.readthedocs.io/en/latest/) – Jay Sep 16 '17 at 07:12