
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can Python Functions Run in HTML Like JavaScript?
Unlike JavaScript, we cannot run Python functions (or scripts) directly in HTML, but we can use tools to make it work. While creating web pages, we use HTML to structure the content and JavaScript to make the page interactive (directly in the browser).
We can also write the JavaScript code within an HTML file using the <script> tag. If you are learning Python, you might wonder if you can run Python code the same way inside HTML. The short answer is no. You can't run Python code directly in HTML like JavaScript. Web browsers are designed only to understand HTML, CSS, and JavaScript, but not Python.
However, there are methods and libraries that allow Python to work with HTML, either on the back-end or using modern browser-based technologies. Let us explore how.
Why JavaScript Runs Natively in HTML
Web browsers are built with JavaScript engines (like Chrome's V8 or Firefox's SpiderMonkey) that understand and run JavaScript code directly inside HTML pages.
For example, the following JavaScript code runs directly in the browser -
<!DOCTYPE html> <html> <body> <h2 id="greeting"></h2> <script> document.getElementById("greeting").innerText = "Hello from JavaScript!"; </script> </body> </html>
This works instantly without any setup, because all browsers understand JavaScript. They do not have built-in support for Python. We get the following output -
Hello from JavaScript!
Running Python on the Backend (Most Common Way)
You cannot run Python in the browser, but you can still use Python on the server side to generate or process HTML. This is how frameworks like Django and Flask work.
Here is how it works -
- A user opens a webpage.
- The server (running Python code) generates the HTML dynamically.
- The HTML is sent to the browser, where only HTML, CSS, and JavaScript are executed.
Python handles data, logic, and server responses, while JavaScript handles what happens in the browser.
Example using Flask
The following code renders HTML using Python, but Python itself is not running in the browser -
It shows how you can use Python (Flask) to generate HTML content on the server side, and then send that HTML to the browser.
Step 1: Python Flask Code (app.py)
from flask import Flask, render_template app = Flask(__name__) @app.route("/") def home(): return render_template("index.html", name="Python")
Here, In the Python file app.py, a Flask web application is created, and a route is defined for the homepage. When someone visits the homepage, the home() function runs and uses render_template() function to send an HTML file (index.html) to the browser, inserting "Python" into it as the value for the variable name.
Step 2: HTML Template (templates/index.html)
<h2>Hello from {{ name }}</h2>
In the index.html file, the above line uses Jinja2 templating to insert the value of name directly into the HTML. Since we passed name="Python" in the Python code, this line becomes <h2>Hello from Python</h2> when viewed in the browser.
Final Output in the Browser
When you open the webpage, the server runs the Python code to prepare the HTML content. The final result that appears in the browser is a webpage displaying the message "Hello from Python" inside an <h2> heading -
<h2>Hello from Python</h2>
Running Python in the Browser (Using Tools)
There are various tools available, using which we can run Python code directly in the web browser. Some of these change/convert Python code into JavaScript (trans-compile). While others run Python interpreters within the browser.
If we insist on running Python code directly in the browser, we need to rely on these tools. Let's look at these one by one -
Brython (Browser Python)
Brython is a Python 3 implementation that runs in the browser. It allows you to write Python code inside HTML and execute it like JavaScript.
Example using Brython
In this case, Python is working in the browser through Brython's interpreter -
<!DOCTYPE html> <html> <head> <script src="https://cdn.jsdelivr.net/npm/[email protected]/brython.min.js"></script> </head> <body onload="brython()"> <button id="btn">Click me</button> <script type="text/python"> from browser import document def say_hello(event): document["btn"].text = "Hello from Python!" document["btn"].bind("click", say_hello) </script> </body> </html>
Following is the output obtained -
PyScript
PyScript is another tool that is used to run Python in the browser using WebAssembly. It is built on top of Pyodide and allows interaction with the DOM and HTML.
Example using PyScript
PyScript is more powerful, but it is also heavier and still under development. It allows you to run more complex Python code directly within HTML -
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" /> <script defer src="https://pyscript.net/latest/pyscript.js"></script> </head> <body> <py-script> def greet(): print("Hello from PyScript!") greet() </py-script> </body> </html>
We get the output as shown below -
def greet(): print("Hello from PyScript!") greet()
Limitations of Running Python in HTML
Tools like Brython and PyScript are undoubtedly useful, but they come with some limitations -
- Slower performance compared to JavaScript.
- Limited access to Python libraries (especially those that use C).
- Larger page load size due to downloading interpreters.
- Still not widely adopted in production websites.