How can I run Python in HTML for web-based interactions?
I have Python files that connect to an SQLite database, process user inputs, and perform calculations to generate outputs. I want to integrate these Python scripts into a web application so they run when a user clicks a button on a web page.
As someone new to Python web programming, I’m looking for the best way to use Python in HTML effectively.
For instance, how can I run my Python scripts from an HTML page? I have explored Django, but its learning curve is steep. I’ve also come across CGI scripts. Which is the better option for running Python in a web environment, or are there other approaches I should consider?
Hello All!
“Using Flask”
Flask is a lightweight Python web framework that makes it easy to integrate Python with HTML. Here’s how you can do it:
- Install Flask with
pip install flask
.
- Create a Flask app to connect your Python scripts to the HTML frontend.
- Use the
@app.route()
decorator to map Python functions to specific HTML endpoints.
Example Code:
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/run_script', methods=['POST'])
def run_script():
# Call your Python function here
result = my_python_function()
return f"Output: {result}"
if __name__ == "__main__":
app.run(debug=True)
This way, when a user clicks a button on the web page, Flask handles the request and runs your Python script.
Hey @dharapatel.130
Adding to @madhurima_sil’s approach, if you want to explore a more traditional setup, you can use a Common Gateway Interface (CGI) script."*
The CGI module is one of the older ways to link Python and HTML. While it’s less common today, it’s a good foundation for understanding server-client communication.
Steps:
- Place your Python scripts in the
cgi-bin
directory of your web server.
- Configure your server to handle
.cgi
files.
Example Code:
#!/usr/bin/env python3
import cgi
print("Content-Type: text/html\n")
print("<html><body>")
print("<h1>Python CGI Script</h1>")
print("<p>Script executed successfully!</p>")
print("</body></html>")
When you navigate to the script’s URL, it runs the Python code and returns the output to the browser. It’s simple, but modern frameworks like Flask are much more powerful and flexible!
Building on what @ishrth_fathima and @madhurima_sil shared, if you’re deploying more complex applications, consider using a Web Server Gateway Interface (WSGI)."*
WSGI is a standard for serving Python applications in scalable environments. It’s ideal when using frameworks like Flask or Django for handling HTML templates and routing, with WSGI managing communication between the web server and your Python app.
Example WSGI App:
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
message = "<html><body><h1>Python WSGI Application</h1></body></html>"
return [message.encode('utf-8')]
To deploy this, use a WSGI server like gunicorn or uwsgi:
gunicorn myapp:application
This approach offers scalability and works seamlessly with modern web servers. Combining WSGI with frameworks like Flask makes your applications production-ready!
Let me know if you have further doubts!