Quantcast
Channel: 🎛️ Dash - Plotly Community Forum
Viewing all articles
Browse latest Browse all 6271

Use callback calculated value to update slider range

$
0
0

I have a Slider that ranges from IETsr to 0, IETsr is a value calculated from the default setting in two dropdown menus on start-up.
What I’d like to happen is when the values for the DropDown are changed by the user and a new IETsr is calculated, the Slider range changes to us the new value.
All the other parts of this code work correctly and IETsr is being calculated every time I change the DropDown but I don’t know how to feed it back into the Slider.
Ignore the # items at the top, this was for when they were hard coded in.

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objects as go


Dep=363                 #Water depth (m)
#Speed = 2.2            #Ship's speed (m/s)
ASV=1.5                 #Speed of sound in water (m/s)
#SPI=6.25                #Distance between sample stations (m)
SB=0-((Dep*2)/ASV)      #Sound travel time to seabed (milliseconds) - negative to denote below time zero
#IET=(SPI/Speed)         #Inter Event Time (s) time to travel SPI
#IETs=IET*1000           #IET in milliseconds
#IETsr=0-round(IETs)           


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
    html.H6("Calculate available record lengths"),    
    html.Label('Shot Point Interval'),
    dcc.Dropdown(id='SPIDD',
        options=[
            {'label': '3.125', 'value': 3.125},
            {'label': u'6.25', 'value': 6.25},
            {'label': '12.5', 'value': 12.5},
            {'label': '25.0', 'value': 25.0},
        ],
        value='6.25'
    ),
    html.Label('Vessel Speed m/s / Knots'),
    dcc.Dropdown(id='SpeedDD',
        options=[
            {'label': '1.95/3.8', 'value': 1.955},
            {'label': '2.05/4.0', 'value': 2.058},
            {'label': '2.16/4.2', 'value': 2.161},
            {'label': '2.26/4.4', 'value': 2.264},            
            {'label': '2.37/4.6', 'value': 2.366},
            {'label': '2.47/4.8', 'value': 2.469},            
        ],
        value='2.16/4.2'
    ),
    
    dcc.Graph(id='graph-with-slider'),
    dcc.Slider(
        id='3DHR Slider',
        min=IETsr,
        max=0,
        value=(IETsr-0)/2,
        marks={i: f"{i}" for i in range(round(IETsr,-2),0,100)},        
        step=50
    ),
    html.Div(id='display1'),
    html.Div(id='display2'),
    html.Div(id='display3')     
])


@app.callback(
    Output('graph-with-slider', 'figure'),
    Output('display1', 'children'),
    Output('display2', 'children'), 
    Output('display3', 'children'),
    Output('3DHR Slider', 'value'),    #This is where I thought it might do the feedback   
    Input('3DHR Slider', 'value'),     
    Input('SPIDD','value'),
    Input('SpeedDD','value'))


def update_figure(REC1,SPIDD,SpeedDD):
    SPIDD=float(SPIDD)
    SpeedDD=float(SpeedDD)
    global IET
    global IETs
    global IETsr
    IET=SPIDD/SpeedDD
    IETs=IET*1000
    IETsr=0-round(IETs)
    return IET,IETs,IETsr
    
    REC2= round((0-IETs)-REC1)  #UHRS record length - Function of above

    fig = go.Figure()

    #Add seabeds    
    fig.add_trace(go.Scatter(x=[0,1,None,1.1,1.6],y=[SB,SB,None,SB,SB],name="Seabed",line=dict(color='green',dash='dash'))) 
    fig.add_trace(go.Scatter(x=[1.1,1.6],y=[REC1+SB,REC1+SB],name="Seabed-UHRS",line=dict(color='blue',dash='dash'))) ##Stacked Rec2    
 

    #Add record length windows
    fig.add_trace(go.Scatter(
        x=[0, 0.5,0.5, 0,0,None,1.1, 1.6,1.6, 1.1,1.1],
        y=[0, 0, REC1, REC1,0,None,0, 0, REC1, REC1,0],
        fill='toself', 
        mode='lines', line_color='#bd3786',name="3DHR " +str(REC1)))    
 
    fig.add_trace(go.Scatter(
        x=[0.5, 1,1, 0.5,0.5,None,1.1, 1.6,1.6, 1.1,1.1],
        y=[0, 0, REC2, REC2,0,None,REC1,REC1,REC1+REC2, REC1+REC2,REC1],
        fill='toself', 
        mode='lines', line_color='#0d0887',name="UHRS " +str(REC2)))   
       
    
    fig.update_xaxes(showticklabels=False,showgrid=False)
    fig.update_layout(template='none')
    fig.update_yaxes(range=[IETsr,0])
    return fig


if __name__ == '__main__':
    app.run_server(debug=True)

6 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 6271

Trending Articles