Update app.py
Browse files
app.py
CHANGED
|
@@ -1 +1,88 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, Form
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from fastapi.templating import Jinja2Templates
|
| 4 |
+
import random
|
| 5 |
+
import datetime
|
| 6 |
+
import re
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
templates = Jinja2Templates(directory="templates")
|
| 10 |
+
|
| 11 |
+
@app.get("/", response_class=HTMLResponse)
|
| 12 |
+
async def read_index(request: Request):
|
| 13 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
| 14 |
+
|
| 15 |
+
@app.post("/fortune", response_class=HTMLResponse)
|
| 16 |
+
async def fortune(
|
| 17 |
+
request: Request,
|
| 18 |
+
birthday: str = Form(...),
|
| 19 |
+
standard: str = Form(...)
|
| 20 |
+
):
|
| 21 |
+
# Parse the birthday string (expected format: YYYY-MM-DD)
|
| 22 |
+
try:
|
| 23 |
+
birth_date = datetime.datetime.strptime(birthday, "%Y-%m-%d").date()
|
| 24 |
+
except Exception:
|
| 25 |
+
return templates.TemplateResponse(
|
| 26 |
+
"index.html",
|
| 27 |
+
{"request": request, "error": "Invalid date format. Please use YYYY-MM-DD."}
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Define fortunes tailored by grade (standard)
|
| 31 |
+
fortunes_by_grade = {
|
| 32 |
+
"1": [
|
| 33 |
+
"You're a bright little star, ready to explore the wonders of code!",
|
| 34 |
+
"Your curiosity is the key to unlocking coding adventures!"
|
| 35 |
+
],
|
| 36 |
+
"2": [
|
| 37 |
+
"Your imagination can build magical programs!",
|
| 38 |
+
"Coding is like storytelling – you are the hero!"
|
| 39 |
+
],
|
| 40 |
+
"3": [
|
| 41 |
+
"Every great coder started as a beginner – keep exploring!",
|
| 42 |
+
"Your mind is a playground for brilliant ideas!"
|
| 43 |
+
],
|
| 44 |
+
"4": [
|
| 45 |
+
"Coding is an adventure waiting for you. Embrace it!",
|
| 46 |
+
"Your ideas are the seeds for a tech revolution!"
|
| 47 |
+
],
|
| 48 |
+
"5": [
|
| 49 |
+
"You're a creative coder in the making. Shine on!",
|
| 50 |
+
"Every line of code is a step toward greatness!"
|
| 51 |
+
],
|
| 52 |
+
"6": [
|
| 53 |
+
"The world of coding is open to your limitless ideas!",
|
| 54 |
+
"Innovation starts with you – code your dreams!"
|
| 55 |
+
],
|
| 56 |
+
"7": [
|
| 57 |
+
"You're a tech explorer – dive into the world of coding!",
|
| 58 |
+
"Let your creativity flow like endless code possibilities!"
|
| 59 |
+
],
|
| 60 |
+
"8": [
|
| 61 |
+
"Coding is the language of the future, and you're fluent in innovation!",
|
| 62 |
+
"Your future is as bright as your code!"
|
| 63 |
+
],
|
| 64 |
+
"9": [
|
| 65 |
+
"Your mind is a powerhouse of coding potential!",
|
| 66 |
+
"Great things are coming your way with every line of code!"
|
| 67 |
+
],
|
| 68 |
+
"10": [
|
| 69 |
+
"The digital world awaits your brilliance – code on!",
|
| 70 |
+
"You're a trailblazer in the making; let your code inspire the world!"
|
| 71 |
+
],
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
# Extract grade number from the standard (e.g., "1st" -> "1")
|
| 75 |
+
grade_match = re.search(r'\d+', standard)
|
| 76 |
+
grade = grade_match.group(0) if grade_match else "1"
|
| 77 |
+
fortunes = fortunes_by_grade.get(grade, ["You're amazing and have a bright future in coding!"])
|
| 78 |
+
fortune_message = random.choice(fortunes)
|
| 79 |
+
|
| 80 |
+
return templates.TemplateResponse(
|
| 81 |
+
"index.html",
|
| 82 |
+
{
|
| 83 |
+
"request": request,
|
| 84 |
+
"fortune": fortune_message,
|
| 85 |
+
"birthday": birthday,
|
| 86 |
+
"standard": standard
|
| 87 |
+
}
|
| 88 |
+
)
|