close

How to run python script in HTML?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to run python script in HTML in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to run python script in HTML?

  1. How to run python script in HTML?

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

  2. run python script in HTML

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

Method 1

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.

Method 2

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>

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read