如何在 HTML 中运行 python 脚本?

Posted

技术标签:

【中文标题】如何在 HTML 中运行 python 脚本?【英文标题】:How to run python script in HTML? 【发布时间】:2017-04-12 05:03:33 【问题描述】:

目前我有一些 python 文件连接到 sqlite 数据库以供用户输入,然后执行一些设置程序输出的计算。我是 python web 编程的新手,我想知道在 web 上使用 python 的最佳方法是什么?

例如:我想在用户单击网页上的按钮时运行我的 python 文件。有可能吗?

我从 Django 开始。但它需要一些时间来学习。我还看到了一种叫做 CgiScripts 的东西。我应该使用哪个选项?请指教。

【问题讨论】:

请查看此链接:-***.com/questions/7460938/… 【参考方案1】:

您可以使用 php

使用 html 运行 python 文件

写一个 php 文件为 index.php:

<html>
<head>
<title>run my python files</title>
<?PHP
echo shell_exec("python test.py 'parameter1'");
?>
</head>

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

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

打印PHP传递的参数。

【讨论】:

整洁!要在您的论点中支持空格,请尝试这样做:echo shell_exec("python test.py \"Parameter 1\"");【参考方案2】:

这可能取决于你想做什么。我个人使用 CGI,如果你从网页输入的内容很简单,它可能会更简单,而且学习时间更少。这里有一些资源:

https://docs.python.org/2/library/cgi.html https://www.tutorialspoint.com/python/python_cgi_programming.htm

但是您可能仍然需要进行一些配置以允许它运行程序而不是显示它。

这里有一个教程:http://httpd.apache.org/docs/current/howto/cgi.html

【讨论】:

非常感谢您的回复。我浏览了这个例子,我正在寻找类似的东西。但是我需要遵循一个目录结构来保存我的文件吗?如何运行文件?请澄清。 我将所有 CGI 程序保存在一个目录 (cgi-bin) 中并配置该目录以便执行程序。我将进行编辑以添加指向教程的链接。 非常感谢。我开始在 cgi 中运行程序。 没问题!祝你好运! @AndrewAnderson 是的,我想是的【参考方案3】:

如果您的网络服务器是 apache,您可以使用 http://modpython.org/ 模块来运行您的 python CGI 脚本。

对于 nginx,你可以使用http://modwsgi.readthedocs.io/en/develop/

【讨论】:

mod_python 项目早已死去,不应该用于任何新事物。 Apache 的选项,而不是您拥有的 nginx,是 mod_wsgi,但 Google 代码站点是不再使用的旧站点,请改用 modwsgi.org。【参考方案4】:

感谢 WebAssembly 和 Pyodide 项目,现在可以在浏览器中运行 Python。看看我的tutorial就可以了。

        const output = document.getElementById("output")
        const code = document.getElementById("code")

        function addToOutput(s) 
            output.value += `>>>$code.value\n$s\n`
            output.scrollTop = output.scrollHeight
            code.value=''
        

        output.value = 'Initializing...\n'
        // init pyodide
        languagePluginLoader.then(() =>  output.value += 'Ready!\n' )

        function evaluatePython() 
            pyodide.runPythonAsync(code.value)
                .then(output => addToOutput(output))
                .catch((err) =>  addToOutput(err) )
        
<!DOCTYPE html>

<head>
    <script type="text/javascript">
        // default pyodide files URL (packages.json, pyodide.asm.data etc)
        window.languagePluginUrl = 'https://pyodide-cdn2.iodide.io/v0.15.0/full/';
    </script>
    <script src="https://pyodide-cdn2.iodide.io/v0.15.0/full/pyodide.js"></script>
</head>

<body>
    Output:
    </div>
    <textarea id='output' style='width: 100%;' rows='10' disabled></textarea>
    <textarea id='code' rows='3'>
import numpy as np
np.ones((10,))
    </textarea>
    <button id='run' onclick='evaluatePython()'>Run</button>
    <p>You can execute any Python code. Just enter something in the box above and click the button. <strong>It can take some time</strong>.</p>
</body>

</html>

【讨论】:

【参考方案5】:

不能直接运行python

你可以用这个

http://karrigell.sourceforge.net/en/pythoninsidehtml.html

or for inside php this

http://www.skulpt.org/

【讨论】:

【参考方案6】:

您应该尝试烧瓶或 django 框架。它们用于集成python和html

【讨论】:

【参考方案7】:

烧瓶有办法做到这一点!

安装

首先你必须输入pip install flask

设置

你说过当用户点击一个链接时你希望它执行一个 python 脚本

from flask import *
#importing all the methods, classes, functions from flask


app = Flask(__name__)


#This is the first page that comes when you type localhost:5000... it will have a a tag that redirects to a page
@app.route("/")
def  HomePage():
    return "<a href='/runscript'>EXECUTE SCRIPT </a>"

#Once it redirects here (to localhost:5000/runscript) it will run the code before the return statement
@app.route("/runscript")
def ScriptPage():
    #Type what you want to do when the user clicks on the link
    # once it is done with doing that code... it will redirect back to the homepage
    return redirect(url_for("HomePage"))

#Running it only if we are running it directly from the file... not by importing
if __name__ == "__main__":
    app.run(debug=True)

【讨论】:

以上是关于如何在 HTML 中运行 python 脚本?的主要内容,如果未能解决你的问题,请参考以下文章

如何在构建之前使CMake运行python脚本,以便为我的项目生成文件以在构建中使用?

如何在网页中调用python脚本?

在C语言中如何嵌入python脚本

如何运行带有blender参数的python脚本?

Python中如何Debug

如何取消python中运行脚本时 run in unittest