In this part I continue my tutorial on Running Python Script by Clicking HTML Button. Here I will create a HTML form and submit button & once a user clicks on HTML button, values from HTML Form will be passed to a External Python Script and Output of script will be Mapped to HTML Page.
Before continuing this tutorial i will suggest you to watch the Part 1 of this tutorial where we Configured the Python Django Part for Creating HTML Template.
Python Django Code :
Views.py :
from django.shortcuts import render import requests |
|
import sys | |
from subprocess import run,PIPE | |
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‘) | |
out= run([sys.executable,‘//mnt//e//work//djnago_testing//test.py‘,inp],shell=False,stdout=PIPE) | |
print(out) | |
return render(request,‘home.html‘,{‘data1‘:out.stdout}) |
Urls.py:
|
||
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), | ||
] | ||
Home.html :
External Python Script :
import sys import datetime time=datetime.datetime.now() output="Hi %s currentadfwffffdwadwd time is %s" % (sys.argv[1],time) print(output)
hey man i need your help urgent
i do not want to give any input but just a button that will enable me to execute .py file and print the output it on webpage
HTML CODE:
——————————————————————————————————————————————————–
URLS CODE :
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.button),
path('output', views.output)
]
————————————————————————————————————————————————————
VIEWS CODE :
from django.shortcuts import render
import requests
import sys
from subprocess import run,PIPE
def button(request):
return render(request,'home.html')
def output(request):
out = run(sys.executable,['E://a.py'],shell=False,stdout=PIPE)
print(out)
return render(request,'home.html',{'data':out})
hey man i need your help urgent!
if i using uwsgi +nginx + django
it say unable to load configuration from test.py
your blog was really interesting,I have learnt so much.
python training institute in south delhi
python training institute in noida
the argument is not getting passed to the python script
Page not found (404)
Request Method: POST
Request URL: http://127.0.0.1:8002/external/
Using the URLconf defined in autowpnow.urls, Django tried these URL patterns, in this order:
admin/
output [name='script']
external
The current path, external/, didn't match any of these.
By following the 2nd video it shows this error. Could you provide a solution for this issue
Did you get a solution I don’t know how to do it. Please explain me
hey! its working but i am not getting the output properly!
OUTPUT:
b"Hi Panchal! how's you doing, After evaluating your expression,we have got the value as 30rn"
Expected OUTPUT:
Hi Panchal! how's you doing, After evaluating your expression,we have got the value as 30
Are you using proper web page is that your correct port? are you reading the correct view.external page?
You should have:
path('external/', views.external)
instead of:
path('external', views.external)