是否可以在HTML上嵌入和运行Python代码?
我正在尝试运行一个嵌入HTML代码的Python脚本,但它不起作用。我想执行一个Python脚本,同时呈现将由脚本打印的HTML是否可以在HTML上嵌入和运行Python代码?,python,html,flask,embed,Python,Html,Flask,Embed,我正在尝试运行一个嵌入HTML代码的Python脚本,但它不起作用。我想执行一个Python脚本,同时呈现将由脚本打印的HTML app.py: #!/usr/bin/python2.6 from flask import Flask, render_template app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') @app.route('/briefin
app.py
:
#!/usr/bin/python2.6
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/briefing')
def briefing():
return render_template('briefing.html')
@app.route('/briefing/code')
def app_code():
return render_template('app_code.py')
if __name__ == '__main__':
app.run(debug=True)
#!/usr/bin/python2.6
import subprocess
from flask import Flask, render_template
app = Flask(__name__)
def get_data():
"""
Return a string that is the output from subprocess
"""
# There is a link above on how to do this, but here's my attempt
# I think this will work for your Python 2.6
p = subprocess.Popen(["tree", "/your/path"], stdout=subprocess.PIPE)
out, err = p.communicate()
return out
@app.route('/')
def index():
return render_template('subprocess.html', subprocess_output=get_data())
if __name__ == '__main__':
app.run(debug=True)
app_code.py
:
#!/usr/bin/python2.6
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/briefing')
def briefing():
return render_template('briefing.html')
@app.route('/briefing/code')
def app_code():
return render_template('app_code.py')
if __name__ == '__main__':
app.run(debug=True)
#!/usr/bin/python2.6
import subprocess
from flask import Flask, render_template
app = Flask(__name__)
def get_data():
"""
Return a string that is the output from subprocess
"""
# There is a link above on how to do this, but here's my attempt
# I think this will work for your Python 2.6
p = subprocess.Popen(["tree", "/your/path"], stdout=subprocess.PIPE)
out, err = p.communicate()
return out
@app.route('/')
def index():
return render_template('subprocess.html', subprocess_output=get_data())
if __name__ == '__main__':
app.run(debug=True)
当我访问http://127.0.0.1:5000/briefing/code
结果是
我知道现在的情况是,我将呈现为HTML,因此不会解释文件中的Python代码
如何运行
app_code.py
,同时从中呈现HTML?你把很多事情都搞混了,我看到了问题的第一篇帖子,花了我一段时间才弄明白你在做什么
您似乎需要掌握的思想是,首先需要用Python准备模型(例如,字符串、对象、包含所需数据的dict等),然后将其注入要呈现的模板中(而不是打印出希望在HTML输出中看到的内容)
如果要将子流程.call的输出显示到HTML页面中,请执行以下操作:
- 创建HTML模板以在中显示它
- 让Flask调用子流程,呈现模板并返回HTTP响应
app.py
:
#!/usr/bin/python2.6
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/briefing')
def briefing():
return render_template('briefing.html')
@app.route('/briefing/code')
def app_code():
return render_template('app_code.py')
if __name__ == '__main__':
app.run(debug=True)
#!/usr/bin/python2.6
import subprocess
from flask import Flask, render_template
app = Flask(__name__)
def get_data():
"""
Return a string that is the output from subprocess
"""
# There is a link above on how to do this, but here's my attempt
# I think this will work for your Python 2.6
p = subprocess.Popen(["tree", "/your/path"], stdout=subprocess.PIPE)
out, err = p.communicate()
return out
@app.route('/')
def index():
return render_template('subprocess.html', subprocess_output=get_data())
if __name__ == '__main__':
app.run(debug=True)
subprocess.html
:
子流程结果
子流程结果
{{subprocess_output}}
在上面的模板中,{{subprocess_output}}
将替换为在将生成的HTML页面发送到浏览器之前从Flask视图传递的值
如何传递多个值
您可以render_模板('page.html',value_1='something 1',value_2='something 2')
在模板中:{{value_1}}
和{{value_2}}
或者,您可以传递一个名为“结果”的dict,例如result
:
render_模板('page.html,result={'value_1':'something 1','value_2':'something 2'})
在模板{{result.value_1}}
和{{result.value_2}}