0

I am a beginner in python web frameworks and i have created a simple Python-Flask RESTful API which enables me to get infromation from a database using GET request but i have a problem in how can i use this API with any website python based frameworks because i know each framework has it's own way to conenct Urls with the code so it will not be just add the .py file to the website files and invoke it from the Url like php, so how can i use this flask APi in any website with python framework like (Django, Flask, ..etc)? Thanks.

This is my simple API:

from flask import Flask, request
from flask_restful import Resource, Api
from sqlalchemy import create_engine
from json import dumps
from flask_jsonpify import jsonify
import MySQLdb
app = Flask(__name__)
api = Api(app)
db = MySQLdb.connect("localhost", "root", "root", "x")

class macros(Resource):
    def get(self):
        cursor = db.cursor()        
        cursor.execute("select * from survey_macro")  # This line will be changes from website to another      
        return jsonify({'macros':[i for i in cursor.fetchall()]})  

api.add_resource(macros, '/macros')  # Route_1

if __name__ == '__main__':
    app.run(port='5002')
2
  • Do you want to add this API to another project or access this API from another Python project? Commented Oct 12, 2017 at 9:12
  • I want to add this appi to another python project and it may be written in Django or any web framework.@JRajan Commented Oct 12, 2017 at 9:16

1 Answer 1

1

If you have a flask website you can just add that code to an existing file and it will work but it is better to use blueprints. Blueprints allow you to separate your code into smaller independent applications so for example you have one blueprint for one part of your website and another for your API.

If you have a django website you can write your API in django-restful. It is better to use just one framework then to have one part of the application written in one framework and another in a second.

One more option is to make your API a separate website and just send requests to it from any other website.

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

5 Comments

Ok, but what if i have a Django website or another framework how can i add that api code file?
I don't have experience with that so I didn't answer for django or any other framework. Sorry. If you want to use django it would be maybe better to use django-restful and write your API in that then to have one part of the application written in django and another in flask.
Ok, thanks for your response but i want to use this api with my clients websites so i want to make it working with any python framework.
The API can be a different website, and you can send requests to him from any other site :)
to use this code in flask should i include it in an existing file.py or put in a new file and push it in the website files will work?

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.