]> git.angelumana.com Git - wu-api/.git/commitdiff
Update views and templates to accept and render image input
authoribidyouadu <angel.d.umana@gmail.com>
Sun, 21 Jul 2024 05:24:36 +0000 (00:24 -0500)
committeribidyouadu <angel.d.umana@gmail.com>
Sun, 21 Jul 2024 05:24:36 +0000 (00:24 -0500)
app/main.py
app/templates/index.html
app/templates/result.html

index 1a5cb0fbfb672f0323f209c204ac87981018b3fb..40884936d4a43ea3dcdcf3f0e905356f33f5e868 100644 (file)
@@ -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__":
index 334ae4e98e32de04cd83b0f2c958fce2a7f2232b..205bab14643048653f5113c0172b6152e3fb35f9 100644 (file)
@@ -1,9 +1,10 @@
 <!DOCTYPE html>
 <html><body>
-    <p>Plug in your favorite number and we'll pass it through the useless number transforminator</p>
-    <form action="/transforminate" method="post">
-        <label for="favnum">Your fav number:</label>
-        <input type="number" id="favnum" name="favnum" required>
-        <button type="submit">transforminate!</button>
+    <p>Upload your favorite cloud pic !! ☁️☁️</p>
+    <form action="/result" enctype="multipart/form-data" method="post">
+        <label for="cloud-pic">Upload your cloud pic:</label>
+        <input type="file" id="cloud-pic" name="input_image" accept="image/*" required>
+        </br>
+        <button type="submit">Upload</button>
     </form>
 </body></html>
\ No newline at end of file
index 92222e0a48744f1f4164b6b47e7929fe223c838c..635ff3093bcf533ee4cb2f05ab2dbcdd3be61e30 100644 (file)
@@ -1,5 +1,6 @@
 <!DOCTYPE html>
 <html><body>
-    <p>Behold! Your useless number transforimnator-inated number is {{output}} !</p>
+    <p>Wow what a lovely pic !!!</p>
+    <img src="data:image/jpeg;base64,{{image | safe}}" />
     <a href="/">Do it again, do it again!</a>
 </body></html>
\ No newline at end of file