flask rest api tutorial

 flask rest API tutorial, let's start developing rest API using python flask. First of all make sure you have installed python and also flask. Now check if you are already using the flask latest version.


Check flask version you installed using command

flask --version

it will show you the latest version of python and flask. Here mine is Python 3.9.4, flask 2.0.1, and Werkzeug 2.0.1.

Now open an empty folder and open cmd and type code . it will open this folder with vs code, as we are going to use it as our code editor.

Create a new file app.py and get ready seat tight to start writing code with me. Let's make our server running first. Open terminal on vs code from the menu or typing (ctrl + shift + `). When your terminal is open make sure you are on the directory of that folder where you save your app.py file.

Write command on your terminal:

export FLASK_APP=app.py

export FLASK_ENV=development

You are ready to write code on app.py and also ready to run your code.

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "hello"

@app.route('/hello')
def hm():
    return {
        "test":"test"
    }

app.run(port=5000)

Now to run this code use this command on your terminal:

flask run

Enjoy your code is running on this url http://127.0.0.1:5000/ , just visit this from your browser and also don't forget to check the another route you have made http://127.0.0.1:5000/hello it will return test.

Let's move forward to the next part.


Command for postgresql:

python

from app import db

db.create_all()

exit()

0 Comments