Add week 3 materials

This commit is contained in:
2026-09-24 21:25:39 +03:00
parent 25fd4ed555
commit 0a1eb51bf8
18 changed files with 743 additions and 0 deletions
@@ -0,0 +1,3 @@
docker-compose.yaml
Dockerfile
__pycache__
@@ -0,0 +1,12 @@
FROM python:3.11.5-slim-bullseye
WORKDIR /application
# Copy the requirements file and download the dependencies.
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
# Copy other data.
COPY . .
CMD [ "python3", "app.py"]
@@ -0,0 +1,45 @@
from flask import Flask, render_template, request
import logging
import os
import requests
app = Flask(__name__)
app.logger.setLevel(logging.INFO)
APP_VERSION = os.getenv('APP_VERSION')
BIND_HOST = os.getenv('BIND_HOST') or '0.0.0.0'
BIND_PORT = os.getenv('BIND_PORT') or '8000'
@app.before_request
def log_request():
app.logger.info(
'method=%s path=%s host=%s remote_addr=%s',
request.method, request.path, request.host, request.remote_addr,
)
@app.route('/')
def home():
return f'Hello from app {APP_VERSION}'
@app.route('/kittens')
def kittens():
# Get a URL to a random kitten photo.
response = requests.get('https://api.thecatapi.com/v1/images/search?api_')
try:
# Response structure is as follows:
# [{"id":"bL3lrUi1A","url":"ex.com/bL3lrUi1A.jpg","width":1280,"height":720}]
data = response.json()
kitten_url = data[0]['url']
return render_template('index.html', kitten_url=kitten_url)
except Exception as e:
return f'Failed to fetch a kitten image :(\n {e}'
if __name__ == '__main__':
app.run(host=BIND_HOST, port=BIND_PORT)
@@ -0,0 +1,2 @@
Flask==2.3.3
requests==2.28.2
@@ -0,0 +1,8 @@
<html>
<head>
<title>DistSys kittens</title>
</head>
<body>
<img src="{{ kitten_url }}">
</body>
</html>