有什么办法可以将此Python代码放入HTML?

时间:2020-11-03 04:28:18

标签: html python-3.x python-3.6 python-3.7

我想将此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>

4 个答案:

答案 0 :(得分:0)

对于这类任务,有为此目的而设计的框架/库。例如,您可以将Flask或Django与视图引擎一起使用。

答案 1 :(得分:0)

我认为您的问题是您要在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>

SnapShot Is added Here

答案 2 :(得分:0)

为您修复! Havent对此进行了测试

<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>

答案 3 :(得分:0)

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>

仅举一个例子,就是您要找的东西吗?