-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (63 loc) · 2.4 KB
/
main.py
File metadata and controls
78 lines (63 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# main.py
import random
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from faker import Faker
# Initialize FastAPI
app = FastAPI()
# Initialize Faker
fake = Faker()
# Jinja2 templates directory
templates = Jinja2Templates(directory="templates")
def generate_profile():
"""
Generate a random user profile with consistent gender using Faker
and a random avatar from RandomUser.me.
Note: Faker generates gender-specific names, but emails are not guaranteed
to match the gender. In this implementation, emails are still random.
Hopefully, Faker will add gender-consistent emails in the future!
Returns a dictionary containing:
- name: gender-consistent full name
- email: (currently random) email
- phone: random phone number
- address: random address (newlines replaced with commas)
- job: random job title
- avatar: URL of a random gender-consistent avatar (0-99)
- gender: 'male' or 'female'
"""
gender = random.choice(["male", "female"])
# Use Faker's profile to get gender-consistent name and email
if gender == "male":
profile = fake.profile(sex='M')
avatar_gender = "men"
else:
profile = fake.profile(sex='F')
avatar_gender = "women"
# Random avatar from randomuser.me (0-99)
avatar_id = random.randint(0, 99)
avatar_url = f"https://randomuser.me/api/portraits/{avatar_gender}/{avatar_id}.jpg"
return {
"name": profile['name'], # gender-consistent name
"email": profile['mail'], # currently random email
"phone": fake.phone_number(),
"address": fake.address().replace("\n", ", "),
"job": fake.job(),
"avatar": avatar_url,
"gender": gender,
}
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
"""
Render the main page with an initial random profile.
"""
profile = generate_profile()
return templates.TemplateResponse("index.html", {"request": request, "profile": profile})
@app.get("/profile", response_class=HTMLResponse)
async def get_profile(request: Request):
"""
Return only the profile card HTML (HTMX partial).
Used when refreshing profile without reloading the page.
"""
profile = generate_profile()
return templates.TemplateResponse("profile_partial.html", {"request": request, "profile": profile})