triflix commited on
Commit
2ff7461
Β·
verified Β·
1 Parent(s): cfc21e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -29
app.py CHANGED
@@ -1,8 +1,8 @@
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()
@@ -13,69 +13,69 @@ 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",
 
1
  from fastapi import FastAPI, Request, Form
 
2
  from fastapi.templating import Jinja2Templates
3
+ from fastapi.responses import HTMLResponse
4
  import datetime
5
+ import random
6
  import re
7
 
8
  app = FastAPI()
 
13
  return templates.TemplateResponse("index.html", {"request": request})
14
 
15
  @app.post("/fortune", response_class=HTMLResponse)
16
+ async def get_fortune(
17
  request: Request,
18
  birthday: str = Form(...),
19
  standard: str = Form(...)
20
  ):
21
+ # Validate birthday (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 positive fortunes for each grade (1st to 10th)
31
  fortunes_by_grade = {
32
  "1": [
33
+ "Your smile is the key to unlock a world of coding adventures!",
34
+ "You're just starting out, and the world of code is waiting for you!"
35
  ],
36
  "2": [
37
+ "Every day is a new opportunity to learn something amazing!",
38
+ "Your curiosity will lead you to great coding adventures!"
39
  ],
40
  "3": [
41
+ "Imagine, create, and code – your journey has just begun!",
42
+ "Keep exploring and you'll build wonders with code!"
43
  ],
44
  "4": [
45
+ "Each line of code is a step towards greatness!",
46
+ "Your ideas are as bright as your future in coding!"
47
  ],
48
  "5": [
49
+ "Coding magic happens when you dare to dream big!",
50
+ "You have the power to create amazing things with code!"
51
  ],
52
  "6": [
53
+ "Innovation begins with your brilliant ideas!",
54
+ "Every keystroke brings you closer to a bright future!"
55
  ],
56
  "7": [
57
+ "Your creativity will change the world – keep coding!",
58
+ "Never stop dreaming, coding, and inspiring others!"
59
  ],
60
  "8": [
61
+ "Your passion for learning is the secret to coding success!",
62
+ "Keep coding and let your creativity shine!"
63
  ],
64
  "9": [
65
+ "The digital world awaits your innovative ideas!",
66
+ "Believe in yourself and code on – the future is yours!"
67
  ],
68
  "10": [
69
+ "Your coding skills are your superpower – use them to inspire!",
70
+ "The future is bright when you combine passion with code!"
71
  ],
72
  }
73
 
74
+ # Extract the numeric grade from the standard string (e.g., "1st" -> "1")
75
  grade_match = re.search(r'\d+', standard)
76
  grade = grade_match.group(0) if grade_match else "1"
77
+ messages = fortunes_by_grade.get(grade, ["You're amazing! Keep coding and shining bright!"])
78
+ fortune_message = random.choice(messages)
79
 
80
  return templates.TemplateResponse(
81
  "index.html",