How to run/debug a streamlit application from an IDE
Asked Answered
A

11

75

I really like streamlit as an environment for research. Mixing a notebook/dashboard-like output I can design quickly with pure code for its definition (no cells etc.) as well as the ability to influence my code through widgets while it runs is a game changer.

For this purpose, I was looking for a way to run or even debug a streamlit application, since the tutorials only show it being started via the commandline:

streamlit run code.py

Is there a way to do either running or debugging from an IDE?

Archdeacon answered 11/2, 2020 at 15:26 Comment(0)
A
101

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.


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

Archdeacon answered 11/2, 2020 at 15:26 Comment(10)
Thanks, this is very useful and working! Just reminder for others to not forget to setup up correct working directory as I didn't do it for the first time and it obviously could not work.Sutter
A suggestion. We can put the directory where the script lies in the 'Working Directory' field and then we do not need to provide the full path of the script in 'Parameters' field for the module. This actually helped me in debugging a streamlit project with dependent python scripts in the same directory.Henleyonthames
And on top of the working directory tip from @Henleyonthames using a macro for the filename in the Module Parameters means you can use the same configuration for all your streamlit python files in the same working directory. eg. Parameters: run $FileName$Prank
Thanks @Schilli for bringing discuss.streamlit.io/t/run-streamlit-from-pycharm/21624/3 to my attention.Archdeacon
I get an error when I put a breakpoint using this method. ``` pydevd_frame_eval_cython_wrapper = sys.modules['_pydevd_frame_eval.pydevd_frame_eval_cython_wrapper'] KeyError: '_pydevd_frame_eval.pydevd_frame_eval_cython_wrapper' ```Muna
For newer versions (I assume 1.12. and above), one should use streamlit.web.cli instead of streamlit.cli, according to this issue.Amadoamador
Note: You may have to just keep it as streamlit and not streamlit.cli. It works for me that way.Vacuva
MANU is correct. streamlit works and is part of the public API, unlike streamlit.cli (see here: discuss.streamlit.io/t/run-streamlit-from-pycharm/21624/3). Summary: Select the module 'streamlit`Darg
Ah nice, so streamlit works now as well. At the time (some time ago), it did not work with the top-level, so I went for streamlit.cli. Thanks for the heads-up! Edit: Ah wait, I edited this already in the main answer. There is even a footnote with a streamlit issue which refers to -m streamlit as being supported for longterm.Archdeacon
Does anyone knows how to add the python application args in the vs code args array? Adding the "--" as an arg before adding other app specific args but they are not recognized by vs code. ThanksMarquita
T
70

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": "debugpy",
    "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:

Selecting Debug config for python interpreter

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

Trillbee answered 20/11, 2020 at 2:20 Comment(4)
Looks like you possibly need to switch to "module": "streamlit" as well since they mention this to be officially supported now. But someone should test this first before the edit is made.Archdeacon
Updated the code to remove the .cli suffix based on Ben's feedback and my testing. Thanks @benTrillbee
this worked for me with changes: since .vscode/launch.json is a new file in my project, wrapping your JSON in { "configurations": [ ... ] } worked for me. I think you imply this in your answer, but i find it easier to replace my entire file contents instead of using the "Add Configuration" control first.Appellation
A warning that "This configuration will be deprecated soon. Please replace `python` with `debugpy` to use the new Python Debugger extension.(2)"Redmon
M
21

Aug, 12, 2022: Please update your pip and streamlit versions. Sometime, it is mandatory to update all both version.

pip install pip --upgrade
pip install --upgrade streamlit

Open Pycharm Editor and go to the Edit Configuration file as mentioned below in picture. Do not clear streamlit in my dropdown box. Click on dropdown box.

enter image description here

Run/Debug Configurations:

You have to change three directories remember that script path.

1) You can obtain script path by typing which streamlit in terminal and paste the path in script path.

2) click on working directory and give directory of your python file which contain streamlit.

3) in Paramaters: give python file name like app.py with run.

enter image description here

Marin answered 12/8, 2022 at 1:11 Comment(3)
Is this working on windows?Envisage
Yes sure this is pycharm featureMarin
Also works in IntelliJ IDEA (Community Edition). Thanks!Unguentum
R
20

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:

try:
    # for streamlit >= 1.12.1
    from streamlit.web import bootstrap
except ImportError:
    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.

Edit: incorporated different import location, thanks to comment by @EmreAydin

Rob answered 20/8, 2020 at 7:31 Comment(4)
The following line worked for me bootstrap.run(real_script, f'run.py {real_script}', [] , {})Alex
Awesome! I think thats the easiest solution honestly. And thanks also to @RudigerWolf for the fix, you should consider editing the original answer.Payday
Note that this is not officially supported and some Streamlit features might not work as expected.Barahona
In new version you can use from streamlit.web import bootstrap Discussion: discuss.streamlit.io/t/…Possessive
H
9

Since the UI has changed slightly, here is what I ended up doing after trying Ben's suggestions.

One thing to note is that you will might see errors if you modify the code while using the debugger. Restarting the debugger usually resolves the issue.


In the Run/Debug Configuration window, add a new Python configuration with the following settings:

  • Confirm that the "Python interpreter" is set to the virtual environment you created and your "Working directory" is correct
  • Select module from the "Run script or module" dropdown
  • Enter streamlit in the "Module name" field
  • Using the "Modify options" dropdown, select Add option > Parameters
  • Enter run app.py in the "Parameters" field
Helium answered 16/8, 2023 at 14:45 Comment(1)
This worked for me. Thx.Ftlb
F
4

With some modification to @aiwa answer - This worked for me in the VS code version - 1.58

{
"configurations": [
    {
        "name": "Python:Streamlit",
        "type": "python",
        "request": "launch",
        "module": "streamlit.cli",
        "args": [
            "run",
            "${file}"
        ],
    }
 ]
}
Frigg answered 23/7, 2021 at 13:56 Comment(0)
S
3

Cannot comment so I have to put this as an answer.

An addition to @Ben's answer (module debugging part): 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

Seldom answered 30/9, 2021 at 13:22 Comment(1)
I updated my original answer with your addition, thanks!Archdeacon
S
3

Alongside other solutions, another easy and quick solution is using pdb library. For instance;

st.dataframe(df)
import pdb; pdb.set_trace()
st.bar_chart(df)

When you run code, your IDE (or even command line) will stop at the 'set trace' point and the command line show you something like that:

(Pdb)>

In that case, you can call your variables and process them on the command line. For instance:

enter image description here

For other options of PDB library please see: https://docs.python.org/3/library/pdb.html

Sympathy answered 20/11, 2022 at 8:40 Comment(0)
C
3

UPD. January 24, 2024

create separated file like debug.py

from streamlit.web.bootstrap import run

real_script = 'demo.py'
run(real_script, False, [], {})

And add this file to PyCharm configuration.

Corliss answered 24/1, 2024 at 10:31 Comment(2)
Works with VS Code/Cursor as wellVibrate
"Thank you very much! Your help truly brightened my day!"Gautama
X
2

The accepted answer did not work for me, I got streamlit.cli not found.

This worked for me:

enter image description here

Xantho answered 15/10, 2023 at 14:25 Comment(0)
V
2

"configurations": [

    {
        "name": "Python: Current File",
        "type": "python",
        "request": "launch",
        "args": [
            "run",
            "${file}",
        ],
        // "program": "${file}",
        "module":"streamlit",
        "console": "integratedTerminal",
        "justMyCode": true
    }
]
Vienna answered 28/10, 2023 at 11:56 Comment(1)
Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Lymphocyte

© 2022 - 2025 — McMap. All rights reserved.