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
+44Lines changed: 44 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,6 +54,50 @@ 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 can be done using standard Python debugging techniques. However, debugging the Streamlit script—or any code invoked by the Streamlit script—is more complex because it runs in a separate process launched by the PythonPanelServer. To debug the Streamlit script, you can use debugpy to attach the Visual Studio Code debugger as follows:
60
+
61
+
## Instrument Streamlit script to debug
62
+
63
+
To enable debugpy debugging, include this code in your streamlit script:
64
+
65
+
```python
66
+
import debugpy # type:ignore
67
+
68
+
try:
69
+
debugpy.listen(("localhost", 5678))
70
+
debugpy.wait_for_client()
71
+
exceptRuntimeErroras e:
72
+
if"debugpy.listen() has already been called on this process"notinstr(e):
73
+
raise
74
+
```
75
+
76
+
The `debugpy.listen()` function opens a port that allows the debugger to attach to the running process. You can specify any available port, as long as it matches the port configured in the launch.json file shown below. Since calling listen() more than once will raise an exception, it is wrapped in a try block to prevent the script from crashing if it is rerun.
77
+
78
+
The `debugpy.wait_for_client()` function pauses script execution until the debugger is attached. This is helpful if you need to debug initialization code, but you can omit this line if it is not required.
79
+
80
+
The `import debugpy` statement includes a type suppression comment to satisfy mypy.
81
+
82
+
## Add debugpy configuration in launch.json
83
+
84
+
You will also need this configuration in your launch.json:
85
+
86
+
```json
87
+
{
88
+
"name": "Attach to Streamlit at localhost:5678",
89
+
"type": "debugpy",
90
+
"request": "attach",
91
+
"connect": {
92
+
"host": "localhost",
93
+
"port": 5678
94
+
},
95
+
"justMyCode": false
96
+
}
97
+
```
98
+
99
+
After running your measurement script and allowing the PythonPanelServer to launch Streamlit with your Streamlit script, you can attach the debugger by clicking the **Attach to Streamlit at localhost:5678** button in the VS Code **Run and Debug** tab. Once attached, you can set breakpoints and use all standard debugging features in your Streamlit script, as well as in any nipanel code invoked by the Streamlit script.
0 commit comments