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?

Ansel Santosa
  • 7,206
  • 1
  • 24
  • 39

1 Answers1

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
Max Worg
  • 2,824
  • 2
  • 17
  • 33
  • 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. – Andrea Guglielmi Jan 25 '15 at 23:24
  • 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. – Max Worg Jan 25 '15 at 23:35
  • 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? – Andrea Guglielmi Jan 25 '15 at 23:43
  • 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. – Max Worg Jan 25 '15 at 23:49
  • 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? – Andrea Guglielmi Jan 25 '15 at 23:56
  • Your `myTemperatureControl.py` script can just write the values of `output` to a database or file and the web server can just read the database instead of calling to a Python script. I updated my answer with details. – Max Worg Jan 26 '15 at 00:03
  • using Pymongo as your database is a great and easy option http://api.mongodb.org/python/current/tutorial.html – Max Worg Jan 26 '15 at 00:04
  • What do you think to create the web server code as a thread called by my program `myTemperatureControl.py` and finally it works in parallel? It's hard to share data between threads? A database is a good idea, finally I will log the temperature in a database for sure, but I'm asking if there is a method that use the memory and not writing a file for general purpose data. – Andrea Guglielmi Jan 26 '15 at 00:12
  • Sure - that is certainly an option. If you start your code from inside the webserver then you won't have to use a database at all and you will always have access to the `output` value. It's not hard to communicate between threads, no. – Max Worg Jan 26 '15 at 00:19
  • I will try. I case I need help I will post the code. Thanks – Andrea Guglielmi Jan 26 '15 at 00:26
  • Sure - write some code and let me know if you have any questions along the way. – Max Worg Jan 26 '15 at 00:28