Renu
Renu

Reputation: 29

How to fetch the data from flask webpage and change the button color on another webpage?

I have this application which displays temperature values on a python flask web page. I am working on a webpage which will open this flask web page on a click of button. So the question is, how these two pages can communicate. I would like to access the status or any errors from flask webpage and change the color of the button accordingly. Can someone please help me with any examples or code snippets? thanks

Upvotes: 0

Views: 894

Answers (1)

Der Henning
Der Henning

Reputation: 219

Do you have access to the flask application? I would suggest to build an endpoint, that provides the needed data as json. Your second webpage can then access the data via XMLHttpRequest (JavaScript) and process the data and manipulate your DOM.

Endpoint in flask application:

from flask import jsonify

@app.route('/api/data')
def send_data():
    data = {'status': 1}
    return jsonify(data)

Javascript request:

function getStatus()
{
    var url = "yourdomain.com/api/data";
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", url, false );
    xmlHttp.send( null );
    return xmlHttp.responseText;
}

Upvotes: 1

Related Questions