Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit b5dd322

Browse files
committed
Update images and address feedback around multiple services
1 parent 747f255 commit b5dd322

File tree

6 files changed

+102
-15
lines changed

6 files changed

+102
-15
lines changed

docs/guides/python/debugging.mdx

Lines changed: 102 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,40 @@ This guide will walk you through setting up local debugging for Nitric applicati
1616

1717
Debugging serverless-style applications can be challenging due to the way functions are triggered by events. For Python, we can use [`debugpy`](https://github.com/microsoft/debugpy) to connect a debugger to the service while it runs.
1818

19-
### 1. Modify the Python entry point
19+
### 1. Add debugpy to our project
20+
21+
```bash
22+
uv add debugpy
23+
```
24+
25+
### 2. Modify the Python entry point
2026

2127
Add the following lines to the top of the service file (e.g., `services/api.py`). This starts a debug server that the IDE can attach to.
2228

29+
<Note>
30+
This code is for local development only and must not be included in production
31+
deployments.
32+
</Note>
33+
2334
```python
2435
import debugpy
2536

2637
host, port = debugpy.listen(("0.0.0.0", 52509)) # Static port for consistent debugging
2738
print(f"✅ Debugpy is listening on {host}:{port}", flush=True)
2839
```
2940

30-
> A **static port** (`52509`) is used so the IDE knows which port to connect to. Update the `launch.json` configuration to match this port before starting the debugger.
41+
<Note>
42+
A **static port** (`52509`) is used so the IDE knows which port to connect to.
43+
Update the `launch.json` configuration to match this port before starting the
44+
debugger.
45+
</Note>
3146

32-
### 2. Update `nitric.yaml`
47+
### 3. Update `nitric.yaml`
3348

34-
Modify the `start` command to include an auto-reloader and ensure Python does not use frozen modules, which can interfere with `debugpy`, E.g.
49+
Modify the `start` command to include an auto-reloader and ensure Python does not use frozen modules, which can interfere with `debugpy`:
3550

3651
```yaml
37-
name: message-board
52+
name: my-project
3853
services:
3954
- basedir: ''
4055
match: services/*.py
@@ -49,11 +64,14 @@ runtimes:
4964
args: {}
5065
```
5166
52-
> This configuration restarts the service on file changes and includes the necessary flags for debugging compatibility.
67+
<Note>
68+
This configuration restarts the service on file changes and includes the
69+
necessary flags for debugging compatibility.
70+
</Note>
5371
54-
### 3. Configure `launch.json` in VS Code
72+
### 4. Configure VS Code
5573
56-
VS Code uses a `launch.json` file to define how it should start or attach to a debugging session. In this case, the debugger doesn't launch the application itself—it attaches to the running service that was started manually from the terminal.
74+
VS Code uses a `.vscode/launch.json` file to define how it should start or attach to a debugging session. In this case, the debugger doesn't launch the application itself—it attaches to the running service that was started manually from the terminal.
5775

5876
To create or update the launch.json file:
5977

@@ -62,7 +80,7 @@ To create or update the launch.json file:
6280
- Choose "Python" when prompted for the environment.
6381
- Replace the default configuration with the following:
6482

65-
```json
83+
```json title: ./vscode/launch
6684
{
6785
"version": "0.2.0",
6886
"configurations": [
@@ -85,14 +103,83 @@ To create or update the launch.json file:
85103
}
86104
```
87105

88-
> Ensure the `port` matches the value used in the `debugpy.listen()` call. If the port changes in the code, update it here as well.
106+
<Note>
107+
Ensure the `port` matches the value used in the `debugpy.listen()` call. If
108+
the port changes in the code, update it here as well.
109+
</Note>
110+
111+
### 5. Running the debugger
112+
113+
Start your nitric service with `nitric start`, in the Terminal, both the Nitric runtime output and the debugpy listener output will be visible, including the active debug port.
114+
115+
![vscode](/docs/images/guides/python-debugging/terminal.png)
116+
117+
Now run the debugger and add breakpoints or watch variables.
118+
119+
![vscode](/docs/images/guides/python-debugging/debug.png)
120+
121+
### 6. Handling multiple services
122+
123+
Nitric applications typically have more than one service, however, `debugpy` only supports listening on a single (host, port) pair per service.
124+
125+
Your services will each have the following snippet with a unique port that maps to the config above.
126+
127+
```python
128+
import debugpy
89129

90-
### 4. Running the debugger
130+
host, port = debugpy.listen(("0.0.0.0", 52509))
131+
print(f"✅ Debugpy is listening on {host}:{port}", flush=True)
132+
```
91133

92-
Start your nitric service with `nitric start` and start the debugger.
134+
You can update your vscode configuration to use compounds, for example if you had two services:
93135

94-
![vscode](/docs/images/guides/python-debugging/debugger.png)
136+
```json title: ./vscode/launch
137+
{
138+
"version": "0.2.0",
139+
"compounds": [
140+
{
141+
"name": "Attach to Both Services",
142+
"configurations": [
143+
"Python Debugger: Service 1",
144+
"Python Debugger: Service 2"
145+
]
146+
}
147+
],
148+
"configurations": [
149+
{
150+
"name": "Python Debugger: Service 1",
151+
"type": "debugpy",
152+
"request": "attach",
153+
"connect": {
154+
"host": "localhost",
155+
"port": 52509
156+
},
157+
"pathMappings": [
158+
{
159+
"localRoot": "${workspaceFolder}",
160+
"remoteRoot": "."
161+
}
162+
]
163+
},
164+
{
165+
"name": "Python Debugger: Service 2",
166+
"type": "debugpy",
167+
"request": "attach",
168+
"connect": {
169+
"host": "localhost",
170+
"port": 52510
171+
},
172+
"pathMappings": [
173+
{
174+
"localRoot": "${workspaceFolder}",
175+
"remoteRoot": "."
176+
}
177+
]
178+
}
179+
]
180+
}
181+
```
95182

96-
In the Terminal, both the Nitric runtime output and the debugpy listener output will be visible, including the active debug port.
183+
Now in `Run and Debug` you can select `Attach to both`
97184

98-
In this example, a breakpoint has been hit on line 16 of `message_api.py`, within an HTTP handler for the `/messages` endpoint.
185+
![vscode](/docs/images/guides/python-debugging/attachboth.png)
31 KB
Loading
216 KB
Loading
264 KB
Loading
-600 KB
Binary file not shown.
76.2 KB
Loading

0 commit comments

Comments
 (0)