@@ -54,6 +54,65 @@ poetry run sphinx-build docs docs/_build --builder html --fail-on-warning
5454start docs\_build\index.html
5555```
5656
57+ # Debugging on the streamlit side
58+
59+ Debugging the measurement script can be done using standard Python debugging
60+ techniques. However, debugging the Streamlit script—or any code invoked by the
61+ Streamlit script—is more complex because it runs in a separate process launched
62+ by the PythonPanelServer. To debug the Streamlit script, you can use debugpy to
63+ attach the Visual Studio Code debugger as follows:
64+
65+ ## Instrument Streamlit script to debug
66+
67+ To enable debugpy debugging, include this code in your streamlit script:
68+
69+ ``` python
70+ import debugpy # type: ignore
71+
72+ try :
73+ debugpy.listen((" localhost" , 5678 ))
74+ debugpy.wait_for_client()
75+ except RuntimeError as e:
76+ if " debugpy.listen() has already been called on this process" not in str (e):
77+ raise
78+ ```
79+
80+ The ` debugpy.listen() ` function opens a port that allows the debugger to attach
81+ to the running process. You can specify any available port, as long as it
82+ matches the port configured in the launch.json file shown below. Since calling
83+ listen() more than once will raise an exception, it is wrapped in a try block to
84+ prevent the script from crashing if it is rerun.
85+
86+ The ` debugpy.wait_for_client() ` function pauses script execution until the
87+ debugger is attached. This is helpful if you need to debug initialization code,
88+ but you can omit this line if it is not required.
89+
90+ The ` import debugpy ` statement includes a type suppression comment to satisfy mypy.
91+
92+ ## Add debugpy configuration in launch.json
93+
94+ You will also need this configuration in your launch.json:
95+
96+ ``` json
97+ {
98+ "name" : " Attach to Streamlit at localhost:5678" ,
99+ "type" : " debugpy" ,
100+ "request" : " attach" ,
101+ "connect" : {
102+ "host" : " localhost" ,
103+ "port" : 5678
104+ },
105+ "justMyCode" : false
106+ }
107+ ```
108+
109+ After running your measurement script and allowing the PythonPanelServer to
110+ launch Streamlit with your Streamlit script, you can attach the debugger by
111+ clicking the ** Attach to Streamlit at localhost:5678** button in the VS Code
112+ ** Run and Debug** tab. Once attached, you can set breakpoints and use all
113+ standard debugging features in your Streamlit script, as well as in any nipanel
114+ code invoked by the Streamlit script.
115+
57116# Developer Certificate of Origin (DCO)
58117
59118 Developer's Certificate of Origin 1.1
0 commit comments