Science and technology

How to put in writing a Python internet API with Flask

Python is a high-level, object-oriented programming language recognized for its easy syntax. It is constantly among the many top-rated programming languages for constructing RESTful APIs.

Flask is a customizable Python framework that provides builders full management over how customers entry information. Flask is a “micro-framework” based mostly on Werkzeug’s WSGI toolkit and Jinja 2’s templating engine. It is designed as an online framework for RESTful API growth.

Flask is without doubt one of the fastest-growing Python frameworks, and in style web sites, together with Netflix, Pinterest, and LinkedIn, have included Flask into their growth stacks. Here’s an instance of how Flask can allow customers to fetch information from a server utilizing the HTTP GET methodology.

Set up a Flask software

First, create a construction to your Flask software. You can do that at any location in your system.

$ mkdir tutorial
$ cd tutorial
$ contact most
$ python3 -m venv env
$ supply env/bin/activate
(env) $ pip3 set up flask-restful
Collecting flask-restful
Collecting Flask>=Zero.eight (from flask-restful)

Import the Flask modules

Next, import the flask module and its flask_restful library into your most code:

from flask import Flask
from flask_restful import Resource, Api

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

class Quotes(Resource):
    def get(self):

api.add_resource(Quotes, '/')

if __name__ == '__main__':

Run the app

Flask features a built-in HTTP server for testing. Test the straightforward API you constructed:

(env) $ python most
 * Serving Flask app "main" (lazy loading)
 * Environment: manufacturing
   WARNING: This is a growth server. Do not use it in a manufacturing deployment.
   Use a manufacturing WSGI server as a substitute.
 * Debug mode: on
 * Running on (Press CTRL+C to give up)

Starting the event server begins your Flask software, which comprises a technique named get to reply to a easy HTTP GET request. You can take a look at it utilizing wget or curl or any internet browser. The URL to make use of is supplied in Flask’s output after you begin the server.

$ curl http://localhost:5000

To see a extra advanced model of the same internet API utilizing Python and Flask, navigate to the Library of Congress’ Chronicling America web site, which supplies entry to details about historic newspapers and digitized newspaper pages.

Why use Flask?

Flask has a number of main advantages:

  1. Python is in style and broadly used, so anybody who is aware of Python can develop for Flask.
  2. It’s light-weight and minimalistic.
  3. Built with safety in thoughts.
  4. Great documentation with loads of clear, working instance code.

There are additionally some potential drawbacks:

  1. It’s light-weight and minimalistic. If you are searching for a framework with plenty of bundled libraries and prefabricated elements, this might not be the best choice.
  2. If it’s a must to construct your personal framework round Flask, you may discover that the price of sustaining your customization negates the good thing about utilizing Flask.

If you are seeking to construct an online app or API, Flask is an efficient choice to think about. It’s highly effective and strong, and the venture documentation makes it simple to get began. Try it out, consider it, and see if it is proper to your venture.

Most Popular features the latest multimedia technologies, from live video streaming to audio packages to searchable archives of news features and background information. The site is updated continuously throughout the day.

Copyright © 2017 Breaking Express, Green Media Corporation

To Top