Flask - 在按钮OnClick事件上调用python函数 [英] Flask - Calling python function on button OnClick event

查看:4706
本文介绍了Flask - 在按钮OnClick事件上调用python函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python和Flask的新手。
我有一个带按钮的Flask Web App。当我点击按钮时,我想执行python方法而不是Javascript方法。我怎么能这样做?

I am new to python and Flask. I have a Flask Web App with a button. When I click on the button I would like to execute a python method not a Javascript method. How can I do this?

我见过python的例子,它使用像这样的表格标签将我重定向到一个新的页面

I have seen examples with python where it redirects me to a new page using a form tag like this

<form action="/newPage" method="post">

但我不希望它将我重定向到新页面。我只是想让它执行python方法。
我正在为Raspberry Pi机器人车做这个。当我按下前进按钮时,我希望它运行方法来向前转动车轮。

but I don't want it to redirect me to a new page. I just want it to execute the python method. I am making this for a Raspberry Pi robot car. When I press the forward button, I want it to run the method to Turn wheels forward.

按钮HTML代码( index.html

<button name="forwardBtn" onclick="move_forward()">Forward</button>

简单的app.py代码 - move_forward()方法位于此处

simple app.py Code - move_forward() method located here

#### App.py code

from flask import Flask, render_template, Response, request, redirect, url_for
app = Flask(__name__)

@app.route("/")
def index():
    return render_template('index.html');

def move_forward():
    #Moving forward code
    print("Moving Forward...")

我在Stackoverflow上遇到了类似的问题,但他们似乎没有回答我的问题,或者我无法理解答案。如果有人可以请给我一个简单的方法来在按钮点击事件上调用Python方法,我们将非常感激。

I have seen similar questions to mine on Stackoverflow, but they don't seem to answer my question or I am unable to understand the answer. If someone could please provide me a simple way to call a Python method on button click event, it would be greatly appreciated.

我看过的其他问题:

- 使用Python Flask调用函数按钮

- 使用按钮调用python函数

- Flask Button在没有刷新页面的情况下运行Python?

推荐答案

您可以在AJAX的帮助下完成此操作...
这是一个调用python函数的示例,它打印hello而不重定向或刷新页面。

You can simply do this with help of AJAX... Here is a example which calls a python function which prints hello without redirecting or refreshing the page.

在app.py下面放置代码段。

In app.py put below code segment.

//rendering the HTML page which has the button
@app.route('/json')
def json():
    return render_template('json.html')

//background process happening without any refreshing
@app.route('/background_process_test')
def background_process_test():
    print "Hello"
    return "nothing"

您的json.html页面应如下所示。

And your json.html page should look like below.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type=text/javascript>
        $(function() {
          $('a#test').bind('click', function() {
            $.getJSON('/background_process_test',
                function(data) {
              //do nothing
            });
            return false;
          });
        });
</script>


//button
<div class='container'>
    <h3>Test</h3>
        <form>
            <a href=# id=test><button class='btn btn-default'>Test</button></a>
        </form>

</div>

当您在控制台中按下Test simple按钮时,您可以看到Hello
显示没有任何刷新。

Here when you press the button Test simple in the console you can see "Hello" is displaying without any refreshing.

这篇关于Flask - 在按钮OnClick事件上调用python函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆