刷新HTML文件中通过按按钮显示的变量。

huangapple go评论53阅读模式
英文:

Refreshing variables shown in a html file by pressing a button

问题

我有一个从我的树莓派传感器获取数据的程序,我希望从树莓派获取的数据显示在一个网页上,想法是每次按下网页上的按钮时都可以更新数据。我现在正在使用Django来创建这个网页,但我不知道是否对我想做的事情来说太复杂了。

这是我的数据提取器代码:

import bme280
import smbus2
from time import sleep

port = 1
address = 0x76 # Adafruit BME280 address. Other BME280s may be different
bus = smbus2.SMBus(port)

bme280.load_calibration_params(bus,address)

bme280_data = bme280.sample(bus,address)
humidity  = bme280_data.humidity
pressure  = bme280_data.pressure
ambient_temperature = bme280_data.temperature

这是Django的views.py文件:

from django.shortcuts import render
from django.http import HttpResponse
import sys
sys.path.append('/home/jordi/weather-station/data')
from bme280_sensor import humidity, ambient_temperature, pressure
from subprocess import run,PIPE

# 创建您的视图

def home(request):
    return render(request, 'base.html', {'humitat': humidity, 'temperatura': ambient_temperature, 'pressio': pressure})

def index(request):
    return HttpResponse("EL temps de vilafant")

最后,这是我的HTML文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <p>Aqui podras veure el temps de vilafant en un futur, no tinguis presa</p>
    <h1>La humitat d'avui és: {{humitat}}</h1>
    <h1>La temperatura d'avui és: {{temperatura}} Cº</h1>
    <h1>La pressió d'avui és: {{pressio}}</h1>

    <input type="button"><br><br>
</body>
</html>

如何使HTML文件中创建的按钮更新显示在HTML页面中的变量?

我尝试创建一个按钮并链接它,以便它可以再次运行数据提取器文件,但发现页面上显示的变量未更新。

英文:

I have a program that takes data from a sensor in my raspberry pi and I want the data taken from the raspberry to be shown in a web page, the idea is that it can be updated every time you press a button that its in the web. I'm right now using django to create the web, but I don't know if it is too much complex for what I'm trying to do.
Here I have my data extractor code:

import bme280
import smbus2
from time import sleep

port = 1
address = 0x76 # Adafruit BME280 address. Other BME280s may be different
bus = smbus2.SMBus(port)



bme280.load_calibration_params(bus,address)


bme280_data = bme280.sample(bus,address)
humidity  = bme280_data.humidity
pressure  = bme280_data.pressure
ambient_temperature = bme280_data.temperature

Here I have the django views.py file:

from django.shortcuts import render
from django.http import HttpResponse
import sys
sys.path.append(&#39;/home/jordi/weather-station/data&#39;)
from bme280_sensor import humidity, ambient_temperature, pressure
from subprocess import run,PIPE


# Create your views here.

def home(request):
    return render(request, &#39;base.html&#39;, {&#39;humitat&#39;: humidity, &#39;temperatura&#39;: ambient_temperature, &#39;pressio&#39;: pressure})

def index(request):
    return HttpResponse(&quot;EL temps de vilafant&quot;)

and finally here I have my html file:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;p&gt;Aqui podras veure el temps de vilafant en un futur, no tinguis presa&lt;/p&gt;
    &lt;h1&gt;La humitat d&#39;avui &#233;s: {{humitat}}&lt;/h1&gt;
    &lt;h1&gt;La temperatura d&#39;avui &#233;s: {{temperatura}} C&#186;&lt;/h1&gt;
    &lt;h1&gt;La pressi&#243; d&#39;avui &#233;s: {{pressio}}&lt;/h1&gt;

    &lt;input type=&quot;button&quot;&gt;&lt;br&gt;&lt;br&gt;


&lt;/body&gt;
&lt;/html&gt;

How can I make that the button created in the html file updates the variables that are shown in the html page?

I have tried to create a button and link it so it can run the data extractor file another time but found that the variables shown in the page are not updated.

答案1

得分: 0

<section>也许您可以将按钮连接到URL路径,并每隔x秒进行轮询。我建议类似于<a href="https://htmx.org/docs/#polling">这样的</a> </section>

英文:

<section> Maybe you connect the button to a url path and poll every x seconds. I recommend something like
<a href="https://htmx.org/docs/#polling"> this</a> </section>

答案2

得分: 0

问题在于views.py只在启动应用程序时加载一次,因此您的变量不会更改。您需要编写一个函数来重新加载数据,类似于:

sensors.py
(我通过模拟值进行了测试,因此这是一个不同的函数)

...
def update_sensor_data():
    bme280_data = bme280.sample(bus,address)
    data = {    
        'humitat': bme280_data.humidity,
        'pressio': bme280_data.pressure,
        'temperatura': bme280_data.temperature
    }
    return data

views.py

from .sensors import update_sensor_data

def home(request):
    data = update_sensor_data()
    return render(request, 'base.html', data)

然后使用按钮发送GET请求到您的视图,从而重新加载数据并用新值呈现页面:

home.html

<body>
    <p>Aqui podras veure el temps de vilafant en un futur, no tinguis presa</p>
    <h1>La humitat d'avui és: {{humitat}}</h1>
    <h1>La temperatura d'avui és: {{temperatura}} Cº</h1>
    <h1>La pressió d'avui és: {{pressio}}</h1>

    <a href="{% url 'home' %}" >
        <button>Update</button>
    </a>
</body>
英文:

The problem is that because views.py will be loaded only once when you start your application, so your variables will not change. You need to write a function to reload your data, something like:

sensors.py
(I tested by mocking values, so its a different function)

...
def update_sensor_data():
    bme280_data = bme280.sample(bus,address)
    data = {    
        &#39;humitat&#39;: bme280_data.humidity,
        &#39;pressio&#39;: bme280_data.pressure,
        &#39;temperatura&#39;: bme280_data.temperature
    }
    return data

views.py

from .sensors import update_sensor_data

def home(request):
    data = update_sensor_data()
    return render(request, &#39;base.html&#39;, data)

And then use the button to send a GET request to your view thus reloading your data and rendering the page with the new values:

home.html

&lt;body&gt;
    &lt;p&gt;Aqui podras veure el temps de vilafant en un futur, no tinguis presa&lt;/p&gt;
    &lt;h1&gt;La humitat d&#39;avui &#233;s: {{humitat}}&lt;/h1&gt;
    &lt;h1&gt;La temperatura d&#39;avui &#233;s: {{temperatura}} C&#186;&lt;/h1&gt;
    &lt;h1&gt;La pressi&#243; d&#39;avui &#233;s: {{pressio}}&lt;/h1&gt;

    &lt;a href=&quot;{% url &#39;home&#39; %}&quot; &gt;
        &lt;button&gt;Update&lt;/button&gt;
    &lt;/a&gt;
&lt;/body&gt;

huangapple
  • 本文由 发表于 2023年6月29日 01:55:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定