简体   繁体   中英

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. As it prints on python console one after the other performing certain task as mentioned.

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

If you are using index.html as a template. 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:

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.

You can use this method

  1. create 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_outposts = Outpost() execute()
  3. in execute function replace the print with execute_outposts.add_outpost(text)
  4. and put execute_outposts.outposts in x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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