-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflights_map.py
42 lines (27 loc) · 1.06 KB
/
flights_map.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
42
from flask import Flask, render_template, Response, stream_with_context
from kafka import KafkaConsumer, TopicPartition
from config import kafka_config
import json
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/adsb/')
def get_messages():
# Instantiate Kafka Consumer object
consumer = KafkaConsumer(
bootstrap_servers=kafka_config['servers'],
value_deserializer=lambda x: json.loads(x.decode('utf-8')))
# Assign given TopicPartition to consumer
t_partition = TopicPartition(kafka_config['topics'][1], 0)
consumer.assign([t_partition])
# Seek to the most recent available offset
consumer.seek_to_end()
def events():
for message in consumer:
# Transform message to event-stream format
message = 'data:{}\n\n'.format(message.value)
yield message.replace('\'', '"')
return Response(events(), mimetype='text/event-stream')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5001)