【问题标题】:How to run a python file in HTML when button is pressed按下按钮时如何在HTML中运行python文件
【发布时间】:2022-01-24 18:26:28
【问题描述】:

当用户点击登录按钮时,它应该运行一个python文件,这是一个检查用户登录信息的程序。 我对编码和 html 很陌生,所以我不太确定它是否可能。 代码

from flask import Blueprint
auth = Blueprint("auth", __name__)

@auth.route('/login')
def login():
    return "<p>Login</p>"

#Here is where I'd like to run the python script

@auth.route('/logout')
def logout():
    return "<p>Logout</p>"

【问题讨论】:

    标签: python html


    【解决方案1】:

    可以使用 PHP 在 html 中运行 python 文件。

    写一个PHP文件为“index.php”:

    <html>
    <head>
    <title>RUN MY PYTHON FILES</title>
    <?PHP
    echo shell_exec("python test.py 'param1'");
    ?>
    </head>
    

    将参数传递给python,创建一个python为“test.py”:

    import sys
    input=sys.argv[1]
    print(input)
    

    打印PHP传递的参数。 如果您的参数需要支持空格,请尝试这样做:

    echo shell_exec("python test.py \"Param 1\"")
    

    【讨论】:

    • 您好,您能更详细地解释一下您所做的工作吗?例如,我在哪里“导入系统”?在 HTML 文件或 PHP 文件中?我是否将“test.py”替换为我要执行的 python 文件?
    • 带有import sys的第二部分是test.py文件的内容。只是一些可以使用参数的示例。是的,您将 test.py 更改为您的文件名。
    【解决方案2】:

    你可以只创建一个函数,让它做你想做的事:

    def your_function():
        # Put whatever it should do here
    

    然后,当您想运行您在函数中编写的代码时,您可以调用它。例如,如果您希望它在您的登录功能中运行,请执行以下操作:

    @auth.route('/login')
    def login():
        your_function()
        return "<p>Login</p>"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 2021-01-04
      相关资源
      最近更新 更多