Have you ever wanted to run a Python script from an HTML file? Maybe you have a web application that needs to execute some Python code, or you want to create a simple HTML interface for a Python script. Whatever the reason, running a Python script from an HTML file is actually quite simple.
What You'll Need
To run a Python script from an HTML file, you'll need a web server and a way to execute the Python script. Here's a list of what you'll need:
- A web server, such as Apache or Nginx
- A Python script that you want to run
- A way to execute the Python script, such as the Python CGI module or a web framework like Flask
Setting Up Your Web Server
The first step is to set up your web server. If you don't already have a web server installed, you can install one using your operating system's package manager. Here are some instructions for installing Apache and Nginx:
Installing Apache
On Ubuntu, you can install Apache with the following command:
sudo apt-get install apache2
On CentOS, you can install Apache with the following command:
sudo yum install httpd
Installing Nginx
On Ubuntu, you can install Nginx with the following command:
sudo apt-get install nginx
On CentOS, you can install Nginx with the following command:
sudo yum install nginx
Once you have your web server installed, you can start it with the following command:
sudo systemctl start apache2
or
sudo systemctl start nginx
You can also enable the web server to start automatically when your system boots with the following command:
sudo systemctl enable apache2
or
sudo systemctl enable nginx
Creating Your HTML File
Now that you have your web server set up, you can create your HTML file. Here's an example HTML file that includes a form that will execute a Python script:
<!DOCTYPE html>
<html>
<head>
<title>Run Python Script</title>
</head>
<body>
<h1>Run Python Script</h1>
<form action="/cgi-bin/run_python.py" method="post">
<label for="input">Enter a message:</label>
<input type="text" id="input" name="input">
<input type="submit" value="Run Script">
</form>
</body>
</html>
This HTML file includes a form that will send a POST request to the /cgi-bin/run\_python.py URL when the user clicks the "Run Script" button. The form includes a text input where the user can enter a message that will be passed to the Python script as a parameter.
Creating Your Python Script
Now that you have your HTML file set up, you can create your Python script. Here's an example Python script that will print the message that was passed to it:
#!/usr/bin/env python
import cgi
form = cgi.FieldStorage()
message = form.getvalue('input')
print('Content-Type: text/html')
print()
print('')
print('')
print('Python Script ')
print('')
print('')
print('Hello, {}!
'.format(message))
print('')
print('')
This Python script uses the cgi module to parse the POST request and retrieve the message that was passed to it. It then prints an HTML page that includes a paragraph that greets the user with the message they entered.
Configuring Your Web Server
Now that you have your HTML file and Python script set up, you need to configure your web server to run the Python script. Here's how to do it for Apache and Nginx:
Configuring Apache
To configure Apache to run the Python script, you need to create a CGI script that will execute the Python script. Here's how to do it:
- Create a directory for your CGI scripts, such as
/var/www/cgi-bin. - Move your Python script to the CGI script directory.
- Make the Python script executable with the following command:
sudo chmod +x /var/www/cgi-bin/run\_python.py
Here's an example Apache configuration file that will execute the Python script:
<VirtualHost \*:80>
ServerName example.com
DocumentRoot /var/www/html
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory /var/www/cgi-bin>
Require all granted
Options +ExecCGI
AddHandler cgi-script .py
</Directory>
</VirtualHost>
This Apache configuration file includes a ScriptAlias directive that maps the /cgi-bin URL to the CGI script directory. It also includes a Directory block that sets the Options directive to include the ExecCGI option and sets the AddHandler directive to execute .py files as CGI scripts.
Configuring Nginx
To configure Nginx to run the Python script, you need to create a location block that will execute the Python script. Here's how to do it:
- Create a directory for your CGI scripts, such as
/usr/lib/cgi-bin. - Move your Python script to the CGI script directory.
- Make the Python script executable with the following command:
sudo chmod +x /usr/lib/cgi-bin/run\_python.py
Here's an example Nginx configuration file that will execute the Python script:
<ifmodule mod\_ssl.c>
ssl\_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl\_prefer\_server\_ciphers on;
ssl\_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DHE+AES128:!ADH:!AECDH:!MD5;
</ifmodule>
server {
listen 80;
server\_name example.com;
root /var/www/html;
location /cgi-bin/ {
gzip off;
expires -1;
add\_header Pragma "no-cache";
add\_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
include fastcgi\_params;
fastcgi\_pass unix:/var/run/fcgiwrap.socket;
fastcgi\_param SCRIPT\_FILENAME /usr/lib/cgi-bin/run\_python.py;
}
location / {
try\_files $uri $uri/ =404;
}
}
This Nginx configuration file includes a location block that maps the /cgi-bin URL to the CGI script directory. It also includes the fastcgi\_params file, which is a configuration file that includes common FastCGI parameters. Finally, it includes the fastcgi\_pass directive, which specifies the location of the FastCGI server, and the fastcgi\_param directive, which specifies the location of the Python script.
Testing Your Setup
Now that you have everything set up, you can test your setup. Here's how to do it:
- Save your HTML file as
index.htmlin the web server's document root directory. - Save your Python script as
run\_python.pyin the CGI script directory. - Start your web server.
- Open a web browser and navigate to
http://example.com. - Enter a message in the text input and click the "Run Script" button.
- You should see a page that says "Hello, <your message>!"
Running a Python script from an HTML file is actually quite simple. All you need is a web server, a Python script, and a way to execute the Python script. By following the steps in this article, you can create a simple HTML interface for your Python scripts.
References
| Title | Author | Date | URL |
|---|---|---|---|
| Installing Apache | Ubuntu | N/A | https://help.ubuntu.com/lts/serverguide/httpd.html |
| Installing Apache | CentOS | N/A | https://docs.centos.org/en-US/8/system-administrators-guide/web-servers/web-servers.html |
| Installing Nginx | Ubuntu | N/A | https://wiki.ubuntu.com/Nginx |
| Installing Nginx | CentOS | N/A | https://docs.centos.org/en-US/8/system-administrators-guide/web-servers/web-servers.html |
| CGI with Python | Python | N/A | https://docs.python.org/3/howto/webservers.html#cgi-scripts |
| FastCGI with Python | Python | N/A | https://docs.python.org/3/howto/webservers.html#fastcgi |
| Flask | Flask | N/A | https://flask.palletsprojects.com/en/2.1.x/ |