hakiemtheman
Posts: 2
Joined: Mon Jan 20, 2020 7:22 pm

Running a Python script from a HTML page

Mon Jan 20, 2020 7:41 pm

Hi,

I'm currently working on a project to be able to open my garage door via an app or a website (running on Raspberry Pi).

I currently have a Python script which I can run directly from the prompt which is controling the GPIO which in turn is triggering my garage door to open and close ... which is nice! 8-)

I'm now trying to trigger the Python script via a webpage which just simply has a button ('open door'). I need to use root to set the GPIO pins and therefore I'm using 'sudo' to run Python.

I've looked around but I cannot seem to find out how to do this. Does anyone have any experience with this kind of thing? Thanks ..

hippy
Posts: 6720
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Running a Python script from a HTML page

Tue Jan 21, 2020 2:32 pm

The best way to do it is have a Python program which can deliver web pages, a HTTP server.

You get your initial web page from that. Then when you click the door open or close button that causes a new page request which indicates which button was pushed. The Python program can take that page request to determine what you did, change its GPIO appropriately, and return the new page for your browser.

tqhien
Posts: 67
Joined: Thu Feb 02, 2012 10:07 am

Re: Running a Python script from a HTML page

Tue Jan 21, 2020 2:34 pm

Hi,

As your main program is already in python, I would suggest to use python-flask to serve html pages and respond to button actions.

Here is a tutorial link that may help you :

https://www.hackster.io/mjrobot/python- ... -pi-41b5fc

hippy
Posts: 6720
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Running a Python script from a HTML page

Tue Jan 21, 2020 3:10 pm

Flask or some other framework can be used, but I prefer a purer Python solution without dependencies on frameworks. A very simplistic example of a virtual light bulb handler is below. This is specifically for Python 2 but should be easy enough to change for Python 3.

Code: Select all

#!/usr/bin/python2

import BaseHTTPServer
import urllib

# .-----------------------------.
# | Virtual Light Bulb Handling |
# `-----------------------------'

bulbOn = False

def SetBulbOn():
  global bulbOn; bulbOn = True

def SetBulbOff():
  global bulbOn; bulbOn = False

# .----------------------.
# | URL Request Handling |
# `----------------------'

def HandleUrlRequest(path):
  url = urllib.unquote(path).strip()
  if   url.endswith("turnon")  : SetBulbOn()
  elif url.endswith("turnoff") : SetBulbOff()

# .------------------------.
# | Response Page Handling |
# `------------------------'

def GetBulbOnPage():
  return """<html><body><center>
            <h1>Bulb is On</h1>
            <a href='turnoff'>Turn Bulb Off</a>
            </center></body></html>"""

def GetBulbOffPage():
  return """<html><body><center>
            <h1>Bulb is Off</h1>
            <a href='turnon'>Turn Bulb On</a>
            </center></body></html>"""

def GetPage():
  if bulbOn : return GetBulbOnPage()
  else      : return GetBulbOffPage()

# .-------------.
# | HTTP Server |
# `-------------'

class HttpHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  def do_GET(self):
    HandleUrlRequest(self.path)
    self.send_response( 200 )
    self.send_header("Content-type", "text/html")
    self.end_headers()
    self.wfile.write(GetPage())

BaseHTTPServer.HTTPServer(("", 8123), HttpHandler).serve_forever()
Access it as "http://localhost:8123" from a browser on the Pi it is running on. Replace "localhost" with your Pi's IP address if accessing it from a PC on your network.

The SetBulbOn() and SetBulbOff() routines can be updated to control actual GPIO and a physical light bulb.

hakiemtheman
Posts: 2
Joined: Mon Jan 20, 2020 7:22 pm

Re: Running a Python script from a HTML page

Thu Jan 30, 2020 9:07 pm

Thanks you guys for your responses...


I managed to get it working via Flask which was quite easy. The link helped a lot! 8-)

User avatar
DAI-X
Posts: 36
Joined: Fri Nov 27, 2015 11:19 pm
Location: sutton coldfield, uk

Re: Running a Python script from a HTML page

Fri Jan 31, 2020 1:59 pm

Hippy I have been looking for a python-only solution for AGES. Thanks for posting this!

phonx
Posts: 1
Joined: Sat Feb 08, 2020 3:30 am

Re: Running a Python script from a HTML page

Sat Feb 08, 2020 3:35 am

Hi;

do you still have the code, would you be able to share
I just purchase a few relay modules and would like to make a
controller for my garage door .


Thank

Phon

Return to “Advanced users”