-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
107 lines (89 loc) · 2.75 KB
/
Copy pathmain.py
File metadata and controls
107 lines (89 loc) · 2.75 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
from datetime import datetime
import pytz
from supabase import create_client
from dotenv import load_dotenv
from fasthtml.common import *
#load environment variables
load_dotenv()
MAX_NAME_CHAR = 15
MAX_MESSAGE_CHAR = 500
TIMESTAMP_FMT = "%Y-%m-%d %H:%M:%S %p IST"
#Initialize Supabase client
supabase = create_client(os.getenv("SUPABASE_URL"), os.getenv("SUPABASE_KEY"))
app,rt = fast_app(
hdrs = (Link(rel="icon", type="assets/x-icon", href="/assets/favicon.png"),),
)
def get_ist_time():
ist_tz = pytz.timezone("Asia/Kolkata")
return datetime.now(ist_tz)
def add_message(name, message):
timestamp = get_ist_time().strftime(TIMESTAMP_FMT)
supabase.table("MyGuestbook").insert(
{"name": name, "message": message, "timestamp": timestamp}
).execute()
def get_messages():
#sort by 'id' in descending order to get the latest entries first
response = (
supabase.table("MyGuestbook")
.select("*")
.order("id", desc=True)
.execute()
)
return response.data
def render_message(entry):
return Article(
Header(f"Name: {entry['name']}"),
P(entry["message"]),
Footer(Small(Em(f"Posted: {entry['timestamp']}"))),
),
def render_message_list():
messages = get_messages()
return Div(
*[render_message(entry) for entry in messages],
id="message-list",
)
def render_content():
form = Form(
Fieldset(
Input(
type="text",
name="name",
placeholder="Name",
required=True,
maxlength=MAX_NAME_CHAR,
),
Input(
type="text",
name="message",
placeholder="Message",
required=True,
maxlength=MAX_MESSAGE_CHAR,
),
Button("Submit", type="submit"),
role="group",
),
method="post",
hx_post="/submit-message", #Send a post request to the /submit-message endpoint
hx_target="#message-list", #only swap the message list
hx_swap="outerHTML", #replace the entire message list
hx_on__after_request="this.reset()", #Reset the form after submission
)
return Div(
P(Em("Write something nice!")),
form,
Div(
"Made with ❤️ by ", # for emojis click(🪟 + .)
A("Karan", herf="https://github.com/karanop001018", target="blank"),
),
Hr(),
render_message_list(),
)
@rt('/')
def get():
return Titled("My GuestBook 📘", render_content())
@rt('/submit-message', methods=["POST"])
def post(name: str, message: str):
add_message(name, message)
return render_message_list()
serve()