Plot the map with origin and destination airport routes based on an input csv file in real time


A python job will parse the message stream and write required airport route details in a csv file every 5 seconds.

Able to load that initial content of the csv file, and plot the map with origin and destination airport route.
However, if the csv file updates with any new content, then it is not reflecting in the map.

Have tried to use “callback and interval” to accomplish interactive, but not able to make it to reflect the changes in real time.

Could you please advise me to “plot the map with origin and destination airport routes in real time based on an input csv file updates”.

My code:

import re
import pandas as pd
import plotly.graph_objects as go
import os

import dash
from dash import Dash, callback_context, no_update
from dash import dcc
from dash import html
from dash.dependencies import Input, Output


df_airports = pd.read_csv(’./output/Airports_New.csv’)
df_flight_paths = pd.read_csv(’./output/Airports_New.csv’)

Extract the data

src_arp = df_airports[‘src_arp’].values
dest_arp = df_airports[‘dest_arp’].values
src_lat = df_airports[‘src_arp_lat’].values
src_long = df_airports[‘src_arp_long’].values
dest_lat = df_airports[‘dest_arp_lat’].values
dest_long = df_airports[‘dest_arp_long’].values

app = Dash(name)

App layout

app.layout = html.Div(
html.H1(“Route input_csv_file_which_will_be_updated_frequently

Stats Dashboard”, style={‘text-align’: ‘center’}),

    dcc.Graph(id='book_stats_map', figure={}),
    dcc.Interval(id='map_update', interval=5000, n_intervals=0)        


Connect the Plotly graphs with Dash Components

Output(‘book_stats_map’, ‘figure’),
Input(‘map_update’, ‘n_intervals’)
def update_map(n):

fig = go.Figure()

flight_paths = []
for i in range(len(df_flight_paths)):
            locationmode = 'USA-states',
            lon = [df_flight_paths['src_arp_long'][i], df_flight_paths['dest_arp_long'][i]],
            lat = [df_flight_paths['src_arp_lat'][i], df_flight_paths['dest_arp_lat'][i]],
            hoverinfo = 'text',
            text = [df_airports['src_arp'][i], df_airports['dest_arp'][i]],
            mode = 'markers+lines',
            line = dict(width = 1,color = 'red'),

        #transition_duration = 500,
        showlegend = False,
        geo = dict(
            resolution = 50,
            showland = True,
            showlakes = True,
            landcolor = 'rgb(247, 253, 195)',
            countrycolor = 'rgb(204, 204, 204)',
            lakecolor = 'rgb(15, 160, 215)',
            projection_type = "cylindrical stereographic",
            coastlinewidth = 2,
            lataxis = dict(
                range = [20, 60],
                showgrid = True,
                dtick = 10
            lonaxis = dict(
                range = [-100, 20],
                showgrid = True,
                dtick = 20
return fig

if name == ‘main’:

Any help on this topic is much appreciated. Thanks.

You must load df_airports from within the callback for it to update.

1 Like

Thank you so much :+1: