简体   繁体   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())

I just wanted to print the upper three print statements on my html page i have tried with the set variable to pass, please help me with this.我只是想在我的 html 页面上打印上面三个打印语句,我尝试使用 set 变量传递,请帮助我。 As it prints on python console one after the other performing certain task as mentioned.当它在 python 控制台上打印时,一个接一个地执行上述某些任务。

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

If you are using index.html as a template.如果您使用index.html作为模板。 It should look like this:它应该如下所示:

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

and you have to change your execute() function like this:并且您必须像这样更改您的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

You can use a list (or any python data structure) to return your results.您可以使用列表(或任何 python 数据结构)来返回结果。

You can use this method你可以使用这个方法

  1. create Outposts class创建Outposts class
     class Outpost: def __init__(self): self.outposts = [] def add_outpost(self, data): self.outposts.append(data)
  2. make a copy of Outposts for execute() function and run execute() :execute() function 复制 Outposts 并运行execute()
     execute_outposts = Outpost() execute()
  3. in execute function replace the print with execute_outposts.add_outpost(text)在执行 function 用execute_outposts.add_outpost(text)替换打印
  4. and put execute_outposts.outposts in x并将execute_outposts.outposts放入x

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

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