Javascript Transcrypt:如何使用Python/Flask发送ajax POST请求以启动服务器端作业
我正处于创建Flask应用程序的早期阶段,遇到了@Miguel Grinberg关于创建长时间运行任务的一个很好的例子。目前,我想减少我必须在项目中学习/使用的JavaScript数量,Transcrypt吸引了我的眼球 然而,在试图找出如何完成POST请求以触发任务的文档中,我有点迷失了方向。以下是JavaScript中的代码:Javascript Transcrypt:如何使用Python/Flask发送ajax POST请求以启动服务器端作业,javascript,jquery,python,ajax,transcrypt,Javascript,Jquery,Python,Ajax,Transcrypt,我正处于创建Flask应用程序的早期阶段,遇到了@Miguel Grinberg关于创建长时间运行任务的一个很好的例子。目前,我想减少我必须在项目中学习/使用的JavaScript数量,Transcrypt吸引了我的眼球 然而,在试图找出如何完成POST请求以触发任务的文档中,我有点迷失了方向。以下是JavaScript中的代码: // send ajax POST request to start background job $.ajax({
// send ajax POST request to start background job
$.ajax({
type: 'POST',
url: '/longtask',
success: function(data, status, request) {
status_url = request.getResponseHeader('Location');
update_progress(status_url, nanobar, div[0]);
},
error: function() {
alert('Unexpected error');
}
});
如何使用Transcrypt在Python中实现这一点?您可以在Flask中使用post方法
from flask import Flask
app = Flask(__name__)
@app.route('/index/', methods=['POST'])
def sample_function():
# do your server task here
return "BROWSER Update/response"
app.run("127.0.0.1", "8000")
在JavaScript中,调用
http://127.0.0.1:8000/index/
使用AJAX。您可以在Flask中使用post方法
from flask import Flask
app = Flask(__name__)
@app.route('/index/', methods=['POST'])
def sample_function():
# do your server task here
return "BROWSER Update/response"
app.run("127.0.0.1", "8000")
在JavaScript中,调用http://127.0.0.1:8000/index/
使用AJAX。按以下步骤操作:
$
在Python中不是有效的标识符,因此请使用pragma('alias','jq','$')
,如下所示
除了lambda之外,Python不知道匿名函数,所以使用普通函数作为回调函数。在下面的示例中,使用了局部函数
注意在字段名周围加引号,例如“success”而不是success,因为这是Python约定
使用Transcrypt的Ajax示例:
__pragma__ ('alias', 'jq', '$')
# For use by eval'ed turtle applet
import turtle
import random
import math
def clear ():
editor.setValue ('')
turtle.reset ()
run ()
def run ():
def success (result):
turtle.reset ()
eval (result)
def fail (a, b, c):
print ('Run error:', a, b, c)
# N.B. The request has to be explicitly encoded, but the response is already implicitly decoded
jq.ajax ({
'url':'http://www.transcrypt.org/compilemodule',
'type': 'POST',
'data': JSON.stringify (editor.getValue ()),
'dataType': 'json',
'contentType': 'application/json',
'success': success,
'fail': fail
})
def mail ():
def success (result):
print (result)
def fail (a, b, c):
print ('Run error:', a, b, c)
jq.ajax ({
'url':'http://www.transcrypt.org/sendmail',
'type': 'POST',
'data': JSON.stringify ([document.getElementById ('mail_address') .value, editor.getValue ()]),
'dataType': 'json',
'contentType': 'application/json',
'success': success,
'fail': fail
})
def selectExample ():
def success (result):
editor.setValue (result [0])
turtle.reset () # Using old paths
window.terminate = True
eval (result [1]) # Using new paths (so cannot clear old result)
def fail (a, b, c):
print ('Select example error:', a, b, c)
selector = document.getElementById ('select_example')
jq.ajax ({
'url':'http://www.transcrypt.org/selectexample',
'type': 'POST',
'data': JSON.stringify (selector.options [selector.selectedIndex] .value),
'dataType': 'json',
'contentType': 'application/json',
'success': success,
'fail': fail
})
selectExample ()
进行如下工作:
$
在Python中不是有效的标识符,因此请使用pragma('alias','jq','$')
,如下所示
除了lambda之外,Python不知道匿名函数,所以使用普通函数作为回调函数。在下面的示例中,使用了局部函数
注意在字段名周围加引号,例如“success”而不是success,因为这是Python约定
使用Transcrypt的Ajax示例:
__pragma__ ('alias', 'jq', '$')
# For use by eval'ed turtle applet
import turtle
import random
import math
def clear ():
editor.setValue ('')
turtle.reset ()
run ()
def run ():
def success (result):
turtle.reset ()
eval (result)
def fail (a, b, c):
print ('Run error:', a, b, c)
# N.B. The request has to be explicitly encoded, but the response is already implicitly decoded
jq.ajax ({
'url':'http://www.transcrypt.org/compilemodule',
'type': 'POST',
'data': JSON.stringify (editor.getValue ()),
'dataType': 'json',
'contentType': 'application/json',
'success': success,
'fail': fail
})
def mail ():
def success (result):
print (result)
def fail (a, b, c):
print ('Run error:', a, b, c)
jq.ajax ({
'url':'http://www.transcrypt.org/sendmail',
'type': 'POST',
'data': JSON.stringify ([document.getElementById ('mail_address') .value, editor.getValue ()]),
'dataType': 'json',
'contentType': 'application/json',
'success': success,
'fail': fail
})
def selectExample ():
def success (result):
editor.setValue (result [0])
turtle.reset () # Using old paths
window.terminate = True
eval (result [1]) # Using new paths (so cannot clear old result)
def fail (a, b, c):
print ('Select example error:', a, b, c)
selector = document.getElementById ('select_example')
jq.ajax ({
'url':'http://www.transcrypt.org/selectexample',
'type': 'POST',
'data': JSON.stringify (selector.options [selector.selectedIndex] .value),
'dataType': 'json',
'contentType': 'application/json',
'success': success,
'fail': fail
})
selectExample ()
是否要使用javascript中的ajax向python flask服务发送POST请求?@abhinav是的,我希望客户端能够在浏览器中触发一个任务,然后在服务器端运行一些python代码,完成后用状态更新浏览器。我希望使用Transcrypt来实现这一点,而不是现在就使用JavaScript(最终我将不得不学习它)。是否要使用JavaScript中的ajax向python flask服务发送POST请求?@abhinav是的,我希望客户端能够在浏览器中触发任务,然后在服务器端运行一些python代码,完成后,使用状态更新浏览器。我希望使用Transcrypt来实现这一点,而不是现在就使用JavaScript(最终我将不得不学习它)。感谢您的回复。我的问题不在于烧瓶部分,那部分很容易。我的问题是,随着我的项目不断扩展,JavaScript所需的复杂性不断增加,我的进度将急剧停止。我在这方面的知识非常有限,希望利用Transcrypt编写编译成JavaScript的python代码。感谢您的回复。我的问题不在于烧瓶部分,那部分很容易。我的问题是,随着我的项目不断扩展,JavaScript所需的复杂性不断增加,我的进度将急剧停止。我在这方面的知识非常有限,希望利用Transcrypt编写编译成JavaScript的python代码。