Chart and Dataframes don't display unless another widget is updated

I can’t figure this out for the life of me. In my main() function I am initializing two placeholders – plot_placeholder and df_placeholder.

These are there to structure the UI layout before the data is available to fill the space and to automatically hide the chart/dataframe if the underlying data changed and it needs to be refetched.

But the plotly chart and dataframe expanders will not display until you change the value on one of the slider slider widgets in the sidebar.

You can test it out by:

  1. enter SPY into ticker symbol input and press enter. This loads the expiration dropdowns. They dont need to be changed but can be.
  2. Select ‘Net Delta’ from Chart Type dropdown.
  3. Click ‘Get Data’ button

The data will fetch after a few seconds and you might see a chart flash on the screen for a second and then disappear. Change any of the slider values and everything will display.

I think its related to my usage of plot_placeholder and df_placeholder, but I’m not sure how.

I just added a state variable plot_visible to the session_state and I updated my main() function to not display the plot_placeholder if [‘plot_visible’] was True.

Now the neither plot_placeholder or the chart is displaying until a widget is updated.

I don’t get it. Thanks for any help!

I just realized something else.

Until recently my plot and dataframes were rendering properly. My current issue started when I removed the logic to plot the data from the main() function to outside of it.

I wonder if handling the placeholder logic in main() but not the plotting logic is the problem?

Is using main() good practice in streamlit? Maybe I need to try to handle the placeholders outside of main() also? The reason I have it set up this way is so the custom styled placeholders are visible on page load before the data loads in order to maintain a consistent UI layout whether data is present or not.

Thanks.

Pulling the data fetching code back into the main() function solved the issue. I’m still not entirely sure why but it worked.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.