Is there a way to run the command streamlit run APP_NAME.py
from within a python script, that might look something like:
import streamlit
streamlit.run("APP_NAME.py")
As the project I'm working on needs to be cross-platform (and packaged), I can't safely rely on a call to os.system(...)
or subprocess
.
Running a Streamlit app is similar to running any Python script. All you need to do is use the "streamlit run appname.py" command in command line. While working on a notebook, like this one, you can download your file as a Python (. py) file and run the same using the "streamlit run" command.
To launch the app locally, in the terminal, you can run the following command: streamlit run streamlit_sharing.py . Please make sure that you need to navigate to the directory where the Python script is saved. Otherwise, you'll have to specify the full path to the file.
To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!
There are two ways to use the python interpreter: interactive mode and script mode. (a) You can also save your commands in a text file, which by convention have the suffix “. py”, for example, program.py. (b) Create your list of commands inside any text editor, for example gedit.
You can run your script from python as python my_script.py
:
import sys
from streamlit import cli as stcli
import streamlit
def main():
# Your streamlit code
if __name__ == '__main__':
if streamlit._is_running_with_streamlit:
main()
else:
sys.argv = ["streamlit", "run", sys.argv[0]]
sys.exit(stcli.main())
Hopefully this works for others: I looked into the actual streamlit file in my python/conda bin, and it had these lines:
import re
import sys
from streamlit.cli import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())
From here, you can see that running streamlit run APP_NAME.py
on the command line is the same (in python) as:
import sys
from streamlit import cli as stcli
if __name__ == '__main__':
sys.argv = ["streamlit", "run", "APP_NAME.py"]
sys.exit(stcli.main())
So I put that in another script, and then run that script to run the original app from python, and it seemed to work. I'm not sure how cross-platform this answer is though, as it still relies somewhat on command line args.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With