Bianco
Posts: 55
Joined: Sun Jul 13, 2014 9:41 pm

Running a Python script from a webpage

Tue Oct 25, 2016 4:18 pm

I made a script which opens and closes my blinds depending on sunrise dans sunset.

Works great.



I would also like to be able to open or close a blind manually during the day from any computer or smartphone (from a webpage served by the Pi).

For this, I'm willing to create three (up, stop, down) scripts per blind but I need your help to build a webpage and create link that will launch the scripts.
I have Apache installed and the demo page tells me "It works!" but that's it, I have no skill in php, html or server stuff.

So what's the easiest way for me to achieve the objective?

scotty101
Posts: 3738
Joined: Fri Jun 08, 2012 6:03 pm

Re: Running a Python script from a webpage

Tue Oct 25, 2016 4:48 pm

You'll need some HTML skills at the very least.

I've integrated python scripts with a simple web interface using Flask. Some links below to a flask tutorial and two projects I've created.

https://blog.miguelgrinberg.com/post/th ... ello-world
https://github.com/scotty3785/pi_tv_rem ... _remote.py
https://github.com/scotty3785/python-bl ... web-app.py

There is also this
https://github.com/dddomodossola/remi
I haven't used it but I believe the concept is to create a simple UI from your application without needing to write any HTML.
Last edited by scotty101 on Tue Oct 25, 2016 4:52 pm, edited 1 time in total.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Running a Python script from a webpage

Tue Oct 25, 2016 4:49 pm

Google webiopi.

Dave.
Apple say... Monkey do !!

User avatar
B.Goode
Posts: 8926
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Running a Python script from a webpage

Tue Oct 25, 2016 5:08 pm

Another forum contributor @penguintutor has an example using the bottle Python library module here: http://www.penguintutor.com/raspberrypi/pi-power

Bianco
Posts: 55
Joined: Sun Jul 13, 2014 9:41 pm

Re: Running a Python script from a webpage

Tue Oct 25, 2016 8:36 pm

Are you sure I need flask?
I'm not developing a Python web app, I just want to lauch the script (wich does some magic calculus and send a few bytes to a GPIO) from the page. I don't need do display any interactive feedback or anything fancy.

This seens awefully simple and yet you come up with complicated solution requiring hours of reading. I was hoping for a <A href="script.py"> tag or something that simple. There's nothing like that?

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: Running a Python script from a webpage

Tue Oct 25, 2016 10:16 pm

add a button to this

Code: Select all

<?php 

$command = escapeshellcmd('script.py');
$output = shell_exec($command);
echo $output;

?>

scotty101
Posts: 3738
Joined: Fri Jun 08, 2012 6:03 pm

Re: Running a Python script from a webpage

Wed Oct 26, 2016 8:21 am

Bianco wrote:Are you sure I need flask?
I'm not developing a Python web app, I just want to lauch the script (wich does some magic calculus and send a few bytes to a GPIO) from the page. I don't need do display any interactive feedback or anything fancy.

This seens awefully simple and yet you come up with complicated solution requiring hours of reading. I was hoping for a <A href="script.py"> tag or something that simple. There's nothing like that?
Mainly agree, but when you decide that it might be nice to have some interaction or some feedback from your script, flask will make it much easier. I found flask to have a very short learning curve (certainly for the basics) and it takes very few lines of code to make it work.

Of course, this is just my own opinion and there are methods that will work if you are unwilling to learn a new skill. Nothing comes for free.
Last edited by scotty101 on Wed Oct 26, 2016 9:41 am, edited 1 time in total.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
B.Goode
Posts: 8926
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Running a Python script from a webpage

Wed Oct 26, 2016 8:56 am

Bianco wrote:Are you sure I need flask?
You don't need flask.

This seens awefully simple and yet you come up with complicated solution requiring hours of reading.
If I had known you did not want to read anything I would not have gone to the trouble of writing a reply.

Return to “Python”