将 HTML 输出返回给 python [英] return HTML output to python

查看:95
本文介绍了将 HTML 输出返回给 python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 python 建立一个本地网站来按下一个按钮,所以我房间的门将通过使用树莓派从我的手机打开.我已经制作了一个 python 程序,如果运行该程序,它将完美地打开门,但是,我正在尝试在 HTML 中创建一个按钮,该按钮将返回一些内容来执行将打开门的文件.

I'm building a local website in python to press a button, so the door to my room will open from my phone by using a raspberry pi. I already have made a python program, which, if ran, will open the door flawlessly, however, I am trying to make a button in HTML that will return something to execute the file which will open the door.

这是我已经拥有的:

from flask import Flask, render_template
from test import open_door

app = Flask(__name__)


@app.route('/open/door')
def doorOpen():
    return render_template('door.html')

@app.route('/opendoor')
def openDoor():
    open_door()
    return 'the door should be open'


if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')

这里是 test.py 文件,执行后会打开门,door.html 如下:

Here is test.py the file which will open the door if executed, and door.html is the following:

<html>
<body>
<a href="/opendoor" class="openbutton">Open for me please</a>
</body>
</html>

这不是什么花哨的东西,但它现在只需要工作.在我看来,返回没有做任何事情,因为我还在 openDoor() 函数中添加了返回和打印函数,而没有任何响应.我在互联网上找不到任何 awnsers 所以我很好奇问题是什么!

It is not something fancy but it only has to work for now. It looks to me like the return is not doing anything since i also added a return and a print function in the openDoor() function withouth any response. I couln't find any awnsers on the internet so i am curious what the problem is!

附言这是我第一次使用 python,我是 HTML 的初学者这是 test.py:

p.s. This is my first time with python and i am a beginner with HTML edit: this is test.py:

import RPi.GPIO as GPIO
import time

testPin = 18

GPIO.setmode(GPIO.BCM)
GPIO.setup(testPin, GPIO.OUT)

counter =0

def open_door():
    try:
        while counter < 900000:
            GPIO.output(testPin, GPIO.HIGH)

            counter += 1

    except:
        print ("Everything is oke!")

    finally:
        GPIO.cleanup()

推荐答案

代替 onclick 函数,您可以将按钮路由到本地主机上的另一个页面:

Instead of an onclick function, you can route the button to another page on your localhost:

from flask import Flask, render_template
from test import open_door
app = Flask(__name__)

@app.route('/')
def doorOpen()
   return render_template('door.html')

@app.route('/opendoor')
def openDoor():
   open_door()

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

door.html 中:

<html>
<body>
<a href="/opendoor" class="openbutton">Open Door</a>
</body>
</html>

在 HTML 文件中,class="openbutton" 用于纯样式目的.

In the HTML file, class="openbutton" is for pure styling purposes.

这篇关于将 HTML 输出返回给 python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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