I’m developing a streamlit front end for my script that lets me use a UPC scanner to add things to my cart using the Kroger API. This is the original but translate the input and print to text_input and toast. The whole True loop works when running the script from terminal no problem. When I use it in streamlit it throws key errors. When I take the while loop out, it runs through the entire script again. I’m having trouble understanding session states. Can anyone help?
while True:
# Waits for user input from the UPC scanner
print("Waiting for UPC")
upc = input()
items = {
"upc": upc,
"quantity": 1
}
while True:
status = add_items_to_cart(token, items)
# Submits the PUT request to add the UPC to the cart
if status == 401:
"""
If the access token is expired for 30 minutes, it will throw a 401 error.
This will use the refresh_auth_token function to request a new access token.
Once it obtains a new token, it will attempt to add the item to the cart
Backlog Item - Need to throw the authorization function in this loop so the user
will not even notice it's renewing.
"""
token, refresh_token = refresh_auth_token(refresh_token, encoded_client_token)
# This prints the time the token was renewed just for developer awareness, not needed
print(f"Refresh token renewed at {current_time}")
# Adds the item to the cart
status = add_items_to_cart(token, items)
# Returns the product JSON
product = get_product(upc, token)
# Obtains the description, size, and image URL of the product
description, size, imgurl = get_product_info(product)
# Standard message for adding something to the cart
message = f"{description} - {size} has been added to your cart"
print(message)
break
elif status == 400:
# User probably left it blank or UPC was not found
print("Shit's fucked, maybe you left it blank")
break
elif status == 204:
# Success
product = get_product(upc, token)
description, size, imgurl = get_product_info(product)
message = f"{description} - {size} has been added to your cart"
print(f'{message}')
break