In this Part of Running Python script on clicking html button. I will show you how to Upload a image using Html Form. And Edit the uploaded image using External Python script. And to map the edited image back to Html Page.

Check out this video, code below follows the video to help:
If you like videos like this consider donating $1, or simply turn off AdBlocker. Either helps me to continue making tutorials.
 

Transcript/ Cheat sheet:

 
Html Templates :
 
Home.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 %}
<br><br>
{% if raw_url or edit_url %}
<span>
RAW IMAGE:
<img src={{raw_url}} height=500 width=500>
PROCESSED IMAGE:
<img src={{edit_url}} height=500 width=500>
</span>
{% endif %}
<br><br>
<form action=/external/ method=post enctype=multipart/form-data>
{% csrf_token %}
Input Text:
<input type=text name=param required><br><br>
<br><br>
<input type=file name=image required>
<br><br>
<input type=submit value=Execute External Python Script>
</form>
</body>
</html>
 
 
 

Views.py =>

































import requests
from django.shortcuts import render
import sys
from subprocess import run,PIPE
from django.core.files.storage import FileSystemStorage
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})
def external(request):
inp= request.POST.get(param)
image=request.FILES[image]
print(image is ,image)
fs=FileSystemStorage()
filename=fs.save(image.name,image)
fileurl=fs.open(filename)
templateurl=fs.url(filename)
print(file raw url,filename)
print(file full url, fileurl)
print(template url,templateurl)
out= run([sys.executable,//mnt//e//work//button-python-click//html button external python script//test.py,inp],shell=False,stdout=PIPE)
image= run([sys.executable,//mnt//e//work//button-python-click//html button external python script//image.py,str(fileurl),str(filename)],shell=False,stdout=PIPE)
print(out)
print(image.stdout)
return render(request,home.html,{data:out.stdout,raw_url:templateurl,edit_url:image.stdout})

 

Urls.py =>

 












from django.contrib import admin
from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns = [
url(r^admin/, admin.site.urls),
url(r^$, views.button),
url(r^output, views.output,name=script),
url(r^external, views.external),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

 
Settings.py =>













































































































import os
# Build paths inside the project like this: os.path.join(BASE_DIR, …)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings – unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = &!eli6&)nyb+c4s!b=9=p@&q6@85_@u39$p6+sk23@v0o1iyhs
# SECURITY WARNING: don’t run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
MEDIA_URL = /media/
MEDIA_ROOT = os.path.join(BASE_DIR, media)
# Application definition
INSTALLED_APPS = [
django.contrib.admin,
django.contrib.auth,
django.contrib.contenttypes,
django.contrib.sessions,
django.contrib.messages,
django.contrib.staticfiles,
]
MIDDLEWARE = [
django.middleware.security.SecurityMiddleware,
django.contrib.sessions.middleware.SessionMiddleware,
django.middleware.common.CommonMiddleware,
django.middleware.csrf.CsrfViewMiddleware,
django.contrib.auth.middleware.AuthenticationMiddleware,
django.contrib.messages.middleware.MessageMiddleware,
django.middleware.clickjacking.XFrameOptionsMiddleware,
]
ROOT_URLCONF = buttonpython.urls
TEMPLATES = [
{
BACKEND: django.template.backends.django.DjangoTemplates,
DIRS: [templates],
APP_DIRS: True,
OPTIONS: {
context_processors: [
django.template.context_processors.debug,
django.template.context_processors.request,
django.contrib.auth.context_processors.auth,
django.contrib.messages.context_processors.messages,
],
},
},
]
WSGI_APPLICATION = buttonpython.wsgi.application
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
default: {
ENGINE: django.db.backends.sqlite3,
NAME: os.path.join(BASE_DIR, db.sqlite3),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
NAME: django.contrib.auth.password_validation.UserAttributeSimilarityValidator,
},
{
NAME: django.contrib.auth.password_validation.MinimumLengthValidator,
},
{
NAME: django.contrib.auth.password_validation.CommonPasswordValidator,
},
{
NAME: django.contrib.auth.password_validation.NumericPasswordValidator,
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = en-us
TIME_ZONE = UTC
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = /static/

 

External Python Scripts :

Image.py











import sys
from
PIL import Image
image_fullpath=sys.argv[1]
image_name=sys.argv[2]
img= Image.open(str(image_fullpath))
image_save_path=image_fullpath.replace(image_name,temp.png)
img.rotate(90).convert(LA).save(image_save_path)
print(/media/temp.png)

Test.py

 








import datetime
import sys
time=datetime.datetime.now()
output=Hi %s welcome to Hackanons & time is %s % (sys.argv[1],time)
print(output)
Get Whole Source Code Here