Chuyển văn bản từ Python sang HTML

snackfartPosts. 2Đã tham gia. Thứ bảy 28 tháng 9 năm 2019 12. 33 giờ chiều

chuyển biến python sang html [tệp riêng]

  • Trích dẫn

Đăng bởi snackfart » Thứ bảy ngày 28 tháng 9 năm 2019 12. 41 giờ tối

vì vậy tôi muốn chuyển một biến python sang tệp html.
Tôi đang sử dụng NodeMCU.
Làm theo những điều tôi đã thử.
- render_template từ picoweb
- %s trong tệp html.

đây là con trăn chính của tôi.

Mã số. Chọn tất cả

import picoweb
app = picoweb.WebApp[__name__]

def web_page[]:
  global gpio_state
  if led.value[] == 1:
    gpio_state="ON"
  else:
    gpio_state="OFF"
  #return app.render_template['index.html', ledState = gpio_state]
  file = open["index.html", "r"]
  page = file.read[]
  #page = open["index.html"].read[].replace["ledState",gpio_state]
  file.close
  return page
  
s = socket.socket[socket.AF_INET, socket.SOCK_STREAM]
s.bind[['', 80]]
s.listen[5]

while True:
  conn, addr = s.accept[]
  print['Got a connection from %s' % str[addr]]
  request = conn.recv[1024]
  request = str[request]
  print['Content = %s' % request]
  led_on = request.find['/?led=on']
  led_off = request.find['/?led=off']
  if led_on == 6:
    print['LED ON']
    led.value[1]
  if led_off == 6:
    print['LED OFF']
    led.value[0]
  response = web_page[]
  conn.send['HTTP/1.1 200 OK\n']
  conn.send['Content-Type: text/html\n']
  conn.send['Connection: close\n\n']
  conn.sendall[response]
  conn.close[]

và đây là tập tin html của tôi

Mã số. Chọn tất cả





    spotWelder
    
    
    
        html {
            font-family: Helvetica;
            display: inline-block;
            margin: 0px auto;
            text-align: center;
            color: #ff5722;
        }
        
        h1 {
            color: #ff5722;
            padding: 2vh;
        }
        
        p {
            font-size: 1.5rem;
        }
        
        .button {
            display: inline-block;
            background-color: #263238;
            border: none;
            border-radius: 4px;
            color: white;
            padding: 16px 40px;
            text-decoration: none;
            font-size: 10px;
            margin: 2px;
            cursor: pointer;
        }
        
        .button2:hover {
            background-color: #8B0000;
            border-radius: 12px;
        }
        
        .button3:hover {
            background-color: #455a64;
            border-radius: 12px;
        }
        
        .button4:hover {
            background-color: #49599a;
            border-radius: 12px;
        }
        
        input[type=number] {
            width: 20%;
            padding: 12px;
            text-align: center;
            margin: 8px 0;
            box-sizing: border-box;
            border: none;
            background-color: #263238;
            border-radius: 12px;
            color: white;
        }
        
        .buttonSubmit:hover {
            background-color: #49599a;
        }
    




    
        

dual pulse spot welder

GPIO state: %s

FIRE

#welds: 420

Current: 256
Current: 256
Current: 256
M1 M2 M3 M4 M5

save load

Đứng đầu

snackfartPosts. 2Đã tham gia. Thứ bảy 28 tháng 9 năm 2019 12. 33 giờ chiều

Lại. chuyển biến python sang html [tệp riêng]

  • Trích dẫn

Đăng bởi snackfart » Thứ bảy ngày 28 tháng 9 năm 2019 2. 36 giờ chiều

tôi tự sửa
về cơ bản, bạn tách phần css khỏi phần html. hơn định dạng html và thêm nó vào phần css

Mã số. Chọn tất cả

def web_page[]:
  if led.value[] == 1:
    gpio_state="ON"
  else:
    gpio_state="OFF"
  #return app.render_template['index.html', ledState = gpio_state]
  #file = open["index.html", "r"]
  #page = file.read[]
  #html = open["index.html"].read[].format[ledState=gpio_state]
  #file.close
  css = open["index.css"].read[]
  page = css + open["index.html"].read[].format[ledState=gpio_state]
  return page
  
s = socket.socket[socket.AF_INET, socket.SOCK_STREAM] # [address family, TCP Socket]
s.bind[['', 80]]
s.listen[5]

while True:
  conn, addr = s.accept[]
  print['Got a connection from %s' % str[addr]]
  request = conn.recv[1024]
  request = str[request]
  print['Content = %s' % request]
  led_on = request.find['/?led=on']
  led_off = request.find['/?led=off']
  if led_on == 6:
    print['LED ON']
    led.value[1]
  if led_off == 6:
    print['LED OFF']
    led.value[0]
  response = web_page[]
  conn.send['HTTP/1.1 200 OK\n']
  conn.send['Content-Type: text/html\n']
  conn.send['Connection: close\n\n']
  conn.sendall[response]
  conn.close[]

Đứng đầu

Python có thể xuất ra HTML không?

Ngôn ngữ Python ngày nay có những ứng dụng tuyệt vời trong hầu hết mọi lĩnh vực, nó có thể được sử dụng cùng với các công nghệ khác để giúp cuộc sống của chúng ta dễ dàng hơn. Một cách sử dụng python như vậy là lấy dữ liệu đầu ra trong tệp HTML . Chúng tôi có thể lưu bất kỳ lượng dữ liệu đầu vào nào vào tệp HTML trong python bằng cách sử dụng các ví dụ sau theo hai cách.

Có thể nhúng mã Python vào HTML không?

Có thể chạy nhúng Python trong tài liệu HTML có thể được thực thi trong thời gian chạy.

Chủ Đề