Executing a python script from web page


5 posts
by jebukaty » Thu May 18, 2017 9:12 pm
Hi, I was wondering if I could execute a python script from a web page. I have a adafruit motor hat on my pi that I use to control motors. And I use python to write the script for the motors. If someone could point me in the right direction to be able to execute this script from a web page that would be great, thanks.
Posts: 9
Joined: Tue May 02, 2017 4:31 pm
by alphanumeric » Fri May 19, 2017 10:03 am
Why not just clip and past the code into idle (file new) and then save it as a local file? Then run it locally on the Pi.
Posts: 1098
Joined: Tue Jan 19, 2016 2:17 pm
by davef21370 » Fri May 19, 2017 4:34 pm
alphanumeric wrote:Why not just clip and past the code into idle (file new) and then save it as a local file? Then run it locally on the Pi.
Because that wouldn't be executing the scripts from a web page!

I'm no expert on this but found this with various suggestions http://stackoverflow.com/questions/448837/how-do-i-create-a-webpage-with-buttons-that-invoke-various-python-scripts-on-the

stackoverflow.com is always a good place to start.

Dave.
Apple say... Monkey do !!
User avatar
Posts: 868
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded
by irab88 » Fri May 19, 2017 5:14 pm
http://raspberrywebserver.com/cgiscript ... ry-pi.html

This is what I used for my project. While I used text boxes, you can use buttons, check boxes, and more.

Just be sure to set permissions to allow GPIO to be run from a non-root user:

(I wasn't having luck with Jessie, so I used Wheezy for my project)

-$usermod -a -G group user

adding "root", "pi" and "www-data" to the groups "kmem" and "dialout"

However, I was still having issues accessing GPIO, so I had to do the following:

-$sudo nano /etc/rc.local
Code: Select all
...
chmod 777 /dev/mem
chmod 777 /dev/gpiomem
exit 0

(I know it's not the best practice, but it's a $35 hobby project; I'm not too concerned about security.)
Posts: 8
Joined: Mon Apr 22, 2013 7:14 pm
by ghp » Fri May 19, 2017 7:09 pm
Hello,
web applications tend to grow. A nice approach to handle this is a python app server as e.g. tornado. See http://www.tornadoweb.org/en/stable/
Extend the sample from the main page with something which has a button and call a class which does something python-style.
As the app server is already in python, there is almost no barrier.
Hope this helps,
Gerhard
Posts: 1141
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany