Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,6 @@ cython_debug/

# PyPI configuration file
.pypirc

# VS Code settings
.vscode/launch.json
43 changes: 43 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,49 @@ poetry run sphinx-build docs docs/_build --builder html --fail-on-warning
start docs\_build\index.html
```

# Debugging on the streamlit side

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:

## in the streamlit script, include this code snippet

```python
import debugpy # type: ignore
import streamlit as st

import nipanel

try:
debugpy.listen(("localhost", 5678))
debugpy.wait_for_client()
except RuntimeError as e:
if "debugpy.listen() has already been called on this process" not in str(e):
raise
```

`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.

`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.

The `import debugpy` line has a type suppression to make mypy happy.

## in launch.json, include this configuration

```json
{
"name": "Attach to Streamlit at localhost:5678",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
},
"justMyCode": false
}
```

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.

# Developer Certificate of Origin (DCO)

Developer's Certificate of Origin 1.1
Expand Down
5 changes: 4 additions & 1 deletion examples/sample/sample_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import nipanel

panel = nipanel.StreamlitPanelValueAccessor(panel_id="sample_panel")
panel = nipanel.initialize_panel()

st.title("Sample Panel")

Expand All @@ -17,6 +17,9 @@
st.write("Boolean")
st.write("List")

if st.button("Rerun app"):
st.rerun()

with col2:
st.write(panel.get_value("sample_string"))
st.write(panel.get_value("sample_int"))
Expand Down
193 changes: 114 additions & 79 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ni-measurement-plugin-sdk = {version=">=2.3"}
typing-extensions = ">=4.13.2"
streamlit = ">=1.24"
nitypes = {version=">=0.1.0dev1", allow-prereleases=true}
debugpy = "^1.8.1"

[tool.poetry.group.dev.dependencies]
types-grpcio = ">=1.0"
Expand Down
3 changes: 2 additions & 1 deletion src/nipanel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

from nipanel._panel import Panel
from nipanel._streamlit_panel import StreamlitPanel
from nipanel._streamlit_panel_initializer import initialize_panel
from nipanel._streamlit_panel_value_accessor import StreamlitPanelValueAccessor

__all__ = ["Panel", "StreamlitPanel", "StreamlitPanelValueAccessor"]
__all__ = ["Panel", "StreamlitPanel", "StreamlitPanelValueAccessor", "initialize_panel"]

# Hide that it was defined in a helper file
Panel.__module__ = __name__
Expand Down
25 changes: 25 additions & 0 deletions src/nipanel/_streamlit_panel_initializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import cast

import streamlit as st

from nipanel._streamlit_panel_value_accessor import StreamlitPanelValueAccessor


def initialize_panel() -> StreamlitPanelValueAccessor:
"""Initialize and return the Streamlit panel value accessor.
This function retrieves the Streamlit panel value accessor for the current Streamlit script.
It is typically used within a Streamlit script to access and manipulate panel values.
The accessor will be cached in the Streamlit session state to ensure that it is reused across
reruns of the script.
Returns:
A StreamlitPanelValueAccessor instance for the current panel.
"""
if "StreamlitPanelValueAccessor" not in st.session_state:
# streamlit is launched with something like --server.baseUrlPath=/my_panel,
# which allows us to get the panel ID from the URL.
panel_id = st.get_option("server.baseUrlPath").split("/")[-1]
st.session_state["StreamlitPanelValueAccessor"] = StreamlitPanelValueAccessor(panel_id)

return cast(StreamlitPanelValueAccessor, st.session_state["StreamlitPanelValueAccessor"])