Yes, following are the codes to update Mapbox plot:
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
import pandas as pd
import plotly.graph_objs as go
from dash.dependencies import Input, Output
import os
from app import app
from utilities import unix_time_hour, dataforstorm, branchdata
from apps.glob_path import LOCAL_DATA_STORE, PRESCIENT_SOURCE_DATA
import datetime
if ‘DYNO’ in os.environ:
app_name = os.environ[‘DASH_APP_NAME’]
else:
app_name = ‘dash-mapplot’
mapbox_access_token = “pk…”
dfbus = branchdata.busnameidlatlng()
df = pd.read_csv(os.path.join(LOCAL_DATA_STORE, ‘Line_Flow_all.csv’))
global_starttime = None
default_starttime = unix_time_hour.convert_sec(df.datetime.min())
global_starttime_second = None
layout = html.Div([
dbc.Row(
dbc.Col(html.Div(id=‘to-derecho-2’), width={“size”: 8, “offset”: 1}, ),
),
dbc.Row(
dbc.Col(html.Div(id=‘to-performance-2’), width={“size”: 8, “offset”: 1}, ),
),
dbc.Row(
dbc.Col(html.Div(id=‘to-regional-2’), width={“size”: 8, “offset”: 1}, ),
),
dbc.Row(
dbc.Col(html.Div(id=‘to-overshort-2’), width={“size”: 8, “offset”: 1}, ),
),
html.Div(
[
dbc.Row(
[
dbc.Col(html.Div(id=‘show-datetime’),
style={‘textAlign’: ‘center’, ‘colore’: ‘red’}
),
]
),
dbc.Row(
[
dbc.Col(width=2),
dbc.Col(
dbc.Col(dcc.Graph(id="map-graph2")), width=8),
dbc.Col(width=2)
]
),
dbc.Row(
[
dbc.Col(html.Div([dcc.RangeSlider(id="day-range2", updatemode='mouseup',
min=unix_time_hour.convert_sec(df.datetime.min()),
max=unix_time_hour.convert_sec(df.datetime.max()),
marks=unix_time_hour.get_marks_from_start_end_m(
df.datetime.min(),
df.datetime.max()),
value=[unix_time_hour.convert_sec(df.datetime.min()),
unix_time_hour.convert_sec(df.datetime.max())],
pushable=0
),
],
),
width={"size": 8, "offset": 2},
),
],
),
dbc.Row(
[
dbc.Col(html.Div(html.P(' '))),
]
),
dbc.Row(
[
dbc.Col(html.Div(html.P(' '))),
]
),
dbc.Row(
[
dbc.Col(html.Div('<br>', id='output-datetime-range-slider'),
width={"size": 8, "offset": 2},
),
]
),
],
),
html.Div(id='intermediate-value', style={'display': 'none'})
]
)
def rounder(t):
if t is not None:
if t.minute >= 30:
return t.replace(second=0, minute=0, hour=t.hour)
else:
return t.replace(second=0, minute=0)
else:
return t
def daytimeconvertor(seconds):
return datetime.datetime.fromtimestamp(seconds + 7 * 3600)
@app.callback(
Output(‘intermediate-value’, ‘children’), [Input(“day-range2”, “value”)])
def reset_global_starttime(timedate):
return get_global_starttime(timedate[0])
@app.callback(
Output(“map-graph2”, “figure”),
[Input(“day-range2”, “value”), Input(“map-graph2”, ‘hoverData’)])
def update_graph(hours, busname):
starttime = rounder(global_starttime)
dstart = df.loc[df.datetime == str(starttime)]
dstart['text'] = "Line: " + dstart['Line'] + "<br> " + "From " + dstart['From Bus'] + " To " + dstart[
'To Bus']
trace_1 = []
start_build_data=time.tome()
trace_1.append
go.Scattermapbox(hoverinfo='text',
lat=[dstart['From lat'], dstart['To lat'],
lon=[dstart['From lng'], dstart['To lng'],
mode="lines+markers",
showlegend=True,
line=go.scattermapbox.Line(width=4, color='red'),
text=dstart.text + '<br>' + "Flow (mw): " + str(dstart.Flow),
marker=go.scattermapbox.Marker(
size=10,
color='rgb(255,10,150)',
opacity=0.7
),
name=dstart['From Bus'] + "-" + dstart['To Bus'],
),
)
layout1 = go.Layout(hovermode='closest', height=940,
geo=dict(projection=dict(type="equirectangular"), ),
margin=go.layout.Margin(l=50, r=50, b=100, t=100, pad=4),
mapbox={'accesstoken': mapbox_access_token, 'bearing': 1, 'center': {'lat': 34, 'lon': -116},
'pitch': 1, 'zoom': 6, "style": 'light'})
print('time used to build data', time.time()-start_build_data)
return {"data": trace_1, "layout": layout1}
The number of the line is 3776.
Please let me know how I can improve the performance.
Thanks.
Pengchu