19
votes

Currently I'm having some python files which connects to sqlite database for user inputs and then performs some calculations which sets the output of the program. I'm new to python web programming and I want to know what is the best method to use python on web?

Ex: I want to run my python files when the user clicks a button on the web page. Is it possible?

I started with Django. But it needs some time for the learning. And I also saw something called CgiScripts. Which option should I use? Please advice.

7

7 Answers

7
votes

It probably would depend on what you want to do. I personally use CGI and it might be simpler if your inputs from the web page are simple, and it takes less time to learn. Here are some resources for it:

However you may still have to do some configuring to allow it to run the program instead of displaying it.

Here's a tutorial on that: http://httpd.apache.org/docs/current/howto/cgi.html

10
votes

You can able to run a python file using html using php

write a PHP file as index.php:

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

passing the parameter to python create a python as test.py:

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

print the parameter passed by PHP.

3
votes

If your web server is apache you can use the http://modpython.org/ module in order to run your python CGI scripts.

For nginx you can use http://modwsgi.readthedocs.io/en/develop/

2
votes

Thanks to WebAssembly and the Pyodide project it is now possible to run Python in the browser. Check out my tutorial on it.

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

You can't run python directly

You may use this

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

Or for inside php this

http://www.skulpt.org/
0
votes

You should try flask or django frameworks. They are used to integrate python and html

0
votes

There is a way to do it with flask!

Installation

First you have to type pip install flask

Setup

You said when a user clicks on a link you want it to execute a python script

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)