How to do some prepare when app start

I have an app that needs some preparation/clean work, like dataset build and app status report.

streamlit can only run app with β€˜run’ command, how can I execute functions when the app starts or stop?