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

Multiple chained callbacks to modify data based on user input avoiding loading from server

$
0
0

@guidocioni wrote:

I’m building an app which displays a 3D scatter of some points queried from an API. The API is reaaaallly slow (average response time 15 seconds) so it is really important to avoid useless queries to the server if it’s not really necessary.

I need to pull the data based on a user input (dropdown) and then update the data, without running the query again, using a slider to translate all the points in x, y or z. The first part is easy to do: I have a function to query the dataset, query_dataset, which returns a dataframe. This uses the value of the dropdown through a callback

@app.callback(
    Output('3d-plot', 'figure'),
    [Input('msn-dropdown', 'value')])
def update_figure(msn):
    df = query_dataset(msn=msn)

    fig = go.Figure(data=[create_trace(df, 'Port', name='Port'),
                    create_trace(df, 'Stbd', name='Stbd')], layout=figure_layout())
    return fig

This works pretty well although the user has to wait about 20 seconds as I have to parse the list of all the options for the dropdown and the data itself, and this needs to be done in 2 distinct queries.

Now that the data is loaded I want to be able to move all the points doing a simple translation in x, y or z based on 3 sliders input. I can define a function to do that

def translate_df(df, dx=0):
   # dx here would be the value of a slider
   df.loc[:,"X"]+= dx
   return df

Then I would insert this into update_figure after querying the dataset. Note that I cannot do this transformation inside update_figure (this is what I’ve found as answer in many questions on this forum) as I cannot run the query again and again whenever I do a transformation.

I thought about different strategies but I couldn’t really figure it out how to do this. I was thinking of defining a callback for translate_df which would respond to the input of the sliders but then I don’t know how to pass df.

Does anyone have any idea?

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 6271

Trending Articles