drekthrall
Posts: 44
Joined: Sat Oct 17, 2015 12:43 pm

write python output to webpage

Tue Dec 08, 2015 4:40 pm

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 ?

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: write python output to webpage

Tue Dec 08, 2015 5:39 pm

http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

drekthrall
Posts: 44
Joined: Sat Oct 17, 2015 12:43 pm

Re: write python output to webpage

Tue Dec 08, 2015 7:37 pm

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 :?

Return to “Python”