Page 1 of 1

write python output to webpage

Posted: Tue Dec 08, 2015 4:40 pm
by drekthrall
Hello ,

I am using apache2 on my raspberry pi 2 B.

I have html page with button.

After clicking on button i want to run python script on Rspi and grab it's output and show it on my page.

For example after activating button i want to see 30

Code: Select all

a=5
b=6
c=a*b
print c
How to do that ?Can you write me some elementary example of html, python ( or js, php ...) code ?

Re: write python output to webpage

Posted: Tue Dec 08, 2015 5:39 pm
by -rst-

Re: write python output to webpage

Posted: Tue Dec 08, 2015 7:37 pm
by drekthrall
Thanks for response. I read something and here I am now.

this is ajax that call python

Code: Select all

function sensor(){
      $.ajax({
               type:"POST",
               url:"/python/sensor.py",
               dataType:"text",
               success: function(msg){
                                     alert("script returned:" + msg)
                                }
       });
}
my python script that read temperature and pressure from sensor:

Code: Select all

#!/usr/bin/env/ python

import Adafruit_BMP.BMP0855 as BMP085
import cgitb
cgitb.enable()
sensor = BMP085.BMP085()
temp = sensor.read_temperature()
pressure = sensor.read_pressure()

print ("Content-Type: text/html;charset=utf-8")
print ('Temp ={0:0.2f}  *C'.format(temp))
print ('Pressure = {0:0.1f} kPa'.format(pressure/1000))
when i call function sensor() I get this :

Code: Select all

script returned#!/usr/bin/env/ python

import Adafruit_BMP.BMP0855 as BMP085
import cgitb
cgitb.enable()
sensor = BMP085.BMP085()
temp = sensor.read_temperature()
pressure = sensor.read_pressure()

print ("Content-Type: text/html;charset=utf-8")
print ('Temp ={0:0.2f}  *C'.format(temp))
print ('Pressure = {0:0.1f} kPa'.format(pressure/1000))
So it seems like ajax just take code from python script and save it to variable msg :?