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 =>
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
Thanks for sharing the information
python training institute in south delhi
python training institute in Noida
Excellent post. I was always checking this blog, and I’m impressed! Extremely useful info specially the last part, I care for such information a lot. I was exploring this particular info for a long time. Thanks to this blog my exploration has ended. video agentur
Very helpful
I also have a projects in my website
https://educationlinks.in
Oh my goodness! a tremendous article dude. Thank you Nonetheless I'm experiencing difficulty with ur rss . Don know why Unable to subscribe to it. Is there anybody getting equivalent rss problem? Anybody who is aware of kindly respond. Thnkx webflow developers
Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!Thanks 统计代写
It is very difficult to find the movie of your choice particularly in horror movies genre. New-age online shopping portals are offering many titles under this category.
0123movie
When it comes to movie distribution for an indie produced film the way it normally happens are independent producers and filmmakers take the risk making the movie without any guaranteed movie distribution deal in place.
fmovies.co
SoundCloud is the ultimate platform for musicians around the world to gain recognition for their tracks. soundcloud plays
Thank you for some other informative website. The place else may just I get that kind of information written in such a perfect method? I have a venture that I am simply now running on, and I’ve been at the glance out for such info. brand design firm
Sure, you can scour the internet and find scads of great topless or semi-nude and suggestive pix of Anderson, but that's not Scully, damnit! Bravo to Chris Carter and Gillian Anderson…uh…sort of.123movie
Also, most films don't get a theatrical release, and the funds are earned through other channels.
0123movie
Additionally, it doesn’t matter how extremely you should be complete with treating a very platform, gradually you’ll discover an instance the places you should want to do a number instruct care; and as a consequence influenced by your real age but also bodybuilding, therfore the inches of one’s caravan it could be a awfully excessive pastime. motor movers web development firms
Hello I found the Free Simple Shopping Icons Download | Design, Tech and Internet post very interesting therefore I’ve included our track-back for it on my own webpage, continue the great job:) web design agency new york
Come across back yard garden unusual periods of one’s Are generally Weight reduction and every one one may be important. One way state could possibly be substantial squandering through the diet. lose weight website design company nyc
There is greaser violence (not Grease greaser violence but like the Outsiders with a 90's humor twist), there is great Rockabilly music from Glen Glenn, Hasil Adkins and Johnny Reno!
https://ww2.0123movie.net/
I used to be recommended this web site by my cousin. I’m no longer sure whether this put up is written via him as nobody else know such exact approximately my problem. You’re amazing! Thank you! web design agency new york
Immigration Lawyers… […]here are some links to sites that we link to because we think they are worth visiting[…]… website design company nyc
Awesome blog, I’m going to spend more time researching this topic branding agencies in san francisco
This is a well-researched article which you have shared here about edit the uploaded image using external python. This is a very informative and useful article. This type of advice is not easy to find so, thanks for this information Watermark Edit Image Online
hello there, your style is so good.Following your posts.