как выполнить импортированную функцию в колбе нажатием кнопки на html файле - PullRequest
0 голосов
/ 11 декабря 2018

Я новичок в колбе.

Я пытаюсь управлять машиной, у которой есть малиновый пи-мозг, и я успешно могу это сделать, выполнив мой сценарий

, теперь я хочувеб-приложение

В моем файле фляги init я импортировал свои самодельные функции, и у меня есть HTML-файл "control.html" .. в этом у меня есть 5 кнопок.теперь, как выполнить мою импортированную функцию по нажатию этих кнопок, и страница не должна перезагружаться.

[код: init .py]

from flask import Flask, render_template
import sys 

''' Importing from my self made file  '''
from keybord_car import carConnect, carRight, carLeft, carFwrd, 
carBack, carStop


app = Flask(__name__)

@app.route('/control')
def control():
    return render_template('control.html')



if __name__ == "__main__":
    app.run(host='192.168.43.205')

[код:contol.html]

<!DOCTYPE html>
<html lang="en">
<head>
    <title>controller web page</title>
</head>
<body>
    <button >Car connect </button>
    <button >Car forward </button>
    <button >Car back </button>
    <button >Car right </button>
    <button >Car left </button> 
</body>
</html>

1 Ответ

0 голосов
/ 11 декабря 2018

Попробуйте некоторые функции сценариев Java.Обновите эти поля в файле control.html

<body>
    <button onclick=buttonFunction(id="1") >Car connect </button>
    <button onclick=buttonFunction(id="2") >Car forward </button>
    <button onclick=buttonFunction(id="3") >Car back </button>
    <button onclick=buttonFunction(id="4") >Car right </button>
    <button onclick=buttonFunction(id="5") >Car left </button> 
    <p id="ID" ></p>
</body>

<script>

function httpGet(theUrl)
{
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
    xmlHttp.send( null );
    return xmlHttp.responseText;
}

function buttonFunction(id) {
  var url="https://192.168.43.205:8080/handler/";
  var response=httpGet(url+id);
  document.getElementById("ID").innerHTML =response;
}
</script>

Добавьте эти строки в файл Python

@app.route("/handler/<ID>",methods=['GET'])
def handler(ID):
    #Your action goes here with corresponding ID
    return "Action Completed | This gets returned to JavaScript Call"
...