This Flask API serves data from a SQLite database containing information about employees of a fictional company. The API allows for basic CRUD (Create, Read, Update, Delete) operations on the employee data, and also provides additional endpoints for data analysis purposes.
The Flask API has the following endpoints:
GET /employees
: Returns a list of all employees in the database.GET /employees/<int:id>
: Returns the employee with the specified ID.POST /employees
: Creates a new employee with the specified data (name, department, salary, hire_date). The API returns the ID of the newly created employee.PUT /employees/<int:id>
: Updates the employee with the specified ID with the specified data (name, department, salary, hire_date).DELETE /employees/<int:id>
: Deletes the employee with the specified ID.GET /departments
: Returns a list of all unique departments in the database.GET /departments/<string:name>
: Returns a list of all employees in the specified department.GET /average_salary/<string:department>
: Returns the average salary of employees in the specified department.GET /top_earners
: Returns a list of the top 10 earners in the company based on their salary.GET /most_recent_hires
: Returns a list of the 10 most recently hired employees.POST /predict_salary
: Takes in data for a new employee (department and hire date) and returns the predicted salary.
flask generate-employees --count 1000
: run this command to generate employees usingfaker
flask train-salary-model
: run this command to train salary prediction model
The database is generated using the SQLAlchemy library and contains a table called "employees
" with the following columns:
id
: an auto-incrementing integer and primary keyname
: a string with a maximum length of 50 charactersdepartment
: a string with a maximum length of 50 characterssalary
: a float with a minimum value of 0 and maximum value of 1000000hire_date
: a datetime object in the format of 'YYYY-MM-DD HH:MM:SS', with a range from 01-01-2020 00:00:00 to today.
- Clone the repository:
git clone https://github.com/s3m3dov/flask-employee-api.git
- Set-up poetry
poetry env use python3.10
poetry install
- Run the development server
poetry run flask run
- Run tests
poetry run pytest app/tests
- Generate fake data for employees
poetry run flask generate-employees --count 1000
The API documentation is available at:
- Swagger UI:
http://localhost:5000/api/swagger
- Redoc UI:
http://localhost:5000/api/redoc
- Rapidoc UI:
http://localhost:5000/api/rapidoc
- Python 3.10
Flask
: a micro web framework for Python used to build the API endpointsFlask-Smorest
: an extension for Flask that simplifies the creation of RESTful APIsMarshmallow
: a Python library for serializing and deserializing data, which is used for validating input and output data in the APIFlask-SQLAlchemy
: an extension for Flask that adds support for SQLAlchemy, a SQL toolkit and ORM, which is used for communicating with the SQLite databaseSQLAlchemy-Utils
: a library that provides various utility functions for working with SQLAlchemyFlask-Migrate
: an extension for Flask that handles SQLAlchemy database migrationsFlask-Testing
: an extension for Flask that provides utilities for testing the APIpandas
: a library for data manipulation and analysis, used for loading and preprocessing data for the salary prediction model.scikit-learn
: a popular machine learning library for Python, used for building and training the salary prediction modelFaker
: a library for generating fake data, used for populating the SQLite database with random employee datapytest
: a framework for testing Python codecolorlog
: a library that provides colored logs