Skip to content

getting started learning Flask for python web development

Notifications You must be signed in to change notification settings

developingAlex/first-flask-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple python flask API

Install flask

Use a virtual environment for the app so that it can maintain its own libraries without worrying about versions.

Prerequisites

  • sudo apt install python3-venv
  • python installed

Walkthrough

(Record of the steps I took)

  1. Make a new virtual environment in a directory called flask-env:
    python3 -m venv flask-env

  2. Activate the environment:
    chmod u+x flask-env/bin/activate
    source flask-env/bin/activate

  3. pip install Flask

    • This gave a red warning output:
      Failed building wheel for MarkupSafe
    • This gave a warning output:
      You are using pip version 8.1.1, however version 10.0.0 is available.
      You should consider upgrading via the 'pip install --upgrade pip' command.
  4. pip install --upgrade pip

  5. Wasting a lot of time trying to investigate this Failed building wheel for MarkupSafe issue.. the main parts of the error output are:
    Running setup.py bdist_wheel for itsdangerous ... error

    error: invalid command 'bdist_wheel'

    Failed building wheel for itsdangerous

    Running setup.py bdist_wheel for MarkupSafe ... error

    error: invalid command 'bdist_wheel'

    Failed building wheel for MarkupSafe

    Looks like a possible solution is to just run:
    pip install wheel

    Now running:
    pip uninstall Flask
    pip install Flask

    But that seemed to complete way too quickly, as if it didn’t retry all the things it did the first time.

    going to try doing the same for the packages that produced the errors originally:

    pip uninstall Flask
    pip uninstall itsdangerous
    pip uninstall MarkupSafe
    pip install MarkupSafe
    pip install itsdangerous
    pip install Flask

    Ok going to assume that is sorted out now and continue on…

  6. Created a file index.py to be the main server app with following code:

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route("/")
    def hello():
    return "hello world"
    
    if __name__ == '__main__':
        app.run(debug=True)
  7. Executing python index.py in the terminal and then visit localhost:5000 in the browser

  8. The line @app.route("/") just above the hello function is a python function decorator, basically it means that flask's route("/") function is being called and is also being passed our hello function. Link to more on decorators

  9. Now to see an example of a CRUD app done in Flask
    The following was taken from the tutorial:
    https://medium.com/python-pandemonium/build-simple-restful-api-with-python-and-flask-part-2-724ebf04d12

  10. The following code is the CRUD app that only handles one type, users, save it as crud.py:

    from flask import Flask, request, jsonify
    from flask_sqlalchemy import SQLAlchemy
    from flask_marshmallow import Marshmallow
    import os
    
    app = Flask(__name__)
    basedir = os.path.abspath(os.path.dirname(__file__))
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'crud.sqlite')
    db = SQLAlchemy(app)
    ma = Marshmallow(app)
    
    
    class User(db.Model):
        id = db.Column(db.Integer, primary_key=True)
        username = db.Column(db.String(80), unique=True)
        email = db.Column(db.String(120), unique=True)
    
        def __init__(self, username, email):
            self.username = username
            self.email = email
    
    
    class UserSchema(ma.Schema):
        class Meta:
            # Fields to expose, normally you wouldn't reveal the id
            fields = ('username', 'email', 'id')
    
    
    user_schema = UserSchema()
    users_schema = UserSchema(many=True)
    
    
    # endpoint to create new user
    @app.route("/user", methods=["POST"])
    def add_user():
        username = request.json['username']
        email = request.json['email']
        
        new_user = User(username, email)
        db.session.add(new_user)
        db.session.commit()
        return user_schema.jsonify(new_user)
    
    
    # endpoint to show all users
    @app.route("/user", methods=["GET"])
    def get_user():
        all_users = User.query.all()
        result = users_schema.dump(all_users)
        return jsonify(result.data)
    
    
    # endpoint to get user detail by id
    @app.route("/user/<id>", methods=["GET"])
    def user_detail(id):
        user = User.query.get(id)
        return user_schema.jsonify(user)
    
    
    # endpoint to update user
    @app.route("/user/<id>", methods=["PUT"])
    def user_update(id):
        user = User.query.get(id)
        username = request.json['username']
        email = request.json['email']
    
        user.email = email
        user.username = username
    
        db.session.commit()
        return user_schema.jsonify(user)
    
    
    # endpoint to delete user
    @app.route("/user/<id>", methods=["DELETE"])
    def user_delete(id):
        user = User.query.get(id)
        db.session.delete(user)
        db.session.commit()
    
        return user_schema.jsonify(user)
    
    
    if __name__ == '__main__':
        app.run(debug=True)
  11. Run your crud.py:

    1. ensure you're running in your appropriate virtual environment:
      1. From the project directory:
        source flask-env/bin/activate
      2. You should see your terminal indicate that you're operating in a different environment to your normal one (snip is vscode's terminal):
        activated-env-indication
    2. run the server with the command:
      python crud.py
    3. Use a RESTful client (such as Postman) to craft and send the request to the server to create a new user:
      readme-assets/postman-create-user

About

getting started learning Flask for python web development

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages