2

I'm new in python development. I have a program for control some sensors (I/O) that is running in a loop While True:.

I would like to create a web page where I could see some values, some variables from my program. Searching on the web, I found many info but I can't understand how to do: I saw many web framework that allow to listen html requests, and when I tried, they worked basically.

I miss how I can interact between the script python that listen web requests and my python program. Should I create the web listener as thread launched by my main program? Using some kind of global variables?

1 Answer 1

1

A simple web server approach for you to interact with your Python scripts from a web interface would be to use Python bottle.

Here is a basic Python bottle program you could use for your purposes:

     from bottle import route, run

        @route('/')
        def hello():
           #using jquery
            return """<script> poll get_temp with JavaScript here</script><div id="temp">temp will update here</div>"""

        @route('/get_temp')
        def getTemp():
           temp = readDataBaseForTemp()
           return temp

        run(host='localhost', port=8080, debug=True)

when you start this program you can use a browser to interact with it on http://localhost:8080/ <--- this will trigger the JavaScript that will poll the server for the temp. Obviously it's not complete but it's the general idea.

The idea here is that JavaScript just makes a call to the Python webserver (using URL http://localhost:8080/get_temp) which triggers your Python myTemperatureControl script. When your script has executed and returns a temperature value it will then send the data back to the JavaScript that asked for it so that the webpage can be updated accordingly.

As for your myTemperatureControl.py script you can send the output of the temperature readings to a common place that will be accessible to the webserver. Typically you would have a database setup for this purpose.

while True: 
   if temperature > 30: 
      output = 1 
   else: 
      output = 0
   #update database or file with output
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks, your example is clear but is more or less what I already saw. But.. it's very important now the next step :-) I mean, my script, where I'm handling the sensor is already running, I should run many lines of code.. just to make it clear: please imagine I have a running program where checking a temperature I raise an output when temperature is over a threshold. I would like to see on my web page the status of the output and the temperature. If I call the script ONLY when the web page is opened, the output is not controlled every time. The program should run indipendently. I hope is clear.
What you need is simple. Set up the bottle server to serve the browser a web page that has a simple JavaScript function that calls the server to check on the temp every so often and reports the updates in the webpage. Your web browse only has to open the web page once and the JavaScript will do the rest.
Ok, but, how javascript can access to python variable, this is my question. Is not an issue how to show the value on the screen, the problem is how I can get the temperature. For example: inside getTemp() above there is yourPythonScript(), but the variable is handled inside a different running program like myTemperatureControl.py... so how I can?
I updated my answer with more details. The order is that the JavaScript makes a request to the web server who then calls your myTemperatureControl.py script, gets a temperature value and then sends it back to the JavaScript so that the web page can be updated. The Javascript can be in a constant loop running every few seconds to always have the latest temperature information.
I can't call myTemperatureControl.py, it's already running with a code like this: while True: if temperature > 30: output = 1 else: output = 0 time.sleep(1). How I can write a method, a function in order to get values?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.