Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
C
CSC190 Group 6
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Imam M. Jah
CSC190 Group 6
Commits
06a465d4
Commit
06a465d4
authored
Oct 21, 2024
by
Imam M. Jah
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
homepage backend python commited
parent
d300f1fd
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
0 deletions
+49
-0
homepage.py
src/homepage.py
+49
-0
No files found.
src/homepage.py
0 → 100644
View file @
06a465d4
from
flask
import
Flask
,
render_template
import
requests
app
=
Flask
(
__name__
)
# Kroger API credentials (replace with your actual credentials)
CLIENT_ID
=
'your_client_id'
CLIENT_SECRET
=
'your_client_secret'
def
get_kroger_token
():
url
=
'https://api.kroger.com/v1/connect/oauth2/token'
data
=
{
'grant_type'
:
'client_credentials'
,
'scope'
:
'product.compact'
}
headers
=
{
'Authorization'
:
f
'Basic {CLIENT_ID}:{CLIENT_SECRET}'
,
'Content-Type'
:
'application/x-www-form-urlencoded'
}
response
=
requests
.
post
(
url
,
data
=
data
,
headers
=
headers
)
response
.
raise_for_status
()
return
response
.
json
()[
'access_token'
]
# Fetch products for a given search term (e.g., fruits, vegetables, beverages)
def
fetch_products
(
token
,
term
,
limit
=
10
):
url
=
f
'https://api.kroger.com/v1/products?filter.term={term}&filter.limit={limit}'
headers
=
{
'Authorization'
:
f
'Bearer {token}'
}
response
=
requests
.
get
(
url
,
headers
=
headers
)
response
.
raise_for_status
()
return
response
.
json
()[
'data'
]
@
app
.
route
(
'/'
)
def
homepage
():
token
=
get_kroger_token
()
# Fetch different product categories for the homepage
fruits
=
fetch_products
(
token
,
'fruits'
,
limit
=
5
)
vegetables
=
fetch_products
(
token
,
'vegetables'
,
limit
=
5
)
beverages
=
fetch_products
(
token
,
'beverages'
,
limit
=
5
)
# Pass all products to the template
return
render_template
(
'homepage.html'
,
fruits
=
fruits
,
vegetables
=
vegetables
,
beverages
=
beverages
)
if
__name__
==
'__main__'
:
app
.
run
(
debug
=
True
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment