Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run/debug a streamlit application from an IDE

Tags:

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?

like image 878
Ben Avatar asked Feb 11 '20 15:02

Ben


People also ask

How do I run streamlit code in Pycharm?

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.

How do I run streamlit code in Visual Studio?

streamlit should exist in the {python environment path}\Scripts folder, you can through get-command streamlit in the powershell to get it. You can try to restart VSCode or take Ctrl+Shift+` shortcut to create a new terminal that activates the corresponding python interpreter.


2 Answers

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

like image 115
Ben Avatar answered Sep 22 '22 13:09

Ben


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.cli",         "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!

like image 43
aiwa Avatar answered Sep 22 '22 13:09

aiwa