In this post, I’ll show you how to run a Streamlit application from a Python script. This is useful if you want to run some logic before your app starts or if you want to package it as an standalone executable.
Note that this technique uses streamlit
’s internal CLI module, hence, it might not
work with certain versions. I tested this code with the following version:
pip install streamlit==1.32.2
Suppose we have an app.py
script. The usual way of starting the app is:
streamlit run app.py
We’ll replicate this functionality with the following script:
# store this as run-streamlit.py
from streamlit.web import cli
if __name__ == "__main__":
cli.main_run(["app.py"])
To run it:
python run-streamlit.py
Note that you can pass arguments to the main_run
function:
from streamlit.web import cli
if __name__ == "__main__":
cli.main_run(["app.py", "--server.port", "1000"])
That’s it!
Found an error? Click here to let us know.