Skip to content

Commit 6d7aff1

Browse files
Mike ProsserMike Prosser
authored andcommitted
document the debugging process in CONTRIBUTING.md
1 parent 47ca7e3 commit 6d7aff1

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

CONTRIBUTING.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,49 @@ poetry run sphinx-build docs docs/_build --builder html --fail-on-warning
5454
start docs\_build\index.html
5555
```
5656

57+
# Debugging on the streamlit side
58+
59+
Debugging the measurement script is just normal python debugging, but debugging the streamlit script, or code that is called from the streamlit script, is more challenging because it runs in a separate process that is launched by the PythonPanelServer. Here's how to use debugpy to attach the Visual Studio Code debugger to the streamlit script:
60+
61+
## in the streamlit script, include this code snippet
62+
63+
```python
64+
import debugpy # type: ignore
65+
import streamlit as st
66+
67+
import nipanel
68+
69+
try:
70+
debugpy.listen(("localhost", 5678))
71+
debugpy.wait_for_client()
72+
except RuntimeError as e:
73+
if "debugpy.listen() has already been called on this process" not in str(e):
74+
raise
75+
```
76+
77+
`debugpy.listen()` exposes a port for the debugger to attach to. The port can be whatever, it just needs to match the port in launch.json below. Note that calling listen() more then once will throw an exception, so we put it in a try block so that when the script is rerun, it won't crash.
78+
79+
`debug.wait_for_client()` will pause execution of the script until the debugger attaches. This is useful if you need to debug into initialization code, but you can omit this line otherwise.
80+
81+
The `import debugpy` line has a type suppression to make mypy happy.
82+
83+
## in launch.json, include this configuration
84+
85+
```json
86+
{
87+
"name": "Attach to Streamlit at localhost:5678",
88+
"type": "debugpy",
89+
"request": "attach",
90+
"connect": {
91+
"host": "localhost",
92+
"port": 5678
93+
},
94+
"justMyCode": false
95+
}
96+
```
97+
98+
Once you have run your measurement script, and PythonPanelSever has run streamlit with your streamlit script, you can then click the "Attach to Streamlit at localost:5678" button in the VS Code "Run and Debug" tab. Then, you should be able to set breakpoints (and do all the other debugging stuff) in your streamlit script, and in any nipanel code that the streamlit script calls.
99+
57100
# Developer Certificate of Origin (DCO)
58101

59102
Developer's Certificate of Origin 1.1

0 commit comments

Comments
 (0)