willbsp
Posts: 6
Joined: Wed May 11, 2016 7:21 pm

Display returned value from Python function on HTML webpage

Wed May 11, 2016 7:32 pm

I want to know how I would go about calling a Python function from HTML and then display the result returned on the webpage. I want to display the distance variable in the chkdistance() function as well as call it through the HTML. Any ideas?

Python Code

Code: Select all

def chkdistance ():
    gpio.output(38, False)

    gpio.output(38, True)
    time.sleep(0.00001)
    gpio.output(38, False)

    while gpio.input(40) == 0:
        pulse_start = time.time()
    while gpio.input(40) == 1:
        pulse_end = time.time()

    pulse_duration = pulse_end - pulse_start

    distance = pulse_duration * 17150

    return distance

ghans
Posts: 7882
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Display returned value from Python function on HTML webp

Thu May 12, 2016 8:03 am

Try using this :

https://github.com/dddomodossola/remi

Will be a lot faster than learning Flask/HTML/JavaScript/AJAX
all by yourself.


ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

willbsp
Posts: 6
Joined: Wed May 11, 2016 7:21 pm

Re: Display returned value from Python function on HTML webp

Thu May 12, 2016 5:16 pm

ghans wrote:Try using this :

https://github.com/dddomodossola/remi

Will be a lot faster than learning Flask/HTML/JavaScript/AJAX
all by yourself.


ghans
Thanks, but I can't use the GUI because I am running headless plus I already have most of my HTML code written and only need one value from Python to be displayed. I also want to understand how it would work step by step and write the code.

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

Re: Display returned value from Python function on HTML webp

Thu May 12, 2016 7:11 pm

If you don't want to use the software posted by ghans, which will work from a headless server because it uses a browser so display the gui and not X, then save your functions output to a text file and then read it into your html with javascript, jquery, etc..

cacti
Posts: 56
Joined: Fri Feb 01, 2013 6:52 pm

Re: Display returned value from Python function on HTML webp

Thu May 12, 2016 7:29 pm

If you use PHP you could just execute something like:

Code: Select all

$result = system("/path_to_python_script/python_script.py");

ghans
Posts: 7882
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Display returned value from Python function on HTML webp

Thu May 12, 2016 7:32 pm

remi produces GUI inside Webbrowsers - which can , but don't need to run on the same machine.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

cacti
Posts: 56
Joined: Fri Feb 01, 2013 6:52 pm

Re: Display returned value from Python function on HTML webp

Thu May 12, 2016 7:52 pm

Maybe my previous answer is a bit ambiguous. Here follows a better attempt.

I suppose you have Apache up and running, but just to make sure execute the following:

Code: Select all

sudo apt-get install apache2
To install PHP:

Code: Select all

sudo apt-get install php5 libapache2-mod-php5
This code will execute a directory listing of the files in the /tmp directory. Note that you can execute any command and the output of the command will be displayed in a web page. Just ensure that the command is executable by www-data or the user Apache runs as.

Code: Select all

<BODY>
<HTML>
  <?$result = `/bin/ls -la /tmp`;?>
  <P>The result is:</P>
  <PRE><?=$result?></PRE>
</BODY?
</HTML>
The obvious advantage of using this is that you are not limited to python or any other lanuage, but can display any console output (stdout) in a web page.

dddomodossola
Posts: 42
Joined: Thu Jan 22, 2015 8:56 am

Re: Display returned value from Python function on HTML webp

Thu May 12, 2016 8:34 pm

Ghans is right!
You will not need apache, php and so on... Just Python and REMI library.

Here is your program :-D

Code: Select all

import remi.gui as gui
from remi import start, App

class MyApp(App):
    def __init__(self, *args):
        super(MyApp, self).__init__(*args)

    def main(self, name='world'):
        # the arguments are	width - height - layoutOrientationOrizontal
        wid = gui.Widget(width=300, height=200)
        wid.set_layout_orientation(gui.Widget.LAYOUT_VERTICAL)
        self.lbl = gui.Label('Hello %s!' % name, width='80%', height='50%')
        self.lbl.style['margin'] = 'auto'
        self.bt = gui.Button('Check distance', width=200, height=30)
        self.bt.style['margin'] = 'auto 50px'

        # setting the listener for the onclick event of the button
        self.bt.set_on_click_listener(self, 'on_button_pressed')

        # appending a widget to another, the first argument is a string key
        wid.append(self.lbl)
        wid.append(self.bt)

        # returning the root widget
        return wid

    def chkdistance(self):
        gpio.output(38, False)

        gpio.output(38, True)
        time.sleep(0.00001)
        gpio.output(38, False)

        while gpio.input(40) == 0:
            pulse_start = time.time()
        while gpio.input(40) == 1:
            pulse_end = time.time()

        pulse_duration = pulse_end - pulse_start

        distance = pulse_duration * 17150

        return distance
        
    # listener function
    def on_button_pressed(self):
        self.lbl.set_text('Distance %s ' % self.chkdistance())


if __name__ == "__main__":
    # start(MyApp,address='127.0.0.1', port=8081, multiple_instance=False,enable_file_cache=True, update_interval=0.1, start_browser=True)
    start(MyApp, debug=True)
In order to obtain remi: https://github.com/dddomodossola/remi

willbsp
Posts: 6
Joined: Wed May 11, 2016 7:21 pm

Re: Display returned value from Python function on HTML webp

Thu May 12, 2016 8:51 pm

cacti wrote:Maybe my previous answer is a bit ambiguous. Here follows a better attempt.

I suppose you have Apache up and running, but just to make sure execute the following:

Code: Select all

sudo apt-get install apache2
To install PHP:

Code: Select all

sudo apt-get install php5 libapache2-mod-php5
This code will execute a directory listing of the files in the /tmp directory. Note that you can execute any command and the output of the command will be displayed in a web page. Just ensure that the command is executable by www-data or the user Apache runs as.

Code: Select all

<BODY>
<HTML>
  <?$result = `/bin/ls -la /tmp`;?>
  <P>The result is:</P>
  <PRE><?=$result?></PRE>
</BODY?
</HTML>
The obvious advantage of using this is that you are not limited to python or any other lanuage, but can display any console output (stdout) in a web page.
Thanks for the replies!
I will try what you suggested and see if I can get it to work.

billintad
Posts: 47
Joined: Tue Jul 28, 2015 10:21 pm

Re: Display returned value from Python function on HTML webp

Thu May 12, 2016 8:53 pm

Taking your description at face value I suggest the following :

Use the inbuilt Python http server. Build a simple http server incorporating your GPIO code as a function. This will let you serve either the entire web-page containing your distance information or just the distance information for incorporation into another web-page served from somewhere else. The latter web-page probably requiring the use of javascript, especially if the distance information needs to be refreshed automatically at regular intervals.

For Python 3 : https://docs.python.org/3/library/http.server.html
For Python 2 : https://docs.python.org/2/library/simplehttpserver.html

I would use Python 3 myself.

cacti
Posts: 56
Joined: Fri Feb 01, 2013 6:52 pm

Re: Display returned value from Python function on HTML webp

Fri May 13, 2016 8:34 am

dddomodossola wrote:Ghans is right!
You will not need apache, php and so on... Just Python and REMI library.
Well I suppose that depends what solution is used ;)
All the suggested solutions will work.


Return to “Python”