-
Notifications
You must be signed in to change notification settings - Fork 15
/
app_london.py
41 lines (33 loc) · 1.33 KB
/
app_london.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from flask import Flask, render_template, Response
from pykafka import KafkaClient
from pykafka.common import OffsetType
def get_kafka_client():
return KafkaClient(hosts='127.0.0.1:9092')
app = Flask(__name__)
@app.route('/')
@app.route('/index.html')
def index():
PAGE_TITLE='London Bus Live Map'
MAP_URL_TEMPLATE = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
MAP_ATTRIBUTION = '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors';
MAP_STARTING_CENTER = [51.5075249,-0.1162361];
MAP_STARTING_ZOOM = 14;
MAP_MAX_ZOOM = 18;
KAFKA_TOPIC = 'geodata_stream_topic_123'
# https://stackoverflow.com/questions/12096522/render-template-with-multiple-variables
return(render_template('index.html', **locals()))
#Consumer API
@app.route('/topic/<topicname>')
def get_messages(topicname):
client = get_kafka_client()
def events():
for i in client.topics[topicname].get_simple_consumer(
# The 2 following parameters avoid recovering old data
# to only get latest/new data
auto_offset_reset=OffsetType.LATEST,
reset_offset_on_start=True
):
yield 'data:{0}\n\n'.format(i.value.decode())
return Response(events(), mimetype="text/event-stream")
if __name__ == '__main__':
app.run(debug=True, port=5001)