You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CONTRIBUTING.md
+43Lines changed: 43 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,6 +54,49 @@ poetry run sphinx-build docs docs/_build --builder html --fail-on-warning
54
54
start docs\_build\index.html
55
55
```
56
56
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
+
exceptRuntimeErroras e:
73
+
if"debugpy.listen() has already been called on this process"notinstr(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.
0 commit comments