close

How to run a flask application?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to run a flask application in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to run a flask application?

  1. How to run a flask application?

    The flask command is a CLI for interacting with Flask apps. The docs describe how to use CLI commands and add custom commands. The flask run command is the preferred way to start the development server.

  2. run a flask application

    The flask command is a CLI for interacting with Flask apps. The docs describe how to use CLI commands and add custom commands. The flask run command is the preferred way to start the development server.

Method 1

The flask command is a CLI for interacting with Flask apps. The docs describe how to use CLI commands and add custom commands. The flask run command is the preferred way to start the development server.

Use the FLASK_APP environment variable to point the command at your app. Set FLASK_ENV=development to run with the debugger and reloader. Never use this command to deploy publicly, use a production WSGI server such as Gunicorn, uWSGI, Waitress, or mod_wsgi.

$ export FLASK_APP=sample
$ export FLASK_ENV=development
$ flask run

On Windows CMD, use set instead of export.

> set FLASK_APP=sample

For PowerShell, use $env:.

> $env:FLASK_APP = "sample"

The python sample.py command runs a Python file and sets __name__ == "__main__". If the main block calls app.run(), it will run the development server.

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

Method 2


you just need to run this command

python app.py

(app.py is your desire flask file)

but make sure your .py file has the following flask settings(related to port and host)

from flask import Flask, request
from flask_restful import Resource, Api
import sys
import os

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

if sys.argv.__len__() > 1:
    port = sys.argv[1]
print("Api running on port : {} ".format(port))

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

api.add_resource(topic_tags, '/')


if __name__ == '__main__':
    app.run(host="0.0.0.0", port=port)

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read