Search code examples
pythonweb-scrapingstreamlittqdm

Can't get stqdm() to work in a for loop in a streamlit app


I've been trying to incorporate the stqdm status bar in the dev branch of my streamlit app and can't get around the error message below. I've wrapped the for loop in the df_create() function in main.py with the stqdm() method but I don't think this is how this is meant to be used. Here's an excerpt of the function showing what I've done:

def create_df(recipes):
    """
    Description:
    Creates one df with all recipes and their ingredients
    Arguments:
    * recipes: list of recipe URLs provided by user
    Comments:
    Note that ingredients with qualitative amounts e.g., "scheutje melk", "snufje zout" have been ommitted from the ingredient list
    """
    df_list = []

    for recipe in stqdm(range(len(recipes))):
        scraper = scrape_me(recipe)
        recipe_details = replace_measurement_symbols(scraper.ingredients())

        recipe_name = recipe.split("https://www.hellofresh.nl/recipes/", 1)[1]
        recipe_name = recipe_name.rsplit('-', 1)[0]
        print("Processing data for "+ recipe_name +" recipe.")
    ...
    ...
    ...

Any help would be appeciated!


enter image description here


Solution

  • From my understanding of the current script, this has nothing to do with stqdm or even streamlit.

    You might have a typo that makes you iterate over the index of the list instead of its elements. Replace range(len(recipes)) with recipes or replace the name of the index with index and go with recipes[index].

    You should probably be able to reproduce this with the code below in a script ran directly with python.

    from recipe_scrapers import scrape_me
    
    recipes = ["https://www.allrecipes.com/recipe/158968/spinach-and-feta-turkey-burgers/"]
    
    # You should rather do: `for recipe in stqdm(recipes):`
    for recipe in range(len(recipes)):
       scrape_me(recipe)
    

    On another note, you might have been able to find this by yourself by trying to produce a more minimal example.