This demo uses SQLModel and htmx to generate an infinite scroll of videos of a YouTube videos.
Make a virtual environment and install the requirements:
$ make install
python3.9 -m venv venv && source venv/bin/activate && pip install -r requirements-dev.txt
Then go to Google Cloud Platform > YouTube Data API v3 and hit the blue ENABLE button, then generate your API key:
Next set up your .env
file:
cp .env-example .env
Populate it with the following environment variables:
-
YT_CHANNEL
: the YouTube channel to use this on, to try it out with PyBites you can useUCBn-uKDGsRBfcB0lQeOB_gA
or for PyCon useUCMjMBMGt0WJQLeluw6qNJuA
(demo below). -
YOUTUBE_API_KEY
: the API key you just created. -
DATABASE_URL
: create a database and point to it (in case ofsqlite
all you need to do is specify one like this:sqlite:///db.sqlite3
)
Next let's cache the YouTube channel's data (all videos' metadata) to not exhaust the YouTube API rate limit.
Run the following command with your virtual environment enabled (this example uses PyCon's YouTube channel):
$ make dbsetup
source venv/bin/activate && python -m youtube.db
Total records: 172 (newly inserted: 172)
Result:
youtube-import.mp4
Lastly run the app:
make run
Navigate to localhost:8000 and you should see something like this:
inifnite-scroll.mp4
There are unit and functional tests for this project.
The unit tests use "cassettes" (cached API responses) so they are fast:
yi-infinite-scroll-unit-test.mp4
The functional (end-to-end) test uses the real DB that is configured in .env
and Selenium to scroll to the bottom of the infinite scroll, then compare the amount of table rows on the page vs the amount of entries in the DB. This test requires FastAPI to be running.
So in terminal 1 run:
make run
Then in terminal 2 run the test:
yi-infinite-scroll-functional-test.cmproj.mp4
Of course you can also run all tests in one go with make test
.