This repository holds the code for part two of the Real Python Python REST APIs With Flask, Connexion, and SQLAlchemy tutorial series.
You should first create a virtual environment:
$ python -m venv venv
$ source venv/bin/activate
Install the pinned dependencies from requirements.txt
:
(venv) $ python -m pip install -r requirements.txt
Then, navigate into the rp_flask_api/
folder:
(venv) $ cd rp_flask_api
(venv) $ python app.py
To see your home page, visit http://127.0.0.1:8000
. You can find the Swagger UI API documentation on http://127.0.0.1:8000/api/ui
.
You can build a SQLite database with content by following the commands below.
Navigate inside the rp_flask_api/
, enter the Python interactive shell and run the commands below:
>>> import sqlite3
>>> conn = sqlite3.connect("people.db")
>>> columns = [
... "id INTEGER PRIMARY KEY",
... "lname VARCHAR UNIQUE",
... "fname VARCHAR",
... "timestamp DATETIME",
... ]
>>> create_table_cmd = f"CREATE TABLE person ({','.join(columns)})"
>>> conn.execute(create_table_cmd)
>>> people = [
... "1, 'Fairy', 'Sugar', '2022-10-08 09:15:10'",
... "2, 'Ruprecht', 'Knecht', '2022-10-08 09:15:13'",
... "3, 'Bunny', 'Easter', '2022-10-08 09:15:27'",
... ]
>>> for person_data in people:
... insert_cmd = f"INSERT INTO person VALUES ({person_data})"
... conn.execute(insert_cmd)
...
<sqlite3.Cursor object at 0x104ac4dc0>
<sqlite3.Cursor object at 0x104ac4f40>
<sqlite3.Cursor object at 0x104ac4fc0>
>>> conn.commit()
This will create a database named people.db
that you can use with your project.
- Philipp Acsany, E-mail: philipp@realpython.com
Distributed under the MIT license. See LICENSE
for more information.