How to run/debug a streamlit application from an IDE

If you're a VS Code user, you can debug your Streamlit app by adding the following configuration to your launch.json file:

{
        "name": "Python:Streamlit",
        "type": "python",
        "request": "launch",
        "module": "streamlit",
        "args": [
            "run",
            "${file}",
            "--server.port",
            "SPECIFY_YOUR_OWN_PORT_NUMBER_HERE"            ]
    }

Specifying the port number allows you to launch the app on a fixed port number each time you run your debug script.

Once you've updated your launch.json file, you need to navigate to the Run tab on the left gutter of the VS code app and tell it which Python config it should use to debug the app:

Selecting Debug config for python interpreter

Thanks to git-steb for pointing me to the solution!


I've come up with an alternative solution which allows you to use PyCharm debugging in a natural way. Simply set up a run script (which I call run.py which looks like this:

from streamlit import bootstrap

real_script = 'main_script.py'

bootstrap.run(real_script, f'run.py {real_script}', [], {})

and set that up as a normal Python run configuration in PyCharm.


I found a way to at least run the code from the IDE (PyCharm in my case). The streamlit run code.py command can directly be called from your IDE. (The streamlit run code.py command actually calls python -m streamlit.cli run code.py, which was the former solution to run from the IDE.)

The -m streamlit run goes into the interpreter options field of the Run/Debug Configuration (this is supported by Streamlit, so has guarantees to not be broken in the future1), the code.py goes into the Script path field as expected. In past versions, it was also working to use -m streamlit.cli run in the interpreter options field of the Run/Debug Configuration, but this option might break in the future.

PyCharm Run configuration shown here

Unfortunately, debugging that way does not seem to work since the parameters appended by PyCharm are passed to streamlit instead of the pydev debugger.

Edit: Just found a way to debug your own scripts. Instead of debugging your script, you debug the streamlit.cli module which runs your script. To do so, you need to change from Script path: to Module name: in the top-most field (there is a slightly hidden dropdown box there...). Then you can insert streamlit.cli into the field. As the parameters, you now add run code.py into the Parameters: field of the Run/Debug Configuration. Run/Debug configuration shown here

EDIT: adding @sismo 's comment

If your script needs to be run with some args you can easily add them as

run main.py -- --option1 val1 --option2 val2

Note the first -- with blank: it is needed to stop streamlit argument parsing and pass to main.py argument parsing.


1https://discuss.streamlit.io/t/run-streamlit-from-pycharm/21624/3