有什么办法可以把 Python 代码放到 HTML 中吗?

Is there any way to put This Python code into HTML?

我想把这个 python 脚本放到这个 html 脚本中

Python代码:

username = input("Username: ")
password = input("Password: ")


database = [
    [ "Java" , "1234"],
    [ "Python" , "5678"],
    [ "HTML" , "910"]
]

if [username, password]in database:
    print("Access Granted")
if [username, password]not in database:
    print("Access Denied")

Html代码:

<html>
<body>
</body>
</html>

对于此类任务,有 frameworks/libraries 个用于此目的。例如,您可以将 Flask 或 Django 与视图引擎一起使用。

我想您的问题是您想在 HTML 网页上显示此 python 代码? 如果这是你的问题,那么我将添加我自己的解决方案。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Button Center</title>
  <style type="text/css">
    *{
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    body{
      width: 100vw;
      height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .program{
      background-color: #000;
      height: 350px;
      width: 400px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    code{
      font-family: Consolas,"courier new";
      color: #ffff;
      padding: 2px;
      font-size: 105%;
    }
    .var{
      color: #FFC107;
    }
  </style>
</head>
<body>
  <div class="program">
    <code>
    <span class="var">username</span> = input("Username: ")<br>
    <span class="var">password</span> = input("Password: ")<br>
    <span class="var">database </span> = [<br>
        [ "Java" , "1234"],<br>
        [ "Python" , "5678"],<br>
        [ "HTML" , "910"]<br>
    ]<br>
    if [username, password]in database:<br>
        print("Access Granted")<br>
    if [username, password]not in database:<br>
        print("Access Denied")<br>
   </code>
  </div>
   
</body>
</html>

已为您解决!虽然还没有测试过

<html>
<body>
username = input("Username: ")
password = input("Password: ")


database = [
    [ "Java" , "1234"],
    [ "Python" , "5678"],
    [ "HTML" , "910"]
]

if [username, password]in database:
    print("Access Granted")
if [username, password]not in database:
    print("Access Denied")
</html>
</body>

python代码:

from flask import Flask, render_template, request
app = Flask(__name__)

@app.route('/verification', methods=['POST'])
def example():
    database = [["Java", "1234"], ["Python", "5678"], ["HTML", "910"]]
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        result = True if [username, password] in database else False
        return render_template('index.html', result=result)

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

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

html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>verification</title>
</head>
<body>
<form action="/verification" method="post">
    <input type="text" name="username" placeholder="please enter username">
    <input type="text" name="password" placeholder="please enter password">
    <button type="submit">submit</button>
</form>
<h1>{{result}}</h1>
</body>
</html>

只是一个例子,这就是你要找的吗?