Currently I have some Python files which connect to an SQLite database for user inputs and then perform some calculations which set 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 the web?

Example: 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 CGI scripts. Which option should I use?


You are able to run a Python file using HTML using PHP.

Add a PHP file as index.php:

<title>Run my Python files</title>
echo shell_exec("python 'parameter1'");

Passing the parameter to Python

Create a Python file as

import sys

Print the parameter passed by PHP.


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: Apache Tutorial: Dynamic Content with CGI


If your web server is Apache you can use the mod_python module in order to run your Python CGI scripts.

For nginx, you can use mod_wsgi.


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

// Init pyodide
languagePluginLoader.then(() => { output.value += 'Ready!n' })

function evaluatePython() {
        .then(output => addToOutput(output))
        .catch((err) => { addToOutput(err) })
<!DOCTYPE html>

    <script type="text/javascript">
        // Default Pyodide files URL ('packages.json', '', etc.)
    <script src=""></script>

    <textarea id='output' style="width: 100%;" rows="10" disabled></textarea>
    <textarea id='code' rows="3">
import numpy as np
    <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>


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

There is a way to do it with flask!


First you have to type pip install flask


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
def  HomePage():
    return "<a href="">EXECUTE SCRIPT </a>"

#Once it redirects here (to localhost:5000/runscript) it will run the code before the return statement
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__":

You can’t run Python code directly

You may use Python Inside HTML.

Or for inside PHP this:


Not the answer you’re looking for? Browse other questions tagged python html web or ask your own question.