How to make "streamlit run example.py" run as a service or system task in windows?

If “streamlit run example.py” can run as a service or system task in windows,
web page can be more stable and we do not have to open a cmd window anymore.

You can save your start command in a batch (.bat) file and use the windows task scheduler to run this on startup or login. If you are working with anaconda/conda and have to activate an environment first, you may use something like this to start the server at a predefined tcpip port:

C:\develop\anaconda\Scripts\activate.bat C:\develop\anaconda\envs\std3 & streamlit hello --server.port 8505

Here the path to my conda install is C:\develop\anaconda and I am activating an env named std3. Notice the ‘&’ which chains the commands! Use full absolute paths. Starting the bat at boot time may involve playing with the access rights on files

2 Likes

In my case (Windows 10 pro, Python 3.7.9 if it matters), I had to use full paths and the “streamlit run” command, .bat file content as follow:

@ECHO OFF
C:\path_to_python_app_folder.venv\Scripts\activate.bat C:\path_to_python_app_folder.venv\ & streamlit run C:\path_to_python_app_folder\path_to_python_app_file.py --server.port 8505

3 Likes

I am trying with the above solution. It is working in terms of opening my .py streamlit application file but it is giving me an error of “FileNotFoundError: [Errno 2] No such file or directory:my_excel.xlsx” which I have used in my python to do data analysis. My files are in the same folder from which I am accessing my .py file. How can I fix this? I am completely new to web application and microsoft service things. Any help would be appreciated. Thank you in advance.

I managed to make mine work this way :
@ECHO OFF
set root=C:\Anaconda3
call %root%\Scripts\activate.bat
cd [PATH_TO_YOUR_STREAMLIT_APP_FOLDER]
pip install -r requirements.txt
call streamlit run app.py

Be sure to have Anaconda installed. Give that path to the root variable.
Activate that conda environment.
The path to your streamlit app is only to the folder, finished by a “” and does not include your .py script. This does not need to be between " " or between . Just copy-paste the path.
I added the pip install -r requirements to force the update of the python libraries needed to run my app. This is not mandatory, but just makes your life easier.
Finally, I simply call my app.

Your file can be located anywhere and the app runs fine.

On Windows, try the non sucking service manager.

1 Like

I have tried nssm which looks no error on service. but .bat file does not run anymore. bat file can run by manual. it seems like streamlit.exe does not work properly as a service. could you share how to do?

I haven’t tried myself, but you probably need to run it in headless mode when run as a service.

streamlit run your_script.py --server.headless true

3 Likes

You are right! Thank you for comments!
Start working as a service once I added headless mode.
Here is a code in my bat file which is worked by nssm.

streamlit run my_scrip.py --server.headless true --server.port 8501

2 Likes

set root=C:[PATH_TO_YOUR_ANACONDA_FILE]
call %root%\Scripts\activate.bat
call conda activate [PYTHON_ENVIRONMENT]
cd [PATH_TO_YOUR_STREAMLIT_APP_FOLDER]
call streamlit run app.py

i run this bat file by adding conda activation code. and it is cool, it is run smoothly, thank for the code

1 Like

May I know what’s the benefit of it? Is that a part of deploying the app from a windows machine? If so, what’s the next step(s)? Thanks.

this worked exactly as described. thank you!
note that you will need to follow instructions on setting up a virtual env