Have you created a python script, which needs a user interaction and gives output according to user requests? In the world of programming python is one the easiest and powerful high-level programming language. So, once you are done with writing a python code first issue arises is code portability, means how user will integrate with your code. For user interaction nothing can be better than a simple website hosted online as we can use the power of WWW (World Wide Web) and get users from all over the internet.So, Let’s see a step by step guide to Run a Python script on clicking HTML button:

For using python as a back-end for HTML site, we have two most popular frameworks name Flask & Django. In this post we will implement this task using Django Web Framework.


Are you worried about Django?

So, guys it seems you are new to this Django/Flask and things are looking like implementing rocket science, but that’s not the thing here I am damn sure even if you don’t know D of Django or F flask you will be able to compete this task of running python script on clicking html button.
Let’s have a look on step by step video tutorial:
 
 

Transcript/ Cheat sheet:

 
Views.py =>












from django.shortcuts import render
import
requests
def button(request):
return render(request,home.html)
def output(request):
data=requests.get(https://www.google.com/)
print(data.text)
data=data.text
return render(request,home.html,{data:data})

Urls.py =>








from django.conf.urls import url
from
django.contrib import admin
from . import views
urlpatterns = [
url(r^admin/, admin.site.urls),
url(r^$, views.button),
url(r^output, views.output,name=script),
]
Templates =>
Home.html

<!DOCTYPE html>
<html>
<head>
<title>
Python button script
</title>
</head>
<body>
<button onclick=location.href='{% url ‘script’ %}’>Execute Script</button> <hr>
{% if data %}
{{data | safe}}
{% endif %}
</body>
</html>

Whole Project Github Repo