Flask Button运行Python而无需刷新页面?

阿迪尔·马利克(Adil Malik)

我刚开始使用python和flask(对于树莓派)。我想要一个可以执行一些python代码来平移和倾斜摄像机并显示视频流的Web应用程序。

到目前为止,我的flask代码是:

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, debug=True)

问题是我的代码依赖于重定向到新目录的每个按钮,尽管效果很好,但每次都会刷新页面,这意味着我的嵌入式视频会重新加载和缓冲。有没有更好的方法来检测按下按钮,然后使用flask执行python代码?

肖恩·维埃拉(Sean Vieira)

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

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('/<cmd>')
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() %}
    <button class="command command-{{ command }}" value="{{ command }}">
        {{ label }}
    </button>
{% endfor %}

{# and then elsewhere #}
<script>
// 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<l; 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);
</script>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何从html页面[javascript]调用Webservice方法而无需刷新页面

来自分类Dev

提交电子邮件表单而无需刷新页面NO JQUERY

来自分类Dev

在覆盖div或工具提示中显示woocommerce消息,而无需刷新页面

来自分类Dev

定期更新页面上的信息,而无需刷新页面

来自分类Dev

在Django中实现“像这样”按钮,而无需刷新页面

来自分类Dev

验证并提交表单,而无需刷新页面

来自分类Dev

更新页面上的数据而无需刷新

来自分类Dev

Python Flask发送警报而无需重新加载整个页面

来自分类Dev

使用Javascript更新HTML div,而无需刷新页面

来自分类Dev

Spotify Web Player-更新URL而无需刷新页面

来自分类Dev

添加帖子并显示它,而无需刷新页面Codeigniter?

来自分类Dev

Flask Jinja2更新div内容而不刷新页面

来自分类Dev

角度-自动更新数据而无需刷新页面

来自分类Dev

一次更新一个输入字段而无需刷新页面

来自分类Dev

淡入淡出来自同一URL的多个图像而无需刷新页面

来自分类Dev

使用laravel更新vue中的数据,而无需刷新页面或重叠div

来自分类Dev

显示img元素而无需刷新页面

来自分类Dev

通过ajax提交表单,并通过JSON从servlet进行回复,而无需刷新页面

来自分类Dev

通过ajax更新数据库,而无需刷新页面codeigniter

来自分类Dev

从数据库获取/获取数据,而无需刷新页面

来自分类Dev

使用数据库查找插入表单值而无需刷新页面

来自分类Dev

刷新包含PHP代码的div,而无需刷新页面

来自分类Dev

单击Perfom asp按钮,处理VB代码并执行Jscript,而无需刷新页面

来自分类Dev

在提交时更新DIV,而无需刷新页面

来自分类Dev

使用Javascript更新HTML div,而无需刷新页面

来自分类Dev

通过URL触发多个单独的JavaScript函数,而无需刷新页面

来自分类Dev

运行查询代码以从数据库检索数组,而无需刷新页面

来自分类Dev

自动刷新Google Maps Infobubble中的数据,而无需刷新页面

来自分类Dev

在HTML页面和Java servlet之间交换数据而无需刷新页面

Related 相关文章

  1. 1

    如何从html页面[javascript]调用Webservice方法而无需刷新页面

  2. 2

    提交电子邮件表单而无需刷新页面NO JQUERY

  3. 3

    在覆盖div或工具提示中显示woocommerce消息,而无需刷新页面

  4. 4

    定期更新页面上的信息,而无需刷新页面

  5. 5

    在Django中实现“像这样”按钮,而无需刷新页面

  6. 6

    验证并提交表单,而无需刷新页面

  7. 7

    更新页面上的数据而无需刷新

  8. 8

    Python Flask发送警报而无需重新加载整个页面

  9. 9

    使用Javascript更新HTML div,而无需刷新页面

  10. 10

    Spotify Web Player-更新URL而无需刷新页面

  11. 11

    添加帖子并显示它,而无需刷新页面Codeigniter?

  12. 12

    Flask Jinja2更新div内容而不刷新页面

  13. 13

    角度-自动更新数据而无需刷新页面

  14. 14

    一次更新一个输入字段而无需刷新页面

  15. 15

    淡入淡出来自同一URL的多个图像而无需刷新页面

  16. 16

    使用laravel更新vue中的数据,而无需刷新页面或重叠div

  17. 17

    显示img元素而无需刷新页面

  18. 18

    通过ajax提交表单,并通过JSON从servlet进行回复,而无需刷新页面

  19. 19

    通过ajax更新数据库,而无需刷新页面codeigniter

  20. 20

    从数据库获取/获取数据,而无需刷新页面

  21. 21

    使用数据库查找插入表单值而无需刷新页面

  22. 22

    刷新包含PHP代码的div,而无需刷新页面

  23. 23

    单击Perfom asp按钮,处理VB代码并执行Jscript,而无需刷新页面

  24. 24

    在提交时更新DIV,而无需刷新页面

  25. 25

    使用Javascript更新HTML div,而无需刷新页面

  26. 26

    通过URL触发多个单独的JavaScript函数,而无需刷新页面

  27. 27

    运行查询代码以从数据库检索数组,而无需刷新页面

  28. 28

    自动刷新Google Maps Infobubble中的数据,而无需刷新页面

  29. 29

    在HTML页面和Java servlet之间交换数据而无需刷新页面

热门标签

归档