Our flask-api-app is an web based application that contains different API's (Application Programming Interfaces) routes that connects to a database. Users and developers are able to see the data from the database when they accessed a route.
- First, the application contains navigation links to a respective feature.
- Each feature contains its owns logic.
- database.py functions as database models.
- app.py is the main file that runs the entire application, and also for populating basic dummy data.
- Populating data can be done using flask shell, in this case, we used a try-except clauses to catch the error in case data is repeated, so the app does not stop running.
python >= 3.10.x
Download
Any Text Editor of your choice:
- Download and Install python.
- Fork this repository and Clone it from your account. Check here on how to fork. Check Here on how to clone a repository.
cd flask-api-app
and enter root directory of the application.
- Inside root folder. Run
py -m venv .venv
for Windows orpython3 -m venv .venv
for Mac or Linux, to create a .env folder environment. - Activate environment
.\.venv\Scripts\activate
. You should see something like this(.venv) PS C:\Users\defaultUser\Desktop\flask-api-app>
on your terminal. - Once environment is activated you can install the requirements including flask
pip install -r requirements.txt
The app contains 5 folder for each rest api.
- Change directory to your respective rest api folder or create a new one.
- Run
flask --app .\your_file.py run --debug
and start editing the file. - Go to
localhost:5000/your-endpoint-name
and you should see a welcome message.
The app contains app.py file that works as a navigation bar where you can change to url.
- Change directory to root directory.
- Run
flask --app app.py run --debug
and start editing your file. - Go to
localhost:5000
and you should see a welcome message and links to your porject url. - Click your project link button or go to
localhost:5000/your-endpoint-name
and you should see a welcome message.
The app uses Flask-SQLAclhemy to handle the insertion of new tables and their content to a SQLite database. Follow these steps to create new content to the database.
-
Go to database.py in root directory and create a python class with
db.Model
as a parameter. Example:class BookDetails(db.Model):
-
Start adding the table attributes inside the class. Example:
ISBN = db.Column(db.Integer)
book_description = db.Column(db.String)
- You should have something similar to this:
# Databse Model Sample:
class BookDetails(db.Model):
book_name = db.Column(db.String, primary_key=True)
ISBN = db.Column(db.Integer)
book_description = db.Column(db.String)
book_price = db.Column(db.Float)
author = db.Column(db.String)
genre = db.Column(db.String)
year_published = db.Column(db.Integer)
copies_sold = db.Column(db.Integer)
- Add your class name in app.py
from database import db,migrate,BooksTesting, BookDetails,[Your_Class_Name]
- Inside the try clause, pass the parameters needed for your class and save the instance in a new variable.
newInstance = My_Class_Name(passing argumments ....)
- Add the model to the session. This will create a table in the database with all the dummy data.
db.session.add(newInstance)
At this point you should be able to query all data, after importing it in your route using the following sample line:
myData= MyDBModel.query.all()
Flask-Migration was added to the app in case developers want to update their models. In order to do so do the following:
- Install the model if it is not already installed.
pip install -r requirements.txt
- Make the changes in your DB Model class.
- Open a new terminal window and type
flask db migrate -m "explain_your_migration"
- Upgrade your database table
flask db upgrade
- (Optional) Type
flask db --help
to look all the options
- Remember to make the required changes using flask blueprint.
- Check the book_browsing_sorting_api as a guide.
- Remember to register your route in app.py.