如何在HTML中运行python脚本?

时间:2016-11-28 12:56:25

标签: python html web

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

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

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

7 个答案:

答案 0 :(得分:2)

这可能取决于你想做什么。我个人使用CGI,如果您对网页的输入很简单,并且学习时间较短,则可能会更简单。以下是一些资源:

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

这是一个关于它的教程:http://httpd.apache.org/docs/current/howto/cgi.html

答案 1 :(得分:2)

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

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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

你不能直接运行python

您可以使用此

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

或者对于内部php这个

http://www.skulpt.org/

答案 4 :(得分:0)

您可以使用 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传递的参数。

答案 5 :(得分:0)

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

答案 6 :(得分:0)

有一种方法可以用烧瓶做到这一点!

<块引用>

安装

首先你必须输入 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)