From 4d8e8c7f00e67b1c22df5b7b78c1743e4b692165 Mon Sep 17 00:00:00 2001 From: ibidyouadu Date: Sun, 21 Jul 2024 00:24:36 -0500 Subject: [PATCH] Update views and templates to accept and render image input --- app/main.py | 16 ++++++++++------ app/templates/index.html | 11 ++++++----- app/templates/result.html | 3 ++- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/app/main.py b/app/main.py index 1a5cb0f..4088493 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,9 @@ -from fastapi import FastAPI, Request, Form +from fastapi import FastAPI, Request, UploadFile, File, BackgroundTasks from fastapi.responses import HTMLResponse from fastapi.templating import Jinja2Templates +from tempfile import NamedTemporaryFile +import os +import base64 app = FastAPI() templates = Jinja2Templates("templates") @@ -12,12 +15,13 @@ def index(request: Request): return response -@app.post("/transforminate") -def transforminate(request: Request, favnum: float = Form(...)): - output = favnum/3.14159 - context = {"request": request, "output": output} +@app.post("/result") +def show_image(request: Request, background_tasks: BackgroundTasks, input_image: UploadFile = File(...)): + contents = input_image.file.read() + encoded_image = base64.b64encode(contents).decode("utf-8") + + context = {"request": request, "image": encoded_image} response = templates.TemplateResponse("result.html", context) - return response if __name__ == "__main__": diff --git a/app/templates/index.html b/app/templates/index.html index 334ae4e..205bab1 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -1,9 +1,10 @@ -

Plug in your favorite number and we'll pass it through the useless number transforminator

-
- - - +

Upload your favorite cloud pic !! ☁️☁️

+ + + +
+
\ No newline at end of file diff --git a/app/templates/result.html b/app/templates/result.html index 92222e0..635ff30 100644 --- a/app/templates/result.html +++ b/app/templates/result.html @@ -1,5 +1,6 @@ -

Behold! Your useless number transforimnator-inated number is {{output}} !

+

Wow what a lovely pic !!!

+ Do it again, do it again! \ No newline at end of file -- 2.43.0