Hi,
I am working on a small Dash app that shows an average score in a chart by second for a video. I want to be able to show a moving vertical line in the chart when video played, i.e. if video is at 5th second, line should be at x=5 and etc.
My initial idea was to add shapes
and update graph using JavaScript but I don’t know it well and not sure how to pass video time value to Python/Dash.
Code without video:
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(children = [
html.Div(children = [
html.Video(
controls = True,
id = 'movie_player',
),
]),
html.Div(children = [
dcc.Graph(
id = 'mean_chart',
figure = {
'data': [go.Scatter(
x = [0,1,2,3,4,5,6,7,8,9,10],
y = [0,1,2,4,5,4,3,5,3,2,1],
)],
'layout': go.Layout(
xaxis = {
'title': 'Second',
'type': 'linear',
},
yaxis = {
'title': 'Average score',
},
shapes = [{
'type': 'line',
'x0': 3,
'y0': -1,
'x1': 3,
'y1': 6,
},]
),
},
),
]),
])
if __name__ == '__main__':
app.run_server(debug = True)
My graph is in @app.callback
in the full app version.
JavaScript function which I am not sure is correct nor what to do with it, i.e. how to pass value to x0
and x1
in shapes
:
var sec;
setInterval(function(){showChart(); }, 100);
function showChart(){
ytplayer = document.getElementById("movie_player");
sec = ytplayer.currentTime;
return sec;
}
Can you suggest the best way to do it? Is it possible without JS or better way than using shapes
?
Thanks!