繁体   English   中英

如何使用 Z319C3206A7F10C17C7B9116D4AZ9 在 html 页面上显示 python 控制台 output

[英]how to display python console output on html page using flask

def execute():
    print("this is imam")
    #perform some task after first print then print next function
    print('this akarsh')
    #perform some task after first print then print next function
    print('this is abhi')

@app.route('/')
def home ():
    return render_template('index.html', x= execute())

我只是想在我的 html 页面上打印上面三个打印语句,我尝试使用 set 变量传递,请帮助我。 当它在 python 控制台上打印时,一个接一个地执行上述某些任务。

def execute():
    return """
        this is imam
        this akarsh
        this is abhi
    """

如果您使用index.html作为模板。 它应该如下所示:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h3>This is a parameter from app</h3>
    {{ x }}
</body>
</html>

并且您必须像这样更改您的execute() function:

def execute():
    x = 'this is imam'
    print("this is imam")
    #perform some task after first print then print next function
    y = 'this akarsh'
    print('this akarsh')
    #perform some task after first print then print next function
    z = 'this is abhi'
    print('this is abhi')
    result = [x,y,x]
    return result

您可以使用列表(或任何 python 数据结构)来返回结果。

你可以使用这个方法

  1. 创建Outposts class
     class Outpost: def __init__(self): self.outposts = [] def add_outpost(self, data): self.outposts.append(data)
  2. execute() function 复制 Outposts 并运行execute()
     execute_outposts = Outpost() execute()
  3. 在执行 function 用execute_outposts.add_outpost(text)替换打印
  4. 并将execute_outposts.outposts放入x

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM