Skip to main content
flask-using-python3

Getting Started with Python Flask

in this python tutorial, I will explore How to create first project using flask framework with python 3.The Flask is a popular Python web framework.

A framework is a code library that makes a developer’s life easier to create reliable, scalable, and maintainable web applications. He is providing reusable code or extensions for common operations. You can also read How To Consume Slack API Using Python and Flask.

What’s Flask Framework

Flask is a web application framework written in Python. Flask is based on the Werkzeug WSGI toolkit and Jinja2 template engine. Both are Pocco projects.

  • It’s easy to set up.
  • It’s supported by an active community.
  • It’s well documented.
  • It’s very simple and minimalistic.

Simple Example of Python flask

I will create simple hello rest api using python 3 and flask. We will create /python-flask-example folder and created server.py file here. I will write all the code into this file.

How To Install Python Flask

We will use python pip package manager to install flask.

pip install flask

Let’s create hello rest end point using flask framework, Added below code into the server.py file.

from flask import Flask
from flask_restful import Resource, Api
from flask_cors import CORS
import requests

app = Flask(__name__)
CORS(app) ## To allow direct AJAX calls

@app.route('/hello', methods=['GET'])
def home():
   try:        
    return 'Hello World'
   except Exception as e:
    print('unable to get data.')

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

Now run above python flask application –
python-flask-example$ py server.py --host=0.0.0.0 --port=3010

Then go to the https://localhost:3010/hello url, you will seeing your first webpage displaying “Hello World!”.

Flask Route Path parameters

We can also pass parameters into the flask route. Like I need to pass name into to the route path variable, created route as follows –

@app.route('/hello/', methods=['GET'])
def home(name):
   try:     
    return 'Hello World %s!' % name
   except Exception as e:
    print('unable to get data.')

Then go to the https://localhost:3010/hello/john url, you will seeing your first webpage displaying “Hello World john!”.

Leave a Reply

Your email address will not be published. Required fields are marked *