Launch streamlit in a non-blocking manner halfway through a larger python codebase

I am trying to do an app that halfway through it launches streamlit and the app continues executing the remaining part of the code. I am trying to do it by spawning a new process but while the streamlit dashboard opens up fine, the program doesnt reach the rest of the code. For example:

import subprocess
  
print('Start')
subprocess.run(["streamlit", "run", filename])
print('End') # <= This never executes

How can I do this please? The streamlit dashboard needs to remain live (connected) while the rest of the code is executed. I am using python 3.8 and streamlit 1.11.1 on Windows 10 (I can upgrade my streamlit if necessary)

According to the docs

The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle. For more advanced use cases, the underlying Popen interface can be used directly.

It also says that run()

Run the command described by args . Wait for command to complete, then return a CompletedProcessinstance.

So it is clear that subprocess.run cannot handle your case. In fact Popen seems to work for me using streamlit 1.13.0 and python 3.10 (for some reason the streanlit.executable was not found but I can launch it using python -m streamlit)

import subprocess

print('Start')
subprocess.Popen(["python", "-m" "streamlit", "run", filename])
print('End')

Thanks but that still doesnt seem to work. Are you sure you got a dashboard launched. Dont you have to call communicate()? For example the following will open the dashboard but it still blocks execution:

import subprocess

print('Start')
process = subprocess.Popen(["python", "-m" "streamlit", "run", filename])
process.communicate() # If you miss this no dashboard is launched. If you keep it it blocks program flow
print('End') # <= This never executes if you call communicate() above

Works for me, again with python 3.10.7 and streamlit 1.13.0, either Linux or Windows. Detailed steps for Windows 10 follow.

Create these two files:

test.py (contains the streamlit application)

import streamlit as st

st.title("Test application")

launcher.py (launches the streamlit application in a subprocess)

import subprocess

filename = "test.py"

print('Start')
subprocess.Popen(["python", "-m" "streamlit", "run", filename])
print('End')

Open a Windows command prompt, cd into the folder containing the files and run python launcher.py

C:\path\to\code>python launcher.py
Start
End

C:\path\to\code>2022-10-06 08:45:12.729 INFO    numexpr.utils: NumExpr defaulting to 4 threads.

  You can now view your Streamlit app in your browser.

  Local URL: http://localhost:8501
  Network URL: http://xxx.xxx.xxx.xxx:8501
1 Like

Oh yes, you are right. I does work when i call it as you say, ie from the command prompt with python launcher.py. Strangely the dashboard doesnt open when I run the same script from within PyCharm (using the same python env.). From VSCode it also works fine. Dont know if I am missing something really basic or PyCharm does something behind the scenes and prevents streamlit from launching when subprocess.Popen() is called.

I am on streamlit 1.13, python 3.8, Windows 10. PyCharm 2022.2 community edition

Thanks so much in anycase, you cannot imagine how long I have been hitting the wall with that.

It might have to do with the way your IDE runs the script. For example, running python C:\path\to\code\launcher.py from a different folder might not work without further adjustments. Just an example that crossed my mind