j-brady / dataset-experiments

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Readme

Building a simple API using dataset, pydantic and FastAPI

dataset library used to build an sqlite3 database and FastAPI used to provide a simple API for queries. pydantic is used to perform data validation.

A simple example database is created which stores .png images as base64 strings (to be rendered by browser) along with a zip archive (stored as LargeBinary) which can be downloaded.

Installation

Using Makefile

make install

Runs the following commands to create a virtual environment:

python3.10 -m venv venv
source venv/bin/activate && pip3 install --upgrade pip && pip install dataset pillow fastapi "uvicorn[standard]" pydantic ipython jinja2

Then creates some dummy data:

python db.py

Then starts a uvicorn server:

uvicorn app:app --reload

Now you can go to localhost and play around

make open

Will open a sample query in firefox (probably need to use a separate shell to run this).

Examples

http://localhost:8000/table/id/gt/15 will query the table named table and return data for id’s greater than 15. The equivalent SQL query would be SELECT * FROM 'table' WHERE id > 15.

http://localhost:8000/table/name/like/%25am%25 will query table named table and return all values for the column name that satisfy %am% pattern.

http://localhost:8000/table/find/name/ham will find all rows with the value ham in the name column.

http://localhost:8000/table/find/data/between/?mini=10&maxi=20 will query data column of table for values between 10 and 20.

Todo

  • allow for arbitrary SQL queries to be submitted
  • re-organize (package, rather than Makefile)

About


Languages

Language:Python 61.6%Language:HTML 31.9%Language:Makefile 6.5%