Skip to content

kodi24fever/flask-api-app

Repository files navigation

Welcome to flask-api-app!

How it Works

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.

Required Software

python >= 3.10.x Download

Any Text Editor of your choice:


Installation

  1. Download and Install python.
  2. Fork this repository and Clone it from your account. Check here on how to fork. Check Here on how to clone a repository.
  3. cd flask-api-app and enter root directory of the application.

How to Install .env folder

  1. Inside root folder. Run py -m venv .venv for Windows or python3 -m venv .venv for Mac or Linux, to create a .env folder environment.
  2. Activate environment .\.venv\Scripts\activate. You should see something like this (.venv) PS C:\Users\defaultUser\Desktop\flask-api-app> on your terminal.
  3. Once environment is activated you can install the requirements including flask pip install -r requirements.txt

How to run the app (DEPECRATED)

The app contains 5 folder for each rest api.

  1. Change directory to your respective rest api folder or create a new one.
  2. Run flask --app .\your_file.py run --debug and start editing the file.
  3. Go to localhost:5000/your-endpoint-name and you should see a welcome message.

How to correctly run the app

The app contains app.py file that works as a navigation bar where you can change to url.

  1. Change directory to root directory.
  2. Run flask --app app.py run --debug and start editing your file.
  3. Go to localhost:5000 and you should see a welcome message and links to your porject url.
  4. Click your project link button or go to localhost:5000/your-endpoint-name and you should see a welcome message.

Creating Database Models using Flask-SQLAlchemy or commonly know "DB Tables"

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.

Creating Your First DB Model or Table

  1. Go to database.py in root directory and create a python class with db.Model as a parameter. Example: class BookDetails(db.Model):

  2. Start adding the table attributes inside the class. Example:

ISBN = db.Column(db.Integer)
book_description = db.Column(db.String)
  1. 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)

Inserting Dummy Data

  1. Add your class name in app.py
from  database  import  db,migrate,BooksTesting, BookDetails,[Your_Class_Name]
  1. 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 ....)
  1. Add the model to the session. This will create a table in the database with all the dummy data.
db.session.add(newInstance)

Showing Data

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()

Doing the Migrations

Flask-Migration was added to the app in case developers want to update their models. In order to do so do the following:

  1. Install the model if it is not already installed. pip install -r requirements.txt
  2. Make the changes in your DB Model class.
  3. Open a new terminal window and type flask db migrate -m "explain_your_migration"
  4. Upgrade your database tableflask db upgrade
  5. (Optional) Type flask db --help to look all the options

Important Note related to adding your files to app.py

  1. Remember to make the required changes using flask blueprint.
  2. Check the book_browsing_sorting_api as a guide.
  3. Remember to register your route in app.py.

Happy Coding!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •