How to call a function or def which returns plotly fig object and update it in another plotly with a button click

Viewed 453

I have two definitions(def) in 'a' file, like ploty_bar and plotly_heatmap, where both returns fig objects of plotly, after plotting.

Now I have 'b' file in the project and I have imported the 'a' file here. I wanted to call those 'a' file definitions in 'b' file with a button click and show the fig objects.

Following is the code for one definition in 'a' file:

def plotly_scatter():
  
    df = read_csv('iris.csv')
    fig = px.scatter(df, x="sepal_width", y="sepal_length", title = "Plotly Scatterplot Chart")
    return fig

Following is the code for one definition in 'b' file: I tried like below but it updated in the same figure, I need two different functions when clicked on two different buttons, how to do that?

fig = go.Figure()

fig.add_trace(go.Scatter(x=x, y=y, mode='markers'))

fig.update_layout(
    updatemenus=[
        dict(type="buttons",
             direction="left",
             buttons=[
                 dict(
                     label="scatter", 
                     method="update",
                     args=[{"mode": "markers"}],  
                 ), dict(
                     label="line",
                     method="update",
                     args=[{"mode": "line"}],  
                 )
             ],
             pad={"r": 10, "t": 10}, showactive=True, x=0.11, xanchor="left", y=1.1, yanchor="top"
             # Layout-related values
             ),
    ]
)

I want a 'b' file where I have 2 buttons, when I click on first button I should be able to call the first function like below and show that paticular fig fig = plotly_scatter() fig.show() For the other button when pressed I should call second function and show the results like below: fig = plotly_bar() fig.show()

When I write these two lines normally it gives the answer, but how to call them and show them when clicked on plotly buttons.

0 Answers
Related