diff options
| author | Dylan Bolger <dylan.bolger00@gmail.com> | 2026-08-18 11:44:36 -0500 |
|---|---|---|
| committer | Dylan Bolger <dylan.bolger00@gmail.com> | 2026-08-18 11:44:36 -0500 |
| commit | 04892822306bf9a01de19cc70bcfc59d7fca9f8f (patch) | |
| tree | fcc8bc92c5a15b669a657561dfe2ac7e14c72857 /app | |
| parent | eead346519758f45dd587cfd471f90f6a6a8037f (diff) | |
| download | photoblog-04892822306bf9a01de19cc70bcfc59d7fca9f8f.tar.xz photoblog-04892822306bf9a01de19cc70bcfc59d7fca9f8f.zip | |
move from fastapi to flask, various cleanups involved
Diffstat (limited to 'app')
| -rw-r--r-- | app/app.py | 6 | ||||
| -rw-r--r-- | app/blueprints/__init__.py (renamed from app/routers/__init__.py) | 0 | ||||
| -rw-r--r-- | app/blueprints/web.py | 9 | ||||
| -rw-r--r-- | app/main.py | 24 | ||||
| -rw-r--r-- | app/routers/web.py | 9 | ||||
| -rw-r--r-- | app/services/__pycache__/__init__.cpython-314.pyc | bin | 157 -> 0 bytes | |||
| -rw-r--r-- | app/services/__pycache__/images.cpython-314.pyc | bin | 1250 -> 0 bytes | |||
| -rw-r--r-- | app/services/images.py | 18 | ||||
| -rw-r--r-- | app/templates/image.html | 9 | ||||
| -rw-r--r-- | app/templates/index.html | 6 |
10 files changed, 26 insertions, 55 deletions
diff --git a/app/app.py b/app/app.py new file mode 100644 index 0000000..8f2896c --- /dev/null +++ b/app/app.py @@ -0,0 +1,6 @@ +from flask import Flask + +from .blueprints import web + +app = Flask(__name__) +app.register_blueprint(web.web_controller)
\ No newline at end of file diff --git a/app/routers/__init__.py b/app/blueprints/__init__.py index e69de29..e69de29 100644 --- a/app/routers/__init__.py +++ b/app/blueprints/__init__.py diff --git a/app/blueprints/web.py b/app/blueprints/web.py new file mode 100644 index 0000000..f4e2949 --- /dev/null +++ b/app/blueprints/web.py @@ -0,0 +1,9 @@ +from flask import Blueprint, render_template, current_app +from ..services import images + +web_controller = Blueprint('web', __name__, template_folder='templates') + +@web_controller.route('/') +def main_page(): + imgs = images.get_all_images(current_app) + return render_template('index.html', images=imgs)
\ No newline at end of file diff --git a/app/main.py b/app/main.py deleted file mode 100644 index b6adaad..0000000 --- a/app/main.py +++ /dev/null @@ -1,24 +0,0 @@ -from fastapi import FastAPI -from fastapi.staticfiles import StaticFiles -from fastapi.templating import Jinja2Templates -from fastapi.middleware.cors import CORSMiddleware -from .routers import web -from .services import images - -app = FastAPI( - docs_url=None, - redoc_url=None, - openapi_url=None - ) - -origins = ["https://dbolger.dev"] - -app.add_middleware(CORSMiddleware, allow_origins=origins, allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"]) - -app.state.image_service = images -app.state.templates = Jinja2Templates(directory="./app/templates") -app.include_router(web.router) -app.mount("/static", StaticFiles(directory="./app/static"), name="static") -app.mount("/photos", StaticFiles(directory=images.photos_dir), name="photos")
\ No newline at end of file diff --git a/app/routers/web.py b/app/routers/web.py deleted file mode 100644 index 678c63e..0000000 --- a/app/routers/web.py +++ /dev/null @@ -1,9 +0,0 @@ -from fastapi import APIRouter, Request - -router = APIRouter() - -@router.get("/") -async def main_page(request: Request): - images = request.app.state.image_service.get_all_images() - return request.app.state.templates.TemplateResponse( - request=request, name="index.html", context={"images": images})
\ No newline at end of file diff --git a/app/services/__pycache__/__init__.cpython-314.pyc b/app/services/__pycache__/__init__.cpython-314.pyc Binary files differdeleted file mode 100644 index b31d35f..0000000 --- a/app/services/__pycache__/__init__.cpython-314.pyc +++ /dev/null diff --git a/app/services/__pycache__/images.cpython-314.pyc b/app/services/__pycache__/images.cpython-314.pyc Binary files differdeleted file mode 100644 index 2514767..0000000 --- a/app/services/__pycache__/images.cpython-314.pyc +++ /dev/null diff --git a/app/services/images.py b/app/services/images.py index 3ce8045..4938014 100644 --- a/app/services/images.py +++ b/app/services/images.py @@ -1,21 +1,19 @@ from pathlib import Path +from flask import Flask, current_app import random -print(str(Path.cwd())) -root_dir = Path.cwd() -photos_dir = Path(f"{root_dir}photos") - image_cache = set() -def cache_images(): +def cache_images(app: Flask): + photos_dir = Path(f"{app.static_folder}/photos") for file_path in photos_dir.rglob("*"): if file_path.is_file(): - image_path = file_path.relative_to(root_dir).name - image_cache.add(str(image_path)) + img_path = file_path.relative_to(app.static_folder) + image_cache.add(str(img_path)) -def get_all_images(): +def get_all_images(current_app: Flask): # if not image_cache: - cache_images() - print(f"returning {len(image_cache)} images") + cache_images(current_app) + print(f"Returning {len(image_cache)} images") shuffled = random.sample(list(image_cache), k=len(image_cache)) return shuffled diff --git a/app/templates/image.html b/app/templates/image.html deleted file mode 100644 index c070faf..0000000 --- a/app/templates/image.html +++ /dev/null @@ -1,9 +0,0 @@ -<html> -<head> - <title>Item Details</title> - <link href="{{ url_for('static', path='/styles.css') }}" rel="stylesheet"> -</head> -<body> - <h1><a href="{{ url_for('read_item', id=id) }}">Item ID: {{ id }}</a></h1> -</body> -</html>
\ No newline at end of file diff --git a/app/templates/index.html b/app/templates/index.html index 991088a..a489c5e 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -6,7 +6,7 @@ <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://dbolger.dev/assets/css/main.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"> - <link rel="stylesheet" href="{{ url_for('static', path='index.css') }}"> + <link rel="stylesheet" href="{{ url_for('static', filename='index.css') }}"> <title>Dylan's Photo Blog</title> </head> @@ -31,8 +31,8 @@ <main class="gallery"> {% for img in images %} <div class="gallery-item"> - <a href="{{ url_for('photos', path=img) }}"> - <img src="{{ url_for('photos', path=img) }}" alt="" loading="lazy"> + <a href="{{ url_for('static', filename=img) }}"> + <img src="{{ url_for('static', filename=img) }}" alt="" loading="lazy"> </a> </div> {% endfor %} |
