Functional example for a process to upload data into a relational database and serve it from a Flask app.
I didn't found documentation about how to user a SQLAlchemy
model from a Flask API while keeping the advantages of using flask-sqlalchemy
to query that model.
Here is what I found out after some reading and trying.
- Docker compose (for the db)
- Python>=3.6
-
Create a virtual environment with python>=3.6
-
Start the database container by running
./commands/run.sh
-
Install requirements
pip install -r requirements.txt
- The example configuration is defined in the
src.core.config.ini
file. The configuration for both parts (loading the database and serving the data) is defined in one file because, although the application was designed to work as decoupled as possible, having different config files is, in my opinion, error prone.
The example comes with a small dataset in src.batch_loader.data.user_data.csv
. In src.batch_loader.data.__init__.py
you will finde a script to generate that dataset. Feel free to edit it, but be aware that altering the script could cause errors if the model is not altered accordingly.
To generate a dataset of the same structure but with different lenghts, run:
python runner.py mock-dataset 1000
being 1000 in this case the number of rows.
- Database data is never dropped on initialization. If the loading script is run more than once with the same data, it will exit with the corresponding exception.
> python runner.py batch
> python runner.py server
/users/
/users/:id
Status Code | Description |
---|---|
200 | Existing users measurement retrieved successfully |
404 | Unable to found requested user |
Issues are welcome :) , both for questions and improvement suggestions.