2024-06-02 07:52:16 发布
网友
在Flask上,有没有一种方法可以运行python脚本而不转到另一个页面?即不重定向?你知道吗
我想做的是,按下一个页面上的按钮,它会触发一个Python函数,该函数将返回一些数据并在同一页面上显示数据。用烧瓶可以吗?你知道吗
下面是这样一个程序的示例:
from flask import Flask, render_template_string, request, jsonify import wikipedia app = Flask(__name__) index_html=''' <!DOCTYPE html> <html lang="en"> <head><title>Wiki Lookup Example</title></head> <body> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> function submit() { $.getJSON({{ request.script_root|tojson|safe }} + '/topics', { key: $('#key').val() }, function(data) { var ul = $('#result'); ul.empty(); $(data.topics).each(function(index, item) { ul.append($('<li>',{text:item}));});});} </script> <p> Search Term: <input id="key" name="key"/> <input id="submit" type="button" value="Go" onclick="submit();" /></p> <hr /><ul id="result"></ul> </body></html>''' @app.route('/') def index(): return render_template_string(index_html) @app.route('/topics') def topics(): key = request.args.get('key') topics = wikipedia.search(key) or ['No topic found'] return jsonify(topics=topics) app.run(debug=True)
解决方案是AJAX。你知道吗
如链接中所述 http://api.jquery.com/jquery.ajax/
$.ajax({ url: "url defined in Flask", method: "GET" }).done(function() { // Change your DOM });
烧瓶中的APi定义:
创建restfulapi。你知道吗
http://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask
在路由中定义url。编写要调用的函数。返回为JSON。在done函数中,可以更改DOM元素。你知道吗
下面是这样一个程序的示例:
解决方案是AJAX。你知道吗
如链接中所述 http://api.jquery.com/jquery.ajax/
烧瓶中的APi定义:
创建restfulapi。你知道吗
http://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask
在路由中定义url。编写要调用的函数。返回为JSON。在done函数中,可以更改DOM元素。你知道吗
相关问题 更多 >
编程相关推荐