Solved: Run a Flask API from CMD

Flask is a lightweight and very adaptable Python web framework that provides a simple template for developing web applications. It is termed as a “micro” framework primarily because it is minimalistic and does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.

If you are thinking about creating a Flask API but uncertain about how to run it from the command line, don’t worry. By end of the article, you will be confidently running your API from CMD.

Python provides several built-in libraries to help with creating APIs in Flask, including flask_restful and flask_sqlalchemy. Using these libraries, you can construct robust APIs with ease.

Running a Flask API from the CommandLine – The Solution

The key to running a Flask API from the CMD is understanding how the Flask environment operates. Flask uses a series of environment variables to control the behavior of your app. The primary one we will be focused on is the FLASK_APP environment variable.

In your terminal, navigate to the directory with your main.py file, and set the FLASK_APP environment variable:

  $ export FLASK_APP=main

This command tells Flask where your application is located. Next, we will start our Flask development server using:

  $ flask run

This will initialize your application and start your server. You’ll be able to access it at http://127.0.0.1:5000/.

Flask Library – An Overview

Flask library provides the necessary functionalities that allow for a sequence of operations to execute when a specific endpoint is hit. Consider the following simple route in a Flask application:

  from flask import Flask
  app = Flask(__name__)

  @app.route("/")
  def hello_world():
      return "Hello, World!"

In this example, @app.route(“/”) is a decorator that Flask provides which tells Flask that the / URI should trigger the hello_world function. Hence, if you run this app and go to http://127.0.0.1:5000/ in your browser, you should see the text “Hello, World!”.

Flask_restful – Creating REST APIs

Flask_restful is an extension for Flask that adds support for quickly building REST APIs. Aiming to simplify the process of building scalable and efficient REST APIs, it encourages best practices with minimal setup.

Here’s a quick introduction on creating a basic ‘Hello World!’ web API using Flask-RESTful.

  from flask import Flask
  from flask_restful import Resource, Api

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

  class HelloWorld(Resource):
      def get(self):
          return {'hello': 'world'}

  api.add_resource(HelloWorld, '/')

  if __name__ == '__main__':
      app.run(debug=True)

In this example, we create a HelloWorld Resource. Resources are built on top of Flask pluggable views, that gives you easy access to multiple HTTP methods just by defining methods on your resource.

Many aspects of developing, implementing, and managing APIs can seem complex, but libraries like Flask and Flask_restful, combined with Python’s straightforward syntax, can make these tasks a lot more manageable. With a clear understanding of how to work with these tools, you can begin to use them to build your APIs and services with confidence.

Related posts:

Leave a Comment