2231puppy
Posts: 5
Joined: Sat Mar 09, 2019 11:02 pm
Location: Annanale, VA, USA

Running Python Scripts in HTML

Fri Jul 12, 2019 2:29 pm

Hello,

I want to make a website to control my robot with. I know how to set up the website and I can do some basic HTML. My motor controller board for the robot is controlled via python. Here is the code to make one wheel go forwards:

Code: Select all

from adafruit_motorkit import MotorKit
kit=motorKit()
kit.motor1.throttle = 1.0
I would need to repeat the last line three more times to make all four of the wheels go forwards. PHP is really hard for me, but that is the only language that the online tutorials have. Once I have the basic code, I can duplicate it to make it go in all directions. Also, I don't quite know how to make buttons in HTML(But I can in Python fine), so if someone could explain how to run the Python code when a button is clicked, that would be nice. Sorry if I seem a little grouchy, I'm really exhausted.

Thanks,
2231puppy


:)

User avatar
neilgl
Posts: 520
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Running Python Scripts in HTML

Fri Jul 12, 2019 8:52 pm

A simple html & php page that runs python scripts when buttons are pressed is:

Code: Select all

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<?php
if(isset($_POST["on"])) {
    exec('python /var/www/html/on.py');
}
if(isset($_POST["off"])) {
    exec('python /var/www/html/off.py');
}
?>
<form method="POST" />
  <input type="submit" value="On" name="on" /><p>
  <input type="submit" value="Off" name="off" />
</form>
</body>
</html>
It used apache2 on the pi and apache2 user 'www-data' was added to to the gpio group.

Andyroo
Posts: 3376
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Running Python Scripts in HTML

Fri Jul 12, 2019 9:03 pm

Another option is to look at Flask - Python itself is the webserver and you can incorporate normal and GPIO commands into web pages

https://pythonspot.com/flask-web-app-with-python/
Need Pi spray - these things are breeding in my house...

2231puppy
Posts: 5
Joined: Sat Mar 09, 2019 11:02 pm
Location: Annanale, VA, USA

Re: Running Python Scripts in HTML

Sat Jul 13, 2019 2:21 pm

Thanks for the help! I have what I need now to get it working.

Return to “Advanced users”