这是烧瓶文件
from flask import Flask, redirect, render_template, request
from subprocess import call
app = Flask(__name__)
@app.route('/')
def mai():
return render_template('homepage.html')
@app.route('/handle_data', methods=['GET','POST'])
def home():
if request.method == "POST":
projectpath = request.form.get("textinput")
class Callpy(object):
def __init__(self,path='test.py'):
self.path = path
def call_python_file(self):
call(['poetry','run','python',f"{self.path}",projectpath])
c= Callpy()
output = c.call_python_file()
if output:
return render_template('homepage.html',output=output)
return redirect('/')
if __name__ =='__main__':
app.run(debug = True)
这是 test.py 文件
import sys
if not sys.argv[1]:
name = input("hello enter your name : ")
name = sys.argv[1]
def run(name):
print(f"My name is {name}")
print(f"My name is {name}")
print(f"My name is {name}")
print(f"My name is {name}")
print(f"My name is {name}")
run(name)
这是 HTML 文件
<form class="form-horizontal" action="{{ url_for('home')}}" method="post">
<fieldset>
<legend>Hello</legend>
<br>
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Enter your name</label>
<div class="col-md-4">
<input id="textinput" name="textinput" type="text" placeholder="" class="form-control input-md"
required="">
</div>
</div>
<br>
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<button id="submit" name="submit" class="btn btn-primary">submit</button>
</div>
</div>
<br>
<div class="logging_window">
<p>{{ output }}</p>
</div>
</fieldset>
</form>
<style>
.logging_window{
display: block;
padding: 9.5px;
font-size: 13px;
line-height: 1.42857143;
color: #333;
word-break: break-all;
word-wrap: break-word;
background-color: #f5f5f5;
border: 1px solid #ccc;
border-radius: 4px;
width: 50%;
margin: auto;
}
</style>
在浏览器中运行 flask 应用程序后,用户可以输入他的名字并提交,并且输出必须显示在 html 页面下方,但它不起作用,我得到的输出为无,我正在调用另一个 python 文件,我需要在浏览器上打印它,输出正在控制台上打印
有人能帮助我如何使用 live [1] 在网站上打印所有这些内容吗:https://i.stack.imgur.com/vXO1H.png [2]:https://i.stack.imgur.com/yu9HD.png