有没有办法返回make_response()
具有某些属性的响应(来自对象或类似),以便它不会再次呈现页面,也不会执行任何其他操作.我试图在服务器上运行代码而不生成任何输出
一个简单的'return None'产生:
ValueError: View function did not return a response
Run Code Online (Sandbox Code Playgroud)
这应该是可能的,因为以下只下载文件而不呈现模板:
myString = "First line of a document"
response = make_response(myString)
response.headers["Content-Disposition"] = "attachment; filename=myFile.txt"
return response
Run Code Online (Sandbox Code Playgroud) 按下我的烧瓶模板中的按钮后,我希望它调用app.py中定义的python函数,通过在我定义函数的下面键入以下内容,我可以在模板中调用它:
app.py中的示例函数:
@app.route('/foo')
def foo(x,y):
pass
app.jinja_env.globals.update(foo=foo)
Run Code Online (Sandbox Code Playgroud)
模板:
<button type="button" onclick="myFunction(this)" name="enable" id="{{counter}}"> Enable </button>
Run Code Online (Sandbox Code Playgroud)
在我的按钮中我有onclick属性只是为了测试使用javascript按下许多正确的按钮:
{% block scripts %}
{{ super() }}
<script>
function myFunction(elem){
if(confirm('Are you sure you want to ' + elem.name) == true){
alert("its done.");
}
else {
return false;
}
}
</script>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
我面临的问题是我需要在模板中提供的功能,以对应正确的按钮.例如,如果按钮显示Enable,那么我需要调用已经定义的启用功能,否则如果按钮对应于false,我想要使用禁用功能.
我觉得我朝着正确的方向前进,但无法超越这一部分.请尽可能详细.
出于好奇,我已经在计算机上保存了许多python脚本,我创建了一个只有一个按钮的html文件。而是进入终端并运行python <path to script>
,我想这样做,以便在单击该按钮时启动我的python脚本之一。这可能/如何做到?
出于示例目的,我们将脚本称为MYSCRIPT.py。我做了一些研究,但没有提出任何希望。我知道以下代码是不正确的,但仅出于起点考虑,这是我的html文件。
<!DOCTYPE html>
<html>
<body>
<head>
<input type="button" id='script' name="scriptbutton" value=" Run Script " onclick="exec('python MYSCRIPT.py');">
</head>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我刚刚开始使用python和flask(对于raspberry pi).我想要一个Web应用程序,它将执行一些python代码来平移和倾斜摄像机并显示视频流.
我现在对于烧瓶的代码是:
from flask import Flask, render_template
import time
import serial
#ser = serial.Serial('/dev/ttyUSB0',9600)
app = Flask(__name__)
@app.route('/')
@app.route('/<cmd>') #each button in my html redirects to a specified directory
def execute(cmd=None):
if cmd == "down":
print "Moving Down"
#ser.write("D")
if cmd == "up":
print "Moving Up"
#ser.write("U")
if cmd == "left":
print "Moving Left"
# ser.write("L")
if cmd == "right":
print "Moving Right"
#ser.write("R")
if cmd == "reset":
print "Reseting.."
#ser.write("X")
return render_template("main.html")
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080, …
Run Code Online (Sandbox Code Playgroud) 我希望能够单击html中的按钮并调用python函数。我已经尝试过了,但它仅适用于文本。而且我在这里看到可以在按钮中使用函数名称,但是它不起作用,我也不知道为什么:/
而且我不希望在单击按钮后转到另一页,我希望停留在同一页上并只执行该函数中的代码。
我的py文件:
from flask import Flask
from flask import render_template
import tkinter as tk
from tkinter import filedialog
import sys
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template('hello.html')
@app.route('/upload/')
def uploaduj():
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
return file_path
Run Code Online (Sandbox Code Playgroud)
我的html文件:
<!doctype html>
<title>Flaskr</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
<div class=page>
<h1>Flaskr</h1>
<div class=metanav>
<button action="/upload/">Klik</button>
</div>
Run Code Online (Sandbox Code Playgroud)
我对python和flask真的很陌生,因此感谢您的帮助。
编辑:我现在知道tkinter将无法在Web浏览器中工作
我有一个 Flask 应用程序,它有一个页面,用户可以在其中输入一个句子。我想将这个用户输入输入到一个 Flask 方法中,该方法将对那个句子执行一些计算,然后返回结果(这是一个字符串)。
理想情况下,我希望它返回到同一页面,就在表单的提交按钮下方。
目前,我正在尝试使用 javascript 来做到这一点。这是我目前所拥有的:
结果.html
<head>
<script type=text/javascript>
$(function() {
$('a#test').bind('click', function() {
$.getJSON('/result_sentence',{
sentence: $('textarea[name="sentence"]').val()}, function(data) {
// do nothing
document.getElementById('display').innerHTML = data.result;
});
return false;
});
</script>
</head><body>
<!-- The form where the user inputs -->
<div id="interactive" style="margin-top:50px">
<textarea style="width:100%;" rows="3" name="sentence" placeholder="Enter your sentence"></textarea>
<a href="#" id=test button type="submit" class="btn btn-lg btn-success"> Predict </a>
<label> I predict.. </label>
<p><span id='display'></span></p>
</div>
Run Code Online (Sandbox Code Playgroud)
应用程序
@app.route('/result_sentence')
def result_sentence():
sentence = flask.request.args.get('sentence')
pred = getPrediction(sentence) …
Run Code Online (Sandbox Code Playgroud)