Hello Folks,
What is the best means to invoke streamlit from within python code ?
I dont want to go via the CLI route of streamlit run <script-name.py>
I don’t quite understand what you’re asking, but here’s an example of how I got it working:
Folder structure:
.
├── exampleapp
│ ├── __init__.py
│ ├── myscript.py
│ └── wrapped_cli_tool.py
└── setup.py
setup.py
import platform
import setuptools
import subprocess
setuptools.setup(
name='exampleapp',
version='1.0.0',
description='',
long_description='',
url='',
author='',
author_email='',
license='',
packages=['exampleapp'],
install_requires=['strea…
This link has some reference, but again to invoke as mentioned in link, python click context is needed.
Thankyou
Jacob
Looks like this is a way:
import os
import streamlit.bootstrap
from streamlit import config as _config
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'app.py')
_config.set_option("server.headless", True)
args = []
#streamlit.cli.main_run(filename, args)
streamlit.bootstrap.run(filename,'',args)
3 Likes
Thanks for the helpful tips! That’s what I was trying to find.
By the way, when I ran your code, I saw the following error.
TypeError: run() missing 1 required positional argument: 'flag_options'
Instead, I had to write as following. This may be due to a different version of Streamlit. (I’m using Streamlit version 0.86.0.)
streamlit.bootstrap.run(filename, '', args, flag_options={})
In anyway, thanks again for the tips!
Regards,
Atsushi
Hi, i figured when i start streamlit with bootstrap, it will break forms. As long as I run the code from command line, everything works. When I invoke streamlit from within python, forms are broken.
I opened an issue here.
opened 09:40AM - 30 Aug 21 UTC
bug
needs triage
### Summary
st.form is not working when streamlit is started via code
### … Steps to reproduce
testform.py
```python
import streamlit.bootstrap
streamlit.bootstrap.run("app.py", '', [], flag_options={})
```
app.py
(Sample code form docs)
```python
import streamlit as st
with st.form("my_form"):
st.write("Inside the form")
slider_val = st.slider("Form slider")
checkbox_val = st.checkbox("Form checkbox")
# Every form must have a submit button.
submitted = st.form_submit_button("Submit")
if submitted:
st.write("slider", slider_val, "checkbox", checkbox_val)
st.write("Outside the form")
```
**Expected behavior:**
When run via CLI `$ streamlit run app.py` everything works as expected.

**Actual behavior:**
When run via bootstrap script `$ python testform.py ` the form is broken.

### Is this a regression?
Unknown
### Debug info
- Streamlit version: 0.87.0
- Python version: 3.7.10
- Conda
- OS version: Win10
- Browser version: Chrome / Edge
Feel free to add if you have any thoughts about it.
For those who are interested in this feature,
streamlit.bootstrap has been moved to streamlit.web.boostrap.
Elijas
December 9, 2022, 2:35pm
6
You can also do
import sys
from streamlit.web import cli as stcli
if __name__ == '__main__':
sys.argv = ["streamlit", "run", "APP_NAME.py"]
sys.exit(stcli.main())
Source:
python, streamlit
iuiu
January 12, 2024, 9:04am
7
Official supported & recommended way is via cli.
streamlit run your_app.py
But still, if you prefer to render the app from within python, in st 1.30 is
from streamlit import config as _config
from streamlit.web.bootstrap import run
_config.set_option("server.headless", True)
run('your_app.py', args=[], flag_options=[], is_hello=False)
But this could change from one release to another (as it’s not officially supported)