How to continuously display Python output in a Webpage?

后端 未结 2 1465
花落未央
花落未央 2020-12-04 13:28

I want to be able to visit a webpage and it will run a python function and display the progress in the webpage.

So when you visit the webpage you can see the output

2条回答
  •  余生分开走
    2020-12-04 14:00

    Hi looks like you don't want to call a test function, but an actual command line process which provides output. Also create an iterable from proc.stdout.readline or something. Also you said from Python which I forgot to include that you should just pull any python code you want in a subprocess and put it in a separate file.

    import flask
    import subprocess
    import time          #You don't need this. Just included it so you can see the output stream.
    
    app = flask.Flask(__name__)
    
    @app.route('/yield')
    def index():
        def inner():
            proc = subprocess.Popen(
                ['dmesg'],             #call something with a lot of output so we can see it
                shell=True,
                stdout=subprocess.PIPE
            )
    
            for line in iter(proc.stdout.readline,''):
                time.sleep(1)                           # Don't need this just shows the text streaming
                yield line.rstrip() + '
    \n' return flask.Response(inner(), mimetype='text/html') # text/html is required for most browsers to show th$ app.run(debug=True, port=5000, host='0.0.0.0')

提交回复
热议问题