简体   繁体   中英

Sending data from Python to Javascript using only one URL in Flask

I am currently working in a Python application that has to send some data (latitude, longitude) to a Javascript file in order to show the current position of a device in a map. I am sending the data as JSON and I am using Flask to send it, but I faced a problem and I have not found a solution yet.

I know how to send data using Flask:

app = Flask(__name__)

@app.route('/myRoute')
def sendData():
    myDict = {'latitude':46.6205, 'longitude':-15.3826}
    myDictJson = json.dumps(myDict)

    return myDictJson

if __name__ == '__main__':
app.run(host:'localhost', port=9999)

But the thing is that the latitude and the longitude are not fixed, they will change every X seconds and I also need the URL to be fixed. How can I implement it? Should I use something different from Flask?

@app.route(/myRoute) should be: @app.route('/myRoute', methods=["GET"]) , by adding methods=["GET"] you can browse the link and see the return in your browser. The URL will be fixed since you specify it in Flask . Since latitude and longitude change all the time this information should be stored somewhere, just use it in your myDict and request the data over and over again in your javascript .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM