1

I'm trying stream stdout of a function to a webpage. The some_function.main takes few mins to complete and I want to show the stdout to the screen (its using logger).

This code does execute the function but I don't get anything on the webpage, I see everything on the screen when I run "python app.py" (I'm using Flask)

from jinja2 import Environment
from jinja2.loaders import FileSystemLoader

@app.route('/buildvm', methods=['GET', 'POST'])
def buildvm():
   if 'username' not in session:
      return redirect(url_for('login'))

   if request.method == "POST" and 'username' in session:
      parmsdic = {'key':'val'}
        def inner(disc):
          sys.path.append('/some/path')
          import some_fuction
          #for x in range(100):
          for x in some_fuction.main(disc, quite=True):
             yield '{0}<br/>\n'.format(x)
       env = Environment(loader=FileSystemLoader('templates'))
       tmpl = env.get_template('results.html')
       return Response(tmpl.generate(result=inner(parmsdic)))

   return render_template('index.html')

my results.html looks like:

{% block body %}
<body>
  {% for line in result %}
    {{ line }}
  {% endfor %}
</body>
{% endblock %}

Any help would be much appreciated, Thank you in advance.

Grene
  • 25
  • 1
  • 5

1 Answers1

0

You need to redirect stdout to a pipe and then put the output of that pipe into the template. Everything you need to know is in the answers of the linked question.

P.S. Pipe is an in-memory structure that can be used instead of files. It is also discussed in the linked question.

Community
  • 1
  • 1
Rusty
  • 904
  • 4
  • 10
  • But if I redirect the output to a file or StringIO , I will not be able to "stream" the output live it will be once the function returns right? – Grene Apr 29 '15 at 14:12