HOWTO · Python
How to Run Python in HTML
Learn how to run Python in HTML
On this page
A browser does not run server-side Python just because a script is placed in static HTML. To show Python-generated content, run Python on a server and return its output in the HTTP response. A PHP application can start a Python process for a small integration; a Django application usually passes data from a view to a template.
Run Python Scripts in HTML using PHP
Save the page with a PHP-processed extension such as .php, and serve it through a configured PHP server. The server also needs a Python interpreter and permission to start it. This self-contained example creates a short Python script, passes two arguments safely, and places the captured result in the response:
<?php
$pythonScript = <<<'PY'
import sys
name = sys.argv[1]
count = int(sys.argv[2])
print(f"Hello, {name}! Count: {count}")
PY;
$scriptPath = tempnam(sys_get_temp_dir(), 'page-python-');
file_put_contents($scriptPath, $pythonScript);
$command = 'python3 ' . escapeshellarg($scriptPath)
. ' ' . escapeshellarg('Mina')
. ' ' . escapeshellarg('3');
$result = shell_exec($command);
unlink($scriptPath);
?><!doctype html>
<html lang="en">
<body>
<p><?= htmlspecialchars($result ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') ?></p>
</body>
</html>
Here PHP starts python3; the Python process receives Mina and 3 as sys.argv[1] and sys.argv[2]. shell_exec() captures the process output, and htmlspecialchars() encodes it before inserting it into the page. This is response content sent to the browser, not text printed to the server console.
shell_exec() must be enabled by the PHP host, and the web server account must be allowed to run the interpreter. The example uses fixed arguments. For dynamic values, validate them and escape each argument separately with escapeshellarg(); never concatenate untrusted input into a shell command. For a larger application, a job queue or a dedicated service can be a better fit than starting a process for each request.
Run Python script in HTML using Django
In a Django application, put application logic in Python views and pass its result to a template. The template renders that value into HTML on the server; the browser receives the rendered page and does not execute Python. For example, a view can prepare context = {"message": "Hello from Django"} and return render(request, "page.html", context), while the template displays it with {{ message }}.
A custom template tag is useful when reusable presentation logic belongs in templates. In an installed app named core, create core/templatetags/__init__.py and core/templatetags/my_custom_tags.py. In the latter, import template from django, set register = template.Library(), and define a function decorated with @register.simple_tag, such as def greeting(): return "Hello from Django". Restart the development server after adding the templatetags package so Django discovers it.
In a template, load the module with {% load my_custom_tags %} and call the tag with {% greeting %}. The tag returns text for server-side template rendering; it does not run arbitrary Python in browser HTML. For simple values, a view and template context are usually clearer than a custom tag. Django also provides built-in filters such as date, linebreaks, safe, and random; see the Django built-in template filters reference.
If the page needs data after it loads, expose a Django view as an endpoint and request it with fetch() or another AJAX client. Check authentication and authorization before returning sensitive data or performing an action. For state-changing POST requests, use Django’s CSRF protection and send the CSRF token. Restricting an endpoint to POST alone does not make it secure. See the Django CSRF documentation.
Choose PHP process invocation when integrating a small existing Python script into a PHP-hosted site and the host permits it. For a Django application, views and templates are the usual way to compute and render page data; use a custom tag for reusable template presentation and an endpoint for data requested after page load.