Activity 49: Python Flask API with SQLALCHEMY integration

Step 3: Create a new fork by having a new repository name

Step 4: After forking it will create a copy in your account

Step 5: Create a folder and name it “act-49

Step 6: launch it on cmd or terminal

Step 7: Clone the forked repository

use this command to clone

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

Step 8: Change the directory

to change directory use this command;

cd = change directory

cd then name of the directory/folder

cd flask-api-with-mysql

Step 9: Create a virtual environment

use this command to create a virtual environment:

python -m venv myvenv

Step 10: Activate virtual environment

to activate the virtual environment, use this command below:

myvenv\Scripts\activate

Once activated, the terminal prompt changes to show the name of the environment (in this case, (myvenv)), indicating that you are now working within the virtual environment.

Step 11: Install the requirements given or dependencies

to install dependencies use this command

pip install -r requirements.txt

the command above will install all the listed dependencies inside in given from the forked repository in requirements.txt

Added venv on gitignore

so, adding the venv directory to .gitignore is essential to prevent the virtual environment files from being tracked by version control tools like Git. Since the venv directory contains environment-specific files that are not necessary for the application’s source code, it is a good practice to exclude it from version control to keep the repository clean and reduce clutter.

followed the steps on how to add venv to the .gitignore file

Steps to add venv to .gitignore, use the command below**:**

nul > .gitignore

Step 12: Open pycharm and open the folder named “flask-api-with-mysql

Step 13: Create a .env file in the root directory of the forked project

Add the following in the .env file;

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

Step 14: Add certificates

  • Certificates are optional if the project or the application requires it (for a secure connection or privacy)

  • on the forked repo cert is already added, therefore we don’t need to copy a certificate (cert) given by aiven

  • in the forked repo, we could see the certs directory/folder which is added by the owner of the project and in there we could see the pasted certificate named “ca.pem

Step 15: Upgrade the database or run the database migration

  • use the following command to run the database migration in terminal or “cmd
flask db upgrade

the command above upgrades or apply the pending imports or migration from the database:

the “flask db upgrade“ uses the .env file to import the pending migrations

Step 15: Run the flask or “run.py

use this command to run “run.py

python run.py

after running the run.py in terminal, copy the link given:

http://127.0.0.1:5000

Step 16: Open postman

create a new collection or folder name it

add a request and name it add user or whatever you want

for us to add a user we’ll use POST method:

paste the given url from the “cmd” or terminal:

http://127.0.0.1:5000

to add a user we’ll use the “/users” then go to body and raw section:

type the necessary data and then “click”:

for us to get the user we’ve added we’ll use GET method using its user_id:

beside the link http://127.0.0.1:5000 with the identifier “/users” we’ll add the user_id given by the added user info;

Create another request in postman:

name it get user:

add the link and the /users with the user_id

in this case the given user_id is 1:

click “send” to get the user info:

there we get the user credentials or info

GitHub repository link (forked): https://github.com/FranciscoJohannes/flask-api-with-mysql