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)
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)
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
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
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
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.