drifterf
Posts: 134
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: Running Python Scripts From A Html

Tue Aug 06, 2019 6:15 pm

ghp wrote:
Mon Aug 05, 2019 4:35 am
- the last line of code is missing tornado.ioloop.IOLoop.current().start()
- html code usually has links inside <body>... here goes the page ...</body>. Your page works unexpectedly.
- in the Popen, add ", shell=True". Or try using /usr/bin/python which is more precise.
- the url mappings contain r"/opendoor2/?(.*)" AND r"/opendoor2". So MyOpenDoor2Handler will not be called. Better tu use different names for the url.

When in doubt how to use the subprocess.Popen, then write a short script with these statements only and run this on its own.
hi, im sorry to be a nightmare, i have tried stuff i know, could you provide a example of what you have said please. im really sorry to be a pain

Thank you for your time regardless

ghp
Posts: 1413
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Running Python Scripts From A Html

Tue Aug 06, 2019 8:05 pm

think this could work

Code: Select all

                                                                                        
import tornado.ioloop
import tornado.web
import subprocess
import os


class Python3ScriptRunner:
    def __init__(self):
        pass
    
    def invoke(self, script_name):
        print("call script ", script_name)
        if os.path.isfile(script_name):
            pid = subprocess.Popen(["/usr/bin/python3", script_name]).pid
            print(pid)
        else:
            raise Exception("script name does not exist " + script_name)    



class MyOpenDoor_1_Handler(tornado.web.RequestHandler):
    def get(self):
        print("MyOpenDoor_1_Handler.get()")
        self.write("""Released The Door - Relay 1""")
        
        try:
            Python3ScriptRunner().invoke( "opendoor_1.py")
        except Exception as e:
            self.set_status(500, "Exception " + str(e))
            return
        self.redirect("/opendoor_1_success")

class MyOpenDoor_2_Handler(tornado.web.RequestHandler):
    def get(self):
        print("MyOpenDoor_2_Handler.get()")
        
        try:
            Python3ScriptRunner().invoke( "opendoor_2.py")
        except Exception as e:
            self.set_status(500, "Exception " + str(e))
            return
        self.redirect("/opendoor_2_success")

class MyFormHandler(tornado.web.RequestHandler):
    def get(self):
        print("MyOpenDoor_2_Handler.get()")
        
        self.write("""<html>
                           <body>
                           
                               <a href="/opendoor_1">Release Door - Relay 1</a>
                               <br/>
                               <a href="/opendoor_2">Release Door - Relay 2</a>
                           </body>
                      </html>""")

    def post(self):
        self.set_header("Content-Type", "text/plain")
        self.write("post request for a form, not used")
        

def make_app():
    return tornado.web.Application([
        ( r"/", MyFormHandler),
        ( r"/myform", MyFormHandler),
        
        ( r"/opendoor_1", MyOpenDoor_1_Handler), 
        ( r"/opendoor_2", MyOpenDoor_2_Handler),
        
        ( r"/opendoor_1_success/?(.*)", tornado.web.StaticFileHandler, {"path": "/home/pi/ACU/html/" , "default_filename": "index.html"} ),
        ( r"/opendoor_2_success/?(.*)", tornado.web.StaticFileHandler, {"path": "/home/pi/ACU/html/" , "default_filename": "index.html"} ),
       
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8787)
    tornado.ioloop.IOLoop.current().start()
    
    

drifterf
Posts: 134
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: Running Python Scripts From A Html

Wed Aug 07, 2019 4:14 pm

Hi, no luck :( all i get now is the following in the terminal when i click something
MyOpenDoor_2_Handler.get()
WARNING:tornado.access:404 GET /favicon.ico (::1) 3.04ms
MyOpenDoor_2_Handler.get()
call script opendoor_2.py
ERROR:tornado.access:500 GET /opendoor_2 (::1) 3.56ms
MyOpenDoor_1_Handler.get()
call script opendoor_1.py
ERROR:tornado.access:500 GET /opendoor_1 (::1) 4.82ms
MyOpenDoor_1_Handler.get()
call script opendoor_1.py
ERROR:tornado.access:500 GET /opendoor_1 (::1) 3.70ms
MyOpenDoor_2_Handler.get()
call script opendoor_2.py
ERROR:tornado.access:500 GET /opendoor_2 (::1) 4.46ms
on the webpage when i click on the first link it shows the "released text" but on the second link (the one trying to call the webpage and py script) i just get "this page isnt working, localhost is currently unable to handle this request, http error 500"

if i run the script i posted before it triggers the relay on button 1 and on button 2 does not trigger the relay but shows the webpage so same issue as before

the script you posted i saved as server123.py and have the original server.py in the same directory as each other

ghp
Posts: 1413
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Running Python Scripts From A Html

Wed Aug 07, 2019 5:12 pm

Think the two scripts opendoor_1.py, opendoor_2.py are not available in local directory. Either rename the scripts you have to these names, or adjust the calls in the Handlers. The code fragment is Python3ScriptRunner().invoke( "opendoor_1.py") or Python3ScriptRunner().invoke( "opendoor_2.py").

In my setup, I have a script opendoor_1.py, but no opendoor_2.py (by purpose to check the code).

Here the console output:

WARNING:tornado.access:404 GET /favicon.ico (192.168.2.152) 1.65ms
favicon is searched by the browser by default. This is no problem.
MyOpenDoor_1_Handler.get()
call script opendoor_1.py
1242 // printout of the pid
hello // my script just says hello
MyOpenDoor_2_Handler.get()
call script opendoor_2.py // this script does not exist
ERROR:tornado.access:500 GET /opendoor_2 (192.168.2.152) 1.98ms

drifterf
Posts: 134
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: Running Python Scripts From A Html

Wed Aug 07, 2019 5:38 pm

Hi, thank you so much for your help, i found the issue was actually with the script i was trying to launch.

as i was launching server.py with python3 the old script was missing the brackets from the print part of the script

i changed the old script from

print "door released"

to

print ("door released")

and now it works. i have deffinalty learned something here lol.

i can only appreciate your help GHP !!! appreciated so much !!

drifterf
Posts: 134
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: Running Python Scripts From A Html

Thu Aug 08, 2019 4:14 pm

sorry one last thing (i promise) the main html page (localhost:port) isit possible to put a better html in this location, if i was to create a html in a web design software and then copy replace the code to the

self.write("""<html> part would this show my designed website or can i ask it to read the html the same as staticfilehandler ?

thanks

ghp
Posts: 1413
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Running Python Scripts From A Html

Thu Aug 08, 2019 4:41 pm

StaticFileHandler is a good choice for the start page.
If you need images or css files, add appropriate url-pathes too.

drifterf
Posts: 134
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: Running Python Scripts From A Html

Thu Aug 08, 2019 4:54 pm

hi, thanks thats excellent, i cant appreicate your time enough with me on this !!! thanks !!

drifterf
Posts: 134
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: Running Python Scripts From A Html

Fri Aug 09, 2019 3:43 pm

so much for the last question i promise thing lol

i have it working as i want but made a html file which redirects back to the main page after 5 seconds.

so when you press a button it calls the opendoor_1_success and shows my return.html (which has the javascript redirect in it) but it stays on this page and does not redirect. if i launch the .html on its open it works fine ?

is there something i can code into the .server.py to auto redirect to the previous page after a set time ?

thanks

drifterf
Posts: 134
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: Running Python Scripts From A Html

Sat Aug 10, 2019 6:28 am

can anyone help with this so i can continue my project over the weekend ? thanks

ghp
Posts: 1413
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Running Python Scripts From A Html

Sat Aug 10, 2019 7:49 am

Think tornado can't achieve to change a web page in a browser after some seconds. **
This requirement needs javascript in the page.

Found https://www.tutorialspoint.com/How-to-u ... -5-seconds
The code snippet from there is

Code: Select all

<!DOCTYPE html>
<html>
   <body>
      <script>
         setTimeout(function(){
            window.location.href = 'https://www.tutorialspoint.com/javascript/';
         }, 5000);
      </script>
      <p>Web page redirects after 5 seconds.</p>
   </body>
</html>
** complex answer: it is possible with some overhead:
- open a websocket from browser to server. needs javascript in browser and to configure appropriate Handler in tornado
- listen for messages in browser, again in javascript, and change page if specific message arrives.
- keep the application state on python side and provide timeouts.

drifterf
Posts: 134
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: Running Python Scripts From A Html

Sat Aug 10, 2019 8:45 am

thanks, i have tornado redirecting to a page called return.html and in that page it has that javascript to tell it to direct to a differnt page.

as said if i do it through the localhost:8787 it shows the page once i click the link but never redorects. if i just launch the return.html it works fine

Return to “Python”