如何在网页中持续显示Python输出? [英] How to continuously display Python output in a Webpage?

查看:1042
本文介绍了如何在网页中持续显示Python输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



所以,当你访问网页时,你可以访问网页,并且可以运行一个python函数并显示网页的进度。看到脚本的输出就好像从命令行运行一样。



根据这里的答案

如何在网页上连续显示python输出?



我正在尝试显示 PYTHON的输出



使用Markus Unterwaditzer的代码和一个python函数。

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ = flask.Flask(__ name__)

def test():
printTest

@ app.route('/ yield')
def index():
def inner():
proc = subprocess.Popen(
test(),
shell = True,
stdout = subprocess.PIPE


而proc.poll()是None:
yield proc.stdout.readline()+'< br /> \\\
'
return flask.Response (inner(),mimetype ='text / html')#大多数浏览器需要#text / html才能立即显示部分页面

app.run(debug = True,port = 5005)

它运行,但我没有在浏览器中看到任何东西。


<你好,你不想调用一个测试函数,而是一个提供输出的实际命令行进程。还从proc.stdout.readline或其他东西创建一个迭代。你还从Python那里说过,我忘了包括你应该把你想要的任何Python代码放在​​一个子进程中,并把它放在一个单独的文件中。

  import flask 
导入子流程
导入时间#您不需要这个。只是包括它,所以你可以看到输出流。
$ b应用程序= flask.Flask(__ name__)

@ app.route('/ yield')
def index():
def inner ):
proc = subprocess.Popen(
['dmesg'],#调用大量输出,所以我们可以看到它
shell = True,
stdout = subprocess .PIPE


在iter中的行(proc.stdout.readline,''):
time.sleep(1)#不需要这个只显示文本流
yield line.rstrip()+'< br /> \\\
'

return flask.Response(inner(),mimetype ='text / html')#text / html是大多数浏览器显示

app.run(debug = True,port = 5000,host ='0.0.0.0')


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 of the script as if you ran it from the command line.

Based on the answer here

How to continuously display python output in a webpage?

I am trying to display output from PYTHON

I am trying to use Markus Unterwaditzer's code with a python function.

import flask
import subprocess

app = flask.Flask(__name__)

def test():
    print "Test"

@app.route('/yield')
def index():
    def inner():
        proc = subprocess.Popen(
            test(),
            shell=True,
            stdout=subprocess.PIPE
        )

        while proc.poll() is None:
            yield proc.stdout.readline() + '<br/>\n'
    return flask.Response(inner(), mimetype='text/html')  # text/html is required for most browsers to show the partial page immediately

app.run(debug=True, port=5005)

And it runs but I don't see anything in the browser.

解决方案

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() + '<br/>\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')

这篇关于如何在网页中持续显示Python输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆