Search code examples
pythonpython-requests

I am encountering an error Maxtries in python while creating an application in streamlit


I am trying to make an application that takes orders of smoothies.

I want to add nutritional value after choosing the fruit, but it is not able to fetch from the API.

Here is the code.

import streamlit as st
from snowflake.snowpark.context import get_active_session
from snowflake.snowpark.functions import col
import requests


st.title("Customize your Smoothie! App :cup_with_straw:")
st.write(
    """Choose the fruits you want in your custom Smoothie!
    """
)


name_on_order = st.text_input('Name on Smoothie:')
st.write('The name on your Smoothie be:', name_on_order)


session = get_active_session()
my_dataframe = session.table("smoothies.public.fruit_options").select(col('FRUIT_NAME'))

ingredients_list = st.multiselect('Choose upto 5 ingredients:',my_dataframe,max_selections=5)


if ingredients_list:
    ingredients_string = ""
    for fruit_chosen in ingredients_list:
        ingredients_string+= fruit_chosen+" "
        
        fruityvice_response = requests.get("https://fruityvice.com/api/fruit/"+fruit_chosen)
        st.text(fruityvice_response.json())
        #fv_dv = st.dataframe(data=fruityvice_response.json(), use_container_width=True)

    my_insert_stmt = """ insert into smoothies.public.orders(ingredients,name_on_order)
            values ('""" + ingredients_string + """','""" + name_on_order + """')"""
    
    #st.write(my_insert_stmt)
    time_to_insert= st.button('Submit Order')
    if time_to_insert:
        session.sql(my_insert_stmt).collect()
        st.success('Your Smoothie is ordered!', icon="✅")

This the error that I am getting:

ConnectionError: HTTPSConnectionPool(host='fruityvice.com', port=443): Max retries exceeded with url: /api/fruit/watermelonElderberries (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0xffff79c51400>: Failed to resolve 'fruityvice.com' ([Errno -3] Temporary failure in name resolution)"))
Traceback:
File "/usr/lib/python_udf/5f3d47ae4d4f192ce40f32e768dce379f8b04388fe033d00a5b697a7351e713f/lib/python3.8/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
File "/home/udf/88736396/streamlit_app.py", line 33, in <module>
    fruityvice_response = requests.get("https://fruityvice.com/api/fruit/watermelon"+fruit_chosen)
File "/usr/lib/python_udf/5f3d47ae4d4f192ce40f32e768dce379f8b04388fe033d00a5b697a7351e713f/lib/python3.8/site-packages/requests/api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
File "/usr/lib/python_udf/5f3d47ae4d4f192ce40f32e768dce379f8b04388fe033d00a5b697a7351e713f/lib/python3.8/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python_udf/5f3d47ae4d4f192ce40f32e768dce379f8b04388fe033d00a5b697a7351e713f/lib/python3.8/site-packages/requests/sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
File "/usr/lib/python_udf/5f3d47ae4d4f192ce40f32e768dce379f8b04388fe033d00a5b697a7351e713f/lib/python3.8/site-packages/requests/sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
File "/usr/lib/python_udf/5f3d47ae4d4f192ce40f32e768dce379f8b04388fe033d00a5b697a7351e713f/lib/python3.8/site-packages/requests/adapters.py", line 519, in send
    raise ConnectionError(e, request=request)

I tried a try-except block and delaying the processing time to complete the loop.


Solution

  • The problem looks like you have a bad URL. Look at the URL they are displaying that failed.

    Max retries exceeded with url: /api/fruit/watermelonElderberries (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0xffff79c51400>: Failed to resolve 'fruityvice.com' ([Errno -3] Temporary failure in name resolution)"))

    The url looks wrong. /api/fruit/watermelonEldererries ?

    Update
    Now that you have fixed your URL you need to consider the possibility that a user will ask for a fruit that is not in the websites database of fruits. What's stopping the user from asking for a 'taco' for example. You need to make sure that the item was found before processing it.

    The problems really don't have anything to do with streamlit, and have to do with your usage of the request module.