File size: 6,032 Bytes
32e73ba
 
d5b8b50
32e73ba
 
d5b8b50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32e73ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5b8b50
32e73ba
 
 
 
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
from fasthtml.common import *
from faq import create_faq
import pandas as pd
from fasthtml_hf import setup_hf_backup

df = pd.read_csv("./ar_en_nutrition.csv")
from food_sql import create_food_model, update_database

app, rt, foods, Food = create_food_model(df)

try:
    app, rt, foods, Food = create_food_model(df)
    update_database(df)
    print("Database updated successfully!")
except Exception as e:
    print(f"Error updating database: {e}")

items = foods()
print(len(items))

for item in items[:5]:
    print(item.arabic_name, item.name, item.calories)

el = foods.search("banana")
for e in el:
    print(e["name"])
    print(e["arabic_name"])
    print(e["calories"])
    print(e)
    break

seo_meta = [
    Meta(
        name="description",
        content="كم كالوري أفضل موقع لمعرفة عدد السعرات الحرارية في الأطعمة  باستخدام البحث الذكي وتقنيات الذكاء الإصطناعي",
    ),
    Meta(name="keywords", content="سعرات حرارية, غذاء, صحة, تغذية, حمية"),
    Meta(name="author", content="Abdelkareem Elkhateb"),
    Meta(property="og:title", content="كم كالوري - حاسبة السعرات الحرارية"),
    Meta(property="og:description", content="احسب السعرات الحرارية في طعامك بسهولة"),
    Meta(property="og:type", content="website"),
    Meta(name="robots", content="index, follow"),
    Meta(name="language", content="ar"),
    Meta(charset="UTF-8"),
    # Meta(property="og:url", content="your-website-url"),
    # Meta(property="og:image", content="url-to-your-logo"),
    Meta(name="twitter:card", content="summary"),
    Meta(name="twitter:title", content="كم كالوري - حاسبة السعرات الحرارية"),
    Meta(name="twitter:description", content="احسب السعرات الحرارية في طعامك بسهولة"),
    # Meta(name="twitter:image", content="url-to-your-logo"),
    # Additional SEO
    Meta(name="theme-color", content="#4a90e2"),
    Meta(name="application-name", content="كم كالوري"),
    Meta(property="og:locale", content="ar_AR"),
    Meta(name="viewport", content="width=device-width, initial-scale=1.0"),
]
app, rt, foods, Food = fast_app(
    "data/foods.db",
    hdrs=[
        *seo_meta,
        Style("""
    :root { 
        --pico-font-size: 100%;
        --pico-color-info: #4a90e2;
    }
    body {
        direction: rtl;
        text-align: right;
    }
    .navbar {
        background-color: var(--pico-color-info);
        padding: 1rem;
        margin-bottom: 2rem;
    }
    .navbar a {
        color: white;
        text-decoration: none;
    }
    table a {
        text-decoration: none;
    }
"""),
    ],
    id=int,
    name=str,
    serving_size=str,
    calories=int,
    total_fat=str,
    saturated_fat=str,
    cholesterol=str,
    sodium=str,
    choline=str,
    folate=str,
    folic_acid=str,
    niacin=str,
    pantothenic_acid=str,
    riboflavin=str,
    thiamin=str,
    vitamin_a=str,
    vitamin_a_rae=str,
    carotene_alpha=str,
    carotene_beta=str,
    cryptoxanthin_beta=str,
    lutein_zeaxanthin=str,
    lucopene=int,
    vitamin_b12=str,
    vitamin_b6=str,
    vitamin_c=str,
    vitamin_d=str,
    vitamin_e=str,
    tocopherol_alpha=str,
    vitamin_k=str,
    calcium=str,
    copper=str,
    irom=str,
    magnesium=str,
    manganese=str,
    phosphorous=str,
    potassium=str,
    selenium=str,
    zink=str,
    protein=str,
    alanine=str,
    arginine=str,
    aspartic_acid=str,
    cystine=str,
    glutamic_acid=str,
    glycine=str,
    histidine=str,
    hydroxyproline=str,
    isoleucine=str,
    leucine=str,
    lysine=str,
    methionine=str,
    phenylalanine=str,
    proline=str,
    serine=str,
    threonine=str,
    tryptophan=str,
    tyrosine=str,
    valine=str,
    carbohydrate=str,
    fiber=str,
    sugars=str,
    fructose=str,
    galactose=str,
    glucose=str,
    lactose=str,
    maltose=str,
    sucrose=str,
    fat=str,
    saturated_fatty_acids=str,
    monounsaturated_fatty_acids=str,
    polyunsaturated_fatty_acids=str,
    fatty_acids_total_trans=str,
    alcohol=str,
    ash=str,
    caffeine=str,
    theobromine=str,
    water=str,
    arabic_name=str,
    pk="id",
)


def mk_search_input():
    return Input(
        id="search-input", name="query", placeholder="ابحث عن الطعام..", required=True
    )


@rt("/search")
def post(query: str):
    results = foods.search(query, limit=20)
    header = Thead(
        Tr(
            *map(Th, ["الاسم بالعربية", "Food", "السعرات الحرارية"]),
        ),
        cls="bg-purple/10",
    )

    rows = [
        Tr(
            Td(
                AX(
                    f["arabic_name"],
                    f"/food/{f['id']}",
                    "results-table",
                    hx_push_url="true",
                )
            ),
            Td(f["name"]),
            Td(f["calories"]),
            cls="even:bg-purple/5",
        )
        for f in results
    ]
    return Table(header, *rows, cls="w-full", id="results-table")


@rt("/food/{id}")
def get(id: int):
    food = foods.get(id)
    return Table(
        *[Tr(Th(k), Td(v)) for k, v in food.__dict__.items() if k != "id"],
        id="results-table",
    )


@rt("/")
def get():
    header = Header(
        Nav(
            Ul(
                Li(A("البحث الذكي", href="/")),
            ),
            cls="container",
        ),
        cls="navbar",
    )
    search_form = Form(
        Group(mk_search_input(), Button("Search")),
        hx_post="/search",
        target_id="results-table",
    )
    return (
        Title("كم كالوري"),
        header,
        Main(
            H1("كم كالوري في .."),
            search_form,
            Div(id="results-table"),
            create_faq(),
            cls="container",
        ),
    )


setup_hf_backup(app)

if __name__ == "__main__":
    serve()