题意:我该如何修复我的变量正确地插入到 Python 中 HTML 代码的格式化字符串中?
问题背景:
For a project I'm running a raspberry pi Pico wh based webserver that should get the inputs of the temperature sensor and display it on the website. I am however not very informed about javascript and html. Because my code already looks bad I've removed the css elements. The reason it has to be like this is because the main program needs to run in micropython on my pi.
在一个项目中,我运行了一个基于 Raspberry Pi Pico W 的 Web 服务器,用于获取温度传感器的输入并在网站上显示。然而,我对 JavaScript 和 HTML 并不是很了解。因为我的代码已经显得很乱,所以我移除了 CSS 元素。之所以必须这样做,是因为主程序需要在我的 Pico 上以 MicroPython 的形式运行。
Note: It is however important the sensor and the raspberry keep being the only physical components.
注意:然而,传感器和 Raspberry Pi 必须始终是唯一的物理组件。
I'm hoping someone can give a solution or guidance, maybe even a diffrent aproach.
我希望有人能提供解决方案或指导,甚至是一个不同的实现方法。
Here is my code: 这是我的代码:
#import libraries
from machine import Pin, I2C
import time
import mcp9808
import ssd1306
import network
import socket
#initialize temp sensor
i2c = I2C(1,scl=Pin(3), sda=Pin(2), freq=10000)
mcp = mcp9808.MCP9808(i2c)
#initialize display
i2c = I2C(0, sda=Pin(20), scl=Pin(21))
display = ssd1306.SSD1306_I2C(128, 64, i2c)
#initialize onboard led
led = machine.Pin("LED", machine.Pin.OUT)
led.on()
ssid = 'SSID'
password = 'WIFI_PASSWORD'
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while True:
# get the temperature (float)
SensTemp = mcp.get_temp()
SensTemp = "{:.2f}".format(SensTemp)
#display.poweron()
display.fill(0)
display.text(SensTemp, 0, 0, 1)
display.text("C", 42, 0, 1)
display.show()
html = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Temperature Display</title>
</head>
<body>
<h1>Current Temperature:</h1>
<p id="temperature"></p>
<script>
// Simulated temperature value
var temperatureValue = {SensTemp}; // You can replace this with actual temperature data
// Update the temperature display
function updateTemperature() {
var temperatureElement = document.getElementById("temperature");
temperatureElement.textContent = temperatureValue + "°C";
}
// Call the function to initially display the temperature
updateTemperature();
</script>
</body>
</html>
"""
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else:
print('connected')
status = wlan.ifconfig()
print( 'ip = ' + status[0] )
# Open socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
# Listen for connections
while True:
try:
cl, addr = s.accept()
print('client connected from', addr)
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()
if not line or line == b'\r\n':
break
response = html
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(response)
cl.close()
except OSError as e:
cl.close()
print('connection closed')
Ive tried making the entire html block a formatted string(python), so I can adjust the value but it doesnt work.
我尝试将整个 HTML 块做成一个格式化字符串(Python),以便调整值,但没有成功。
Ive done a few days of research but I'm not much further.
我已经做了几天的研究,但进展不大。
问题解决:
Let's distill the relevant parts of this question.
让我们提炼出这个问题的相关部分。
You have a script that functionally does the following.
你有一个脚本,功能上实现了以下操作。
sensor_temp = 10
html = """
The current temperature is {sensor_temp}.
"""
response = html
print(response)
#> The current temperature is {sensor_temp}.
That is, you acquire a sensor_temp
from somewhere, stick it into a string, and later serve (or here, print) that string. The HTML, javascript, and raspberry pi are all red herrings — this is really a question about string formatting.
也就是说,你从某个地方获取一个 `sensor_temp`,将其插入到一个字符串中,然后稍后提供(或者在这里是打印)该字符串。HTML、JavaScript 和 Raspberry Pi 都是误导因素——这实际上是一个关于字符串格式化的问题。
The easiest thing to do is to preface the string by f
and use fstrings
.
最简单的方法是在字符串前加上 `f`,使用 f-strings。
sensor_temp = 10
# Note the f on the following line, the only change.
html = f"""
The current temperature is {sensor_temp}.
"""
response = html
print(response)
#> The current temperature is 10.
Or you could use old format strings, e.g.
或者你可以使用旧的格式化字符串,例如:
sensor_temp = 10
# Now no f, and no labelled input in braces {} below.
html = """
The current temperature is {}.
"""
response = html.format(sensor_temp)
print(response)
#> The current temperature is 10.
Or you could manually create the string.
或者你可以手动创建字符串。
sensor_temp = 10
html = "The current temperature is " + str(sensor_temp) +".\n"
response = html
print(response)
#> The current temperature is 10.
If this is a broad type of project that you're going to do, it might be worth looking into using the jinja templating engine. This is a small library that plays very well with python.
如果这是一个你将要进行的广泛类型的项目,值得考虑使用 `jinja` 模板引擎。这是一个与 Python 配合得非常好的小型库。
标签:code,string,temp,my,html,print,sensor,response,temperature From: https://blog.csdn.net/suiusoar/article/details/144165797