anschelburk / nutrient_guide

Apache License 2.0
0 stars 0 forks source link

Adding the ingredients to a dict #12

Open ryaustin opened 9 months ago

ryaustin commented 9 months ago
ingredients = {}
st.button('Add to list')
if st.button:
    ingredients[search] = results
    st.write(ingredients)

pp(ingredients)
# next you want to present a button to store the search query (add to list)
#1 user presses the button (add to list)
#2 the search query is stored in a dictionary with the key being the search query and the value being the results { pumpkin: {results} }
#3 after storing you have a method that presents the stored search queries and displays a table (df) of all the ingredients and their nutritional values
#4 then you'll have a method to reference the stored daily values and compate the total nutritional values of the stored ingredients to the daily values
#5 then you'll have a method to display the results of the comparison
anschelburk commented 9 months ago

Seem to be running into an issue where the following has stopped returning any results:

results_name = r.json().get('foods')[0].get('description')

I added some code to the end of the program to create a dictionary I could append...

Unchanged code (for reference) from earlier in program:

search_endpoint = 'https://api.nal.usda.gov/fdc/v1/foods/search' DEMO_KEY = 'DEMO_KEY' r = requests.get(search_endpoint, params={"query": search, "api_key":DEMO_KEY}) results_name = r.json().get('foods')[0].get('description') results_nutrients = r.json().get('foods')[0].get('foodNutrients')

New code:

ingredients = {'foodname':[], 'nutrients':[]} if st.button: ingredients['foodname'].append(results_name) ingredients['nutrients'].append(results_nutrients) st.write('My list:') st.write(ingredients)

The code worked when I searched for a food and tried to add it. When I tried to add the same food a second time, the code I wrote above stopped working (even when I reloaded the page, and put #'s next to all the new code). I keep getting the following error:

image

Any thoughts? It's weird that that particular piece of code would generate the error, since it's been working fine up until now - and since it shouldn't be connected to the dictionary I tried to create. (I initially thought it might be that we'd exceeded the DEMO_KEY, but it didn't work even when I put my API key in.) Maybe I'll just try again later? Any thoughts appreciated!

anschelburk commented 9 months ago

Ignore that! Figured it out - using the command line!! That's a weekly win. Will reach out if I have any other questions!