html登陆不刷新flask,Flask Button运行Python而无需刷新页面?

小编典典

我将其分为两条路线,以使你更轻松地了解所要做的事情:

LEFT, RIGHT, UP, DOWN, RESET = "left", "right", "up", "down", "reset"

AVAILABLE_COMMANDS = {

'Left': LEFT,

'Right': RIGHT,

'Up': UP,

'Down': DOWN,

'Reset': RESET

}

@app.route('/')

def execute():

return render_template('main.html', commands=AVAILABLE_COMMANDS)

@app.route('/')

def command(cmd=None):

if cmd == RESET:

camera_command = "X"

response = "Resetting ..."

else:

camera_command = cmd[0].upper()

response = "Moving {}".format(cmd.capitalize())

# ser.write(camera_command)

return response, 200, {'Content-Type': 'text/plain'}

然后,在模板中,你只需要使用一些JavaScript发送请求即可:

{# in main.html #}

{% for label, command in commands.items() %}

{{ label }}

{% endfor %}

{# and then elsewhere #}

// Only run what comes next *after* the page has loaded

addEventListener("DOMContentLoaded", function() {

// Grab all of the elements with a class of command

// (which all of the buttons we just created have)

var commandButtons = document.querySelectorAll(".command");

for (var i=0, l=commandButtons.length; i

var button = commandButtons[i];

// For each button, listen for the "click" event

button.addEventListener("click", function(e) {

// When a click happens, stop the button

// from submitting our form (if we have one)

e.preventDefault();

var clickedButton = e.target;

var command = clickedButton.value;

// Now we need to send the data to our server

// without reloading the page - this is the domain of

// AJAX (Asynchronous JavaScript And XML)

// We will create a new request object

// and set up a handler for the response

var request = new XMLHttpRequest();

request.onload = function() {

// We could do more interesting things with the response

// or, we could ignore it entirely

alert(request.responseText);

};

// We point the request at the appropriate command

request.open("GET", "/" + command, true);

// and then we send it off

request.send();

});

}

}, true);

2020-04-06

Python Flask框架中,如果你想创建一个网页应用并添加刷新功能,你可以使用`flask_socketio`库结合HTML模板来实现实时数据更新或者手动触发页面刷新。首先,你需要安装Flask-SocketIO: ```bash pip install flask-socketio ``` 然后,在你的Flask应用中导入相关模块,并设置SocketIO实例: ```python from flask import Flask, render_template import flask_socketio app = Flask(__name__) app.config['SECRET_KEY'] = 'your_secret_key' socketio = flask_socketio.SocketIO(app) ``` 接下来,你可以定义一个事件处理函数,例如每隔一段时间发送一个信号来模拟刷新: ```python @socketio.on('refresh') def handle_refresh(): # 模拟数据更新 data_to_send = {'message': 'Page refreshed'} emit_data(data_to_send) def emit_data(data): socketio.emit('update', data, broadcast=True) # 广播给所有连接的客户端 ``` 最后,渲染模板并在其中添加一个JavaScript部分,通过SocketIO连接到服务器并触发刷新事件: ```html <!-- templates/index.html --> <!DOCTYPE html> <html lang="en"> <head> <script src="{{ url_for('static', filename='socket.io.js') }}"></script> </head> <body> <!-- ...其他内容... --> <button onclick="socket.emit('refresh')">Refresh Page</button> <script type="text/javascript"> var socket = io.connect('http://' + document.domain + ':' + location.port); socket.on('update', function(data) { // 更新页面内容 console.log(data.message); // 可能需要的DOM操作... }); </script> </body> </html> ``` 在这个例子中,用户点击“刷新”按钮会触发服务器端的`handle_refresh`函数,进而广播一个`update`事件给所有连接的客户端,浏览器内的JS代码接收到这个事件后更新页面内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值