title: Python Flask how to use Response to serve from a generator from a mongo query
body: <p>Say that my function "search()" fetches some content in the mongodb and returns the generator.</p>
  
  <p>My flask view function looks like this</p>
  
  <pre><code>@app.route("search/")
  def search_page():
     generator = search()
     return Response(generator)
  </code></pre>
  
  <p>But if I do that, I get this error:</p>
  
  <pre><code>Error on request:
  Traceback (most recent call last):
    File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 180, in run_wsgi
      execute(self.server.app)
    File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 171, in execute
      write(data)
    File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 151, in write
      assert type(data) is bytes, 'applications must write bytes'
  AssertionError: applications must write bytes
  </code></pre>
  
  <p>The generator itself would yield several json values. I could always use the generator to construct a list and return the list, but I would like to avoid doing that.</p>