Skip to main content

Command Palette

Search for a command to run...

Activity 49: Python Flask API with SQLALCHEMY integration

Updated
3 min readView as Markdown

Step 1: Fork the Repository

  1. Go to the GitHub repository: Visit the repository thirdygayares/restaurant_menu on GitHub.

  2. Fork the Repository:

    • On the top-right corner of the repository page, click the Fork button.

    • Choose your GitHub account to fork the repository to.


Step 2: Clone Your Repository

  1. After forking the repository, clone it to your local machine.

  2. Open a terminal or command prompt, and run the following command (replace your-username with your GitHub username):

     git clone https://github.com/JovRoncal/flask-api-with-mysql
    

  3. Navigate to the project directory:

     cd <project-name>
    


Step 3: Set Up the Project

A. Create a Virtual Environment

  1. In the terminal, create a new virtual environment. This is essential to isolate your project's dependencies from the global Python environment.

    For macOS/Linux:

     python -m venv venv
    

    For Windows:

     python -m venv venv
    

  2. Activate the virtual environment:

    For Windows:

     venv\Scripts\activate
    

B. Install Requirements

  1. With the virtual environment activated, install the necessary dependencies using the requirements.txt file.

     pip install -r requirements.txt
    

C. Add .env File

  1. Create a .env file in the root of the project directory.

  2. Add the following environment variables to the .env file:

     FLASK_ENV=development
     DATABASE_URL=mysql://avnadmin:{password}@{host}:{port}/{databasename1}
     SECRET_KEY=your_secret_key
    

    • Replace {password}, {host}, {port}, and {databasename1} with the appropriate values from your Aiven MySQL database.

    • SECRET_KEY should be a secret string (you can generate one randomly).

D. Copy Certs to app/certs

  1. Copy any required certificate files (if needed) to the app/certs directory.

E. Run Database Migrations

  1. In the terminal, navigate to the project directory and run:

     flask db upgrade
    

    This will apply any database migrations to set up the tables required by the application.

F. Run the Application

  1. To run the application, execute the following command:

     python run.py
    

    This will start the Flask application on http://127.0.0.1:5000/.


Step 4: Create a Database in Aiven

  1. Log into your Aiven account: Go to the Aiven dashboard at Aiven.

  2. Create a New Database:

    • Choose the MySQL service.

    • Enter a database name (e.g., databasename1).

    • Follow the Aiven setup process to complete the database creation.

  3. Get Database Connection Details:

    • After setting up the database, you’ll get a connection URL in the format:

        mysql://avnadmin:{password}@{host}:{port}/{databasename1}
      
    • Use this connection URL in your .env file.


Step 5: Code Explanation and Postman Testing

Code Overview

  1. run.py: This file runs the Flask application and serves it on the specified port (5000 by default).

  2. Database Models: The code likely uses SQLAlchemy for database interaction. The models define the structure of tables and relationships in the database (e.g., User model).

  3. API Routes: Routes are defined in app/routes.py or similar, handling HTTP requests like GET, POST, PUT, etc., for interacting with the User resource.


Postman Testing

  1. Create User in Postman:

    • Open Postman and create a new POST request.

    • Set the URL to http://127.0.0.1:5000/users.

    • In the "Body" tab, select raw and choose JSON as the format.

    • Add the user data (similar to the example above) and send the request.

    • If successful, you should get a response with the created user’s data, including their ID.

          {
            "username": "roncal",
            "email": "roncal@gmail.com",
            "password": "ramonjov123",
             "role": "user"
          }
      

  2. Get User by ID in Postman:

    • Create a new GET request.

    • Set the URL to http://127.0.0.1:5000/users/4 (or another ID you want to query).

    • Send the request and verify that you get the user details for the given ID.

https://github.com/JovRoncal/Activity-49-Python-SQLAlchemy

T

Try all endpoint please

More from this blog

Jov's Blog

48 posts