1

enter image description here

Basically all the sliders are populated using dcc.Input. The program works where input boxes populate the min/max of the slider. The x-axis of the graph that will displayed is based on the last slider that is changed

@app.callback(
    [Output('pieces', 'min'),
     Output('pieces', 'max'),
     Output('ups', 'min'),
     Output('ups', 'max'),
     Output('labor', 'min'),
     Output('labor', 'max'),
     Output('production', 'min'),
     Output('production', 'max'),
     Output('setup', 'min'),
     Output('setup', 'max'),
     Output('delivery', 'min'),
     Output('delivery', 'max'),
     Output('foil_markup', 'min'),
     Output('foil_markup', 'max'),
     Output('labor_markup', 'min'),
     Output('labor_markup', 'max'),
     Output('overall_markup', 'min'),
     Output('overall_markup', 'max')],
    [Input('submit-val', 'n_clicks'),
     State('pieces_low', 'value'),
     State('pieces_high', 'value'),
     State('ups_low', 'value'),
     State('ups_high', 'value'),
     State('labor_low', 'value'),
     State('labor_high', 'value'),
     State('production_low', 'value'),
     State('production_high', 'value'),
     State('setup_low', 'value'),
     State('setup_high', 'value'),
     State('delivery_low', 'value'),
     State('delivery_high', 'value'),
     State('foil_markup_low', 'value'),
     State('foil_markup_high', 'value'),
     State('labor_markup_low', 'value'),
     State('labor_markup_high', 'value'),
     State('overall_markup_low', 'value'),
     State('overall_markup_high', 'value')])
def pieces_low_value(n_clicks, pieces_min, pieces_max, ups_min, ups_max, labor_min, labor_max, production_min, production_max,
                     setup_min, setup_max, delivery_min, delivery_max, foil_markup_min, foil_markup_max,
                     labor_markup_min, labor_markup_max, overall_markup_min, overall_markup_max):

    return pieces_min, pieces_max, ups_min, ups_max, labor_min, labor_max, production_min, production_max, \
           setup_min, setup_max, delivery_min, delivery_max, foil_markup_min, foil_markup_max, labor_markup_min, \
           labor_markup_max, overall_markup_min, overall_markup_max 

Is there a way to obtain the min and max of a dcc.Slider object without including both of them as an Input to the callback function? Or if it is not possible is there a way to only add the 'min' and 'max' of the slider of interest?

It seems lengthy to actually include an Input('component_id', 'max') and Input('component_id', 'min') for all the 14 Inputs with the slider values since I will need the 'min' and 'max' of a slider that was last changed which is found by doing ctx = dash.callback_context and last_changed = ctx.triggered[0]['prop_id'].split(".")[0]

@app.callback(
    Output('graph', 'figure')
    [Input('foil_price', 'value'),
     Input('UOM_foil_length', 'value'),
     Input('UOM_foil_width', 'value'),
     Input('piece_length', 'value'),
     Input('piece_width', 'value'),
     Input('pieces', 'value'),
     Input('ups', 'value'),
     Input('labor', 'value'),
     Input('production', 'value'),
     Input('setup', 'value'),
     Input('delivery', 'value'),
     Input('foil_markup', 'value'),
     Input('labor_markup', 'value'),
     Input('overall_markup', 'value')])
def update_figure(price_per_roll, foil_length, foil_width,
                  piece_length, piece_width, pieces, ups,
                  labor_rate, production_rate, setup,
                  delivery, foil_markup, labor_markup, overall_markup):
    ctx = dash.callback_context
    last_changed = ctx.triggered[0]['prop_id'].split(".")[0]
Pherdindy
  • 1,168
  • 7
  • 23
  • 52

1 Answers1

0

What are those min and max values based on? You could listen to the data. I know you're more concerned about verbosity, but you could also use State, so those values don't trigger the callback.

coralvanda
  • 6,431
  • 2
  • 15
  • 25
  • Added how the sliders are populated and the graph for more details how it works – Pherdindy Apr 02 '22 at 01:19
  • How about, instead of using `dcc.Input`s to feed into the sliders, you just use `dcc.Input`s directly for those values? That would save a lot of callback wiring. – coralvanda Apr 02 '22 at 01:50
  • Was initially planning to have a point move on the line graph as the slider is moved so the incremental effect of the sliders can be seen but it probably can be omitted. But just wondering if there is a simple way like maybe `ctx = dash.callback_context` to achieve what is needed – Pherdindy Apr 02 '22 at 02:29
  • If you want the user to be able to enter all of these values, then I think 9 inputs might be the fewest possible. The callback context will only let you see which input triggered the callback, so that won't help in this case with reducing the number of inputs here. – coralvanda Apr 02 '22 at 13:31
  • Can I see a sample of how it would be done? I was also thinking if it was possible to set the returned values from the functions made in the callback to a global variable this may be another option as well – Pherdindy Apr 04 '22 at 05:57
  • Which part? Setting the `dcc.Input` components as `Input`s to a callback? – coralvanda Apr 04 '22 at 23:21
  • 1
    Already got it. I found out about `dcc.Store` then I would use it to store a dictionary with the minimum/maximum values of each slider. Then used `dash.callback_context` to determine which slider was last changed and search via key – Pherdindy Apr 04 '22 at 23:27
  • Good work! Does that mean you've solved your problem? – coralvanda Apr 05 '22 at 02:08