Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Run a Streamlit App from within a Python Script?

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.

like image 272
David Avatar asked Jul 06 '20 17:07

David


People also ask

How do I run streamlit app in Python?

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.

How do I run streamlit locally?

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.

How do I run a script from a Python script?

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!

What are the ways to run a program using the Python interpreter?

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.


2 Answers

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())
like image 51
Jiri Dobes Avatar answered Sep 29 '22 06:09

Jiri Dobes


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.

like image 42
David Avatar answered Sep 29 '22 07:09

David