Update Meme Generator project

This commit is contained in:
2026-01-04 14:03:10 -08:00
parent 155f0c9c6d
commit 433f2ba034
9 changed files with 66 additions and 28 deletions

View File

@@ -1,3 +1,5 @@
"""Flask app to generate memes."""
import random
import os
import requests
@@ -6,17 +8,17 @@ from flask import Flask, render_template, abort, request
from QuoteEngine import Ingestor
from MemeEngine import MemeEngine
# Create the Flask application object. "__name__" tells Flask where to find templates and static files.
# Create the Flask application object. "__name__" tells Flask,
# where to find templates and static files.
app = Flask(__name__)
# Create a global MemeEngine instance that will write generated memes into "./static"
# so that the images can be served by the web server.
# Create a global MemeEngine instance that will write generated memes
# into "./static" so that the images can be served by the web server.
meme = MemeEngine("./static")
def setup():
"""Load all resources"""
"""Load all resources."""
quote_files = [
"./_data/DogQuotes/DogQuotesTXT.txt",
"./_data/DogQuotes/DogQuotesDOCX.docx",
@@ -45,7 +47,7 @@ quotes, imgs = setup()
@app.route("/")
def meme_rand():
"""Generate a random meme
"""Generate a random meme.
Steps:
- Pick a random image from imgs
@@ -63,7 +65,7 @@ def meme_rand():
@app.route("/create", methods=["GET"])
def meme_form():
"""User input for meme information
"""User input for meme information.
This route renders a form where the user can input:
- image_url: URL of the source image
@@ -75,7 +77,7 @@ def meme_form():
@app.route("/create", methods=["POST"])
def meme_post():
"""Create a user defined meme
"""Create a user defined meme.
This route:
- Reads form data sent via POST from the meme_form page
@@ -95,8 +97,8 @@ def meme_post():
tmp_file = f"tmp/{random.randint(0, 10000)}.jpg"
with open(tmp_file, "wb") as file:
file.write(image.content)
except:
print("Failed to generate meme")
except OSError as e:
print("Failed to generate meme:", e)
path = None
if os.path.exists(tmp_file):
os.remove(tmp_file)