. Advertisement .
..3..
. Advertisement .
..4..
As a web developer, you frequently need to do tasks using a computer language like PHP or Python. This guide will show you two different methods to run Python scripts In HTML.
Method 1: Utilizing PHP To Run Python Scripts In HTML
You can utilize Hypertext Preprocessor or PHP to run a Python script in HTML. A simple example is shown in the code below.
Add a file in HTML as index.html.
<html>
<head>
<title>Running a Python script</title>
<?PHP
echo shell_exec("python script.py");
?>
</head>
<body>
<!-- BODY -->
</body>
</html>
Create a file in Python as script.py.
a = 2000
b = 21
print(f"a = {a}")
print(f"b = {b}")
print(f"a + b = {a + b}")
The console will then display the information below.
a = 2000
b = 21
a + b = 2021
Suppose you wish to pass certain values to a Python script; you can apply the code as follows.
Create a file in HTML as index.html.
<html>
<head>
<title>Running a Python script</title>
<?PHP
echo shell_exec("python script.py "Parameter #1" "Parameter #2"");
?>
</head>
<body>
<!-- BODY -->
</body>
</html>
Below is how the script will appear.
Add a file in Python as script.py.
import sys
a = sys.argv[1]
b = sys.argv[2]
print(f"a = {a}")
print(f"b = {b}")
print(f"a + b = {a + b}")
In this case, the output remains similar to the example above.
Method 2: Utilizing Django To Run Python Scripts In HTML
Django is one of the most well-known and reliable web frameworks in Python. It can more easily be run inside HTML because this framework is Python-based. To execute this method, you will use the template tags. Some built-in tags include random, safe, linebreaks, date, etc.
You can also design your own template tags due to Django’s customizability. With the template tags, you may give back data to the HTML templates, which may be embedded within the template.
Here is how to make a template tag. Let’s say you already have the application core in this Django project.
Inside the application core, make a templatetags directory. It should appear as below.
core/
__init__.py
models.py
admin.py
views.py
urls.py
...
templatetags/
__init__.py
...
Next, make a file in Python called my_custom.tags.py and place it within the folder templatetags.
Put the code below inside of the file.
from django import template
register = template.Library()
@register.simple_tag
def my_tag():
return "Hello World from my_tag() custom template tag."
All of your custom tags will be stored in the module my_custom_tags.py. As you can see, the my_tag is a newly constructed template tag that may now be utilized in every HTML template. It will give back the string Hello World from my_tag() custom template tag to your template. You can make more tags here to carry out particular and standard tasks.
Now, load and use your first custom tag in the HTML template.
{% load static %}
{% load my_custom_tags %}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<title>Intro</title>
</head>
<body>
<p>{% my_tag %}</p>
</body>
</html>
Load the module my_custom_tags.py inside your template. Once it is done, you can now utilize the tags provided in my_custom_tags. Notice that before utilizing any custom tags, the module with such tags must first be loaded.
You may also establish end-points and send them AJAX requests to carry out an operation or obtain data instead of utilizing the template tag. To send the AJAX request, you can utilize jquery, fetch(), or other access methods.
It is crucial to make sure the end-point is safe and prevents simple access to private data or site functionalities when creating one to handle the AJAX request. You can also put CSRF validation to the end-point and set it up to only handle POST requests. Keep in mind that CSRF tokens should be included in the POST form data.
The Bottom Line
So there you have the methods to run Python scripts In HTML. Hopefully, this guide with many examples can make your learning process easier.Bonus: learn more about HTML multiple classes in this detailed guide here!
Leave a comment