51

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?

Ben
  • 2,314
  • 1
  • 19
  • 36

8 Answers8

77

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

Ben
  • 2,314
  • 1
  • 19
  • 36
  • 5
    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. – Nerxis Apr 22 '21 at 15:30
  • 1
    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. – jasoos Nov 29 '21 at 15:36
  • And on top of the working directory tip from @jasoos 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$` – AndyMc Dec 21 '21 at 03:34
  • Thanks @Schilli for bringing https://discuss.streamlit.io/t/run-streamlit-from-pycharm/21624/3 to my attention. – Ben Feb 07 '22 at 10:55
  • 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' ``` – Hanan Shteingart Mar 09 '22 at 11:53
  • 1
    For newer versions (I assume 1.12. and above), one should use `streamlit.web.cli` instead of `streamlit.cli`, according to [this issue](https://github.com/streamlit/streamlit/issues/5146#issuecomment-1223306586). – Ivan Panchenko Oct 05 '22 at 09:33
  • 2
    Note: You may have to just keep it as `streamlit` and not `streamlit.cli`. It works for me that way. – MANU Mar 07 '23 at 10:13
  • MANU is correct. `streamlit` works and is part of the public API, unlike `streamlit.cli` (see here: https://discuss.streamlit.io/t/run-streamlit-from-pycharm/21624/3). Summary: Select the module 'streamlit` – Rhubarb Aug 18 '23 at 14:45
  • 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. – Ben Aug 29 '23 at 13:25
41

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:

Selecting Debug config for python interpreter

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

SternK
  • 11,649
  • 22
  • 32
  • 46
aiwa
  • 411
  • 4
  • 5
  • 2
    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. – Ben Feb 07 '22 at 14:01
  • 1
    Updated the code to remove the .cli suffix based on Ben's feedback and my testing. Thanks @ben – aiwa Aug 28 '22 at 01:12
16

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.

Jamiu S.
  • 5,257
  • 5
  • 12
  • 34
chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66
  • 2
    The following line worked for me bootstrap.run(real_script, f'run.py {real_script}', [] , {}) – Rudiger Wolf May 06 '21 at 07:40
  • 1
    Awesome! I think thats the easiest solution honestly. And thanks also to @RudigerWolf for the fix, you should consider editing the original answer. – Laurin Herbsthofer May 31 '21 at 10:13
  • Note that this [is not officially supported](https://github.com/streamlit/streamlit/issues/3739#issuecomment-915629235) and some Streamlit features might not work as expected. – Jan M. Dec 18 '21 at 12:33
  • In new version you can use from streamlit.web import bootstrap Discussion: https://discuss.streamlit.io/t/1-12-1-replacement-technique-for-my-debug-method/29599 – EmreAydin Mar 29 '23 at 13:54
9

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

Khawar Islam
  • 2,556
  • 2
  • 34
  • 56
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}"
        ],
    }
 ]
}
Jamiu S.
  • 5,257
  • 5
  • 12
  • 34
vinayak hegde
  • 2,117
  • 26
  • 26
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

Sismo
  • 33
  • 4
1

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

Suat Atan PhD
  • 1,152
  • 13
  • 27
0

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
ScrapeHeap
  • 186
  • 2
  • 11