communication between python script and html java scrypt

Hi everyone,

First of all i wanna to apologize for my English...
I wanna please You for little help. I wanna to write some code witch will be communication between my python script running on server(Yun) as an infinity loop and html web page with java script localised on http://arduino.local/sd/

my knowladge in this is very poor so i will be very grateful for give me some exampel code... or links

I know that the best way for my project will be some using of javascript jquery ajax program in html witch can GET and POST variables to/from python script but i dont know how to do that...

i don't want to do some html by python script only get/put data to js...

what i wanna to do is:

run python script in boot of yun - I was see some topic's so maby i'll handle with that

this script should do/look something like this:

import time
while True:
x=random(0,99)
POST x to Java Script
GET y from Java Script
c=x+int(y)
POST c to Java Script
time.sleep(60)

Html/JS with some setTimeout("myfunction()",1000) should :

var x=GET x from python
document.getElementById(DivX).innerHTML= x
var y =document.getElementById(DivY).value
POST y to python

Im dont know if that what i posted is understand for anybody... belive it is...

Thanks for help, any advice will be like a gold :slight_smile:

You might want to look into a Python based web framework - I've used Bottle and I hear Flask is similar. Such a framework makes interaction with the JavaScript much simpler.

The general structure of your Python code will be different, but will accomplish the same thing. You will write functions in Python, and the framework will automatically call those functions in response to GET and POST requests from the JavaScript. The framework application acts like a server and the JavaScript makes requests to it.

Your pseudocode uses the term "POST" with the apparent meaning of sending data from your Python code to the JavaScript code. This is not the normal use of that term in this context, it could cause confusion. In a web app like this, the Python code is the server: the JavaScript makes requests, and the Python returns data or files in response. The JavaScript can make GET or POST requests - both are initiated by the JavaScript, with data returned by the Python. The difference between them is not the direction that data flows, but in how the data from JavaScript is encoded: in basic terms, GET is a simpler form where all of the data coming in to the Python code is encoded nnthe URL, while POST allows more features and encodes the data in the body of the request.

Thank for your help :slight_smile:
That is ruin all concept of my program but now its more clarity... and im know what kind of basics i need to know.

sqNick:
That is ruin all concept of my program

Yes, event-based programming is different from the rather linear programming style that is typically used in sketches. Rather than perform a set sequence of events in the same order every time, the application waits for something to happen, and then reacts to it.

In your original pseudocode, it looks like the Python code was in control, and the JavaScript reacted to it. It's much simpler to do the opposite. Re-arranging it, it will likely look more like:

JavaScript:

Every XXX milliseconds:
    var y =document.getElementById(DivY).value
    POST y to python, get x and c as response
    document.getElementById(DivX).innerHTML= x

Python:

When JavaScript POST received:
    y = POSTed data from JavaScript
    x=random(0,99)
    c=x+int(y)
    respond with x, c to JavaScript

The major actions performed by the JavaScript and the Python code are the same - they are just re-arranged and performed under different circumstances. It's not really that big of a change, just a different organization and a different way of thinking.

again big thanks for your answer but i need to figure something else and i don't know how :slight_smile:
In the basic of my concept was that i run python script that will do more task... i wanna to build some grow controller... i plan somthing like this...:
us mcu part as slave to python script

python i infinite loop:

  • get data from leonardo throw the process class like temperatures from few ds18b20, dht22, rtc, etc...
  • get some settings which i can set in browser like time of "on and off relays",limits borders of temperatures etc
  • process this data
  • send readings (of dht, ds ...) to the browser occasionally (only when i wanna to check what is going on in my device)
  • send processed data back to mcu (set's of relays, pwm, IO)

In a start of this project I don't know anything about electronic/programming but thanks to this forum other page's and people like You i can learn and believe that it will be possible to me to do this ambitious project :slight_smile:

Now i thinking about to us 2 python scripts:
one to do everything what i write upper but instat of "browser" send data and get them to/from database
and 2nd webapi in python to get readings from db and set settings to it... via browser

sorry for my english i believe it is understanded
any advice will be welcome :slight_smile:

wish me luck :slight_smile: and thx again