├── Dynamic Update ├── dynamic1.py ├── finalguess.py ├── mqtt-flask.py └── templates │ ├── dy0.html │ ├── dy1.html │ └── finalguess.html ├── README.md └── mqtt.py /Dynamic Update/dynamic1.py: -------------------------------------------------------------------------------- 1 | 2 | from flask import Flask, jsonify, render_template, request 3 | import webbrowser 4 | import time 5 | 6 | app = Flask(__name__) 7 | 8 | @app.route('/_stuff', methods = ['GET']) 9 | def stuff(): 10 | 11 | return jsonify(result=time.time()) 12 | 13 | 14 | @app.route('/') 15 | def index(): 16 | 17 | return render_template('dy1.html') 18 | 19 | 20 | if __name__ == '__main__': 21 | 22 | app.run() 23 | -------------------------------------------------------------------------------- /Dynamic Update/finalguess.py: -------------------------------------------------------------------------------- 1 | 2 | from flask import Flask, jsonify, render_template, request 3 | import webbrowser 4 | import time 5 | 6 | app = Flask(__name__) 7 | 8 | @app.route('/_stuff', methods = ['GET']) 9 | def stuff(): 10 | lat = time.time()/77500000 11 | longi = time.time()/20800000 12 | 13 | return jsonify(lat=lat,longi=longi) 14 | 15 | 16 | @app.route('/') 17 | def index(): 18 | 19 | return render_template('finalguess.html') 20 | 21 | 22 | if __name__ == '__main__': 23 | 24 | app.run() 25 | -------------------------------------------------------------------------------- /Dynamic Update/mqtt-flask.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, jsonify, render_template, request 2 | from flask_mqtt import Mqtt 3 | import time 4 | 5 | 6 | 7 | lat = 19.22 8 | longi = 72.86 9 | 10 | app = Flask(__name__) 11 | app.config['MQTT_BROKER_URL'] = 'Your server' 12 | app.config['MQTT_BROKER_PORT'] = Your port 13 | app.config['MQTT_USERNAME'] = 'Your Username' 14 | app.config['MQTT_PASSWORD'] = 'Your Password' 15 | app.config['MQTT_REFRESH_TIME'] = 1.0 # refresh time in seconds 16 | mqtt = Mqtt(app) 17 | 18 | @mqtt.on_connect() 19 | def handle_connect(client, userdata, flags, rc): 20 | mqtt.subscribe('lat') 21 | mqtt.subscribe('longi') 22 | 23 | 24 | 25 | @mqtt.on_message() 26 | def handle_mqtt_message(client, userdata, message): 27 | #data = dict( 28 | # topic=message.topic, 29 | # payload=message.payload.decode() 30 | #) 31 | global lat 32 | global longi 33 | topic = message.topic 34 | if topic == 'lat': 35 | lat = float(message.payload.decode()) 36 | print(lat) 37 | if topic == 'longi': 38 | longi = float(message.payload.decode()) 39 | print(longi) 40 | 41 | 42 | @app.route('/_stuff', methods = ['GET']) 43 | def stuff(): 44 | global lat 45 | global longi 46 | 47 | 48 | return jsonify(lat=lat,longi=longi) 49 | 50 | 51 | 52 | @app.route('/') 53 | def index(): 54 | return render_template('finalguess.html') 55 | 56 | if __name__ == '__main__': 57 | 58 | app.run() 59 | -------------------------------------------------------------------------------- /Dynamic Update/templates/dy0.html: -------------------------------------------------------------------------------- 1 | 2 | jQuery Example 3 | 5 | 8 | {% block body %}{% endblock %} -------------------------------------------------------------------------------- /Dynamic Update/templates/dy1.html: -------------------------------------------------------------------------------- 1 | {% extends "dy0.html" %} 2 | {% block body %} 3 | 23 | 24 |

Dynamic Update

25 |

26 | ? 27 | 30 | 31 | 32 | 33 | {% endblock %} 34 | -------------------------------------------------------------------------------- /Dynamic Update/templates/finalguess.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Please work now 5 | 7 | 8 | 9 | 81 | 87 | 88 | 89 |

90 |

Click Anywhere

91 | 92 | 93 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-Flask-dynamic-update- 2 | Here I have put all the files related to the dynamic update of web page with the help of flask and jasonify. 3 | there is a basic code for simply updating the value of a dynamic variable result and then there are codes of connecting this to mqtt clould and google maps 4 | to get the location through the mqtt cloud server and puting them on google maps 5 | 6 | You can look at the following youtube playlist to understand the code. 7 | https://www.youtube.com/playlist?list=PLFXza2AmUJa-2OQphRF3RLvkhCD0dTOqC 8 | -------------------------------------------------------------------------------- /mqtt.py: -------------------------------------------------------------------------------- 1 | 2 | import paho.mqtt.client as mqtt 3 | import os, urlparse 4 | import serial 5 | 6 | #ser = serial.Serial('com15',9600) 7 | # Define event callbacks 8 | def on_connect(client, userdata, flags, rc): 9 | print("rc: " + str(rc)) 10 | 11 | def on_message(client, obj, msg): 12 | print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload)) 13 | #ser.write(str(msg.payload)) 14 | def on_publish(client, obj, mid): 15 | print("mid: " + str(mid)) 16 | 17 | def on_subscribe(client, obj, mid, granted_qos): 18 | print("Subscribed: " + str(mid) + " " + str(granted_qos)) 19 | 20 | def on_log(client, obj, level, string): 21 | print(string) 22 | try: 23 | mqttc = mqtt.Client() 24 | # Assign event callbacks 25 | mqttc.on_message = on_message 26 | mqttc.on_connect = on_connect 27 | mqttc.on_publish = on_publish 28 | mqttc.on_subscribe = on_subscribe 29 | 30 | # Uncomment to enable debug messages 31 | #mqttc.on_log = on_log 32 | 33 | # Parse CLOUDMQTT_URL (or fallback to localhost) 34 | #url_str = os.environ.get('CLOUDMQTT_URL', 'mqtt://localhost:1883') 35 | #url = urlparse.urlparse(url_str) 36 | #topic = url.path[1:] or 'test' 37 | 38 | # Connect 39 | mqttc.username_pw_set("username", "password") 40 | mqttc.connect('server', port, 60) 41 | 42 | # Start subscribe, with QoS level 0 43 | a = mqttc.subscribe("/frommothership", 0) 44 | b = mqttc.subscribe("lat",0); 45 | b = mqttc.subscribe("project",0); 46 | 47 | # Publish a message 48 | #mqttc.publish("/frommothership","addresses" ) 49 | 50 | # Continue the network loop, exit when an error occurs 51 | rc = 0 52 | while True : 53 | #mqttc.publish("/frommothership", "my message") 54 | rc = mqttc.loop() 55 | print("rc: " + str(rc)) 56 | 57 | except: 58 | exit 59 | #ser.close() 60 | --------------------------------------------------------------------------------