top of page

WhatTheCarb API app

WhatTheCarb is a nutrition analysis application designed to help users quickly and easily get detailed nutrition information for their recipes. This app leverages the Edamam API to provide comprehensive nutritional data, making it especially useful for individuals with specific dietary needs, such as those with diabetes.

​

​

Here's a short description of the techniques, functions, and libraries used in this project:

  1. Environment Variables:

    • Library: dotenv

    • Function: load_dotenv()

    • Purpose: Loads environment variables from a .env file, which is used to securely manage sensitive information such as API keys.

  2. Logging:

    • Library: logging

    • Functions: logging.basicConfig(), logging.getLogger(), logger.debug(), logger.info(), logger.critical(), logger.error()

    • Purpose: Configures and uses logging to track the application's execution, which helps in debugging and monitoring.

  3. GUI Development:

    • Library: tkinter

    • Widgets: Tk, Frame, Label, Text, Button, Canvas, Scrollbar, Menu, messagebox

    • Purpose: Creates a graphical user interface for the application, including input fields, buttons, labels, and frames.

  4. Image Handling:

    • Library: PIL (Pillow)

    • Classes: Image, ImageTk

    • Purpose: Loads and resizes images to be displayed in the GUI.

  5. HTTP Requests:

    • Library: requests

    • Function: requests.post()

    • Purpose: Sends HTTP POST requests to the Edamam Nutrition Analysis API to retrieve nutritional data based on user input.

  6. Error Handling:

    • Function: try-except

    • Purpose: Catches and handles exceptions that occur during API requests and other operations to provide user feedback and prevent application crashes.

  7. Nutritional Data Analysis:

    • Functions: analyze_nutrition(), display_nutrition_data(), format_number()

    • Purpose: Processes user input, sends it to the API, and displays the nutritional analysis in a structured format using Tkinter widgets.

  8. UI Layout Management:

    • Function: grid(), pack()

    • Purpose: Manages the layout and positioning of widgets within the application window.

  9. Dynamic Content Display:

    • Functions: clear_analysis(), display_error()

    • Purpose: Clears previous analysis results and displays new results or error messages dynamically based on user actions.

 

Example Breakdown of a Key Function

Function: analyze_nutrition()

  • Description: This function handles the nutrition analysis process. It retrieves the user input, sends it to the Edamam Nutrition Analysis API, processes the response, and updates the GUI with the analysis results or an error message if the request fails.

 

Steps:

  1. Logs the initiation of the nutrition analysis.

  2. Retrieves the text input from the user and strips any extra whitespace.

  3. Constructs the API request with the necessary headers and data payload.

  4. Sends the request using requests.post().

  5. Processes the API response, logs the response data, and clears any previous analysis results.

  6. Displays the new analysis results in the GUI or shows an error message if the request fails.

 

Sample Code Snippet

​​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

This project effectively combines the use of environment variables, logging, HTTP requests, and GUI development to create a functional and user-friendly nutrition analysis application.

​

You can find the complete project on my GitHub

Direct link to the repository: SnowY4you/WhatTheCarb: A little app to make life easier

​

Screen shots from the app, first in the input part and the second the output.​

sample code.png
WhatTheCarb02.png
WhatTheCarb01.png
bottom of page