Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6235997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:46:27+00:00 2026-05-24T10:46:27+00:00

I was working on a Flask project, getting some data from an API wrapper.

  • 0

I was working on a Flask project, getting some data from an API wrapper. The wrapper returned a generator object, so I print the values (for obj in gen_object: print obj) before passing it to Flask’s render_template().

When requesting the page while printing the objects, the page is empty. But removing the for loop the page renders the generator object’s content.

@app.route('/')
def front_page():
    top_stories = r.get_front_page(limit=10)
    # this for loop prevents the template from rendering the stories
    for s in top_stories:
        print s
    return render_template('template.html', stories=top_stories)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:46:28+00:00Added an answer on May 24, 2026 at 10:46 am

    Yes, generators are ment to be consumed once. Each time we iterate a generator we ask it to give us another value, and if there’s no more values to give the StopIteration exception is thrown which would stop the iteration. There’s no way for the generator to know that we want to iterate it again without cloning it.

    As long as the records can fit comfortably in memory, I would use a list instead:

    top_stories = list(top_stories)
    

    That way you can iterate top_stories multiple times.

    There’s a function called itertools.tee to copy an iterator that also could help you but it’s sometimes slower than just using a list.
    Reference:
    http://docs.python.org/library/itertools.html?highlight=itertools.tee#itertools.tee

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on a project using Flask and Google App Engine . Calling
I'm working on this Flash project done with AS3 and for some reason the
I'm currently working on a Flash project in which an object is moving along
I am working on a web development project using flask and sqlalchemy orm. My
I'm working on a Flash project right now and I am having to force
I'm working on a large Flash CS4 project with multiple swfs and want to
I'm working on a flash web application (Actionscript 2.0) for my honours project but
I am working on a project which involves uploading flash video files to a
I'm working with XML and Flash. I tried to pass XML values to my
I'm working on Flash Builder with latest flex SDK. I have a problem getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.