Skip to content

Commit

Permalink
Add custom runserver command to enable debugpy (#590)
Browse files Browse the repository at this point in the history
Co-authored-by: Sam Dudley <samuel.dudley@digital.trade.gov.uk>
  • Loading branch information
smanga24 and SamDudley authored Jan 16, 2025
1 parent 59ecce2 commit 44f85bb
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 2 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
DJANGO_SETTINGS_MODULE=config.settings.local
DEBUGPY_ENABLED=False
SECRET_KEY=super-secret-key
ALLOWED_HOSTS="*"
DEBUG=True
Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,36 @@ The names of the management commands denote their function.
- Amend the custom_usermodel table to be the same as the new User app one
- Add the user app initial migration to the list of django migrations that have been run
- Deploy new codebase

# Setup DebugPy

Add environment variable in your .env file

```bash
ENABLE_DEBUGPY=True
```

Create launch.json file inside .vscode directory

```json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Remote Attach",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "0.0.0.0",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app/"
}
]
}
]
}
```
18 changes: 18 additions & 0 deletions core/management/commands/runserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os

from django.core.management.commands.runserver import Command as RunserverCommand


class Command(RunserverCommand):
def run(self, *args, **options):
if os.getenv("DEBUGPY_ENABLED", "False").lower() == "true":
if os.getenv("RUN_MAIN") or os.getenv("WERKZEUG_RUN_MAIN"):
import debugpy

try:
debugpy.listen(("0.0.0.0", 5678))
self.stdout.write("debugpy: listening on port 5678...\n")
except Exception as err:
self.stderr.write(f"debugpy: failed to initialize {err}\n")

super().run(*args, **options)
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ services:
command: python manage.py runserver 0.0.0.0:8000
# command: granian --interface wsgi config.wsgi:application --workers 2 --host 0.0.0.0 --port 8000
ports:
- "0.0.0.0:8000:8000"
- "8000:8000"
- "5678:5678"
volumes:
- ./:/app/
depends_on:
Expand Down
37 changes: 36 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ isort = "^5.10.1"
ruff = "^0.3.4"
mypy = "^1.13.0"
django-debug-toolbar = "^4.4.6"
debugpy = "1.8.9"

[build-system]
requires = ["poetry-core"]
Expand Down

0 comments on commit 44f85bb

Please sign in to comment.