If you are using a visual debugger (like the one available in Visual Studio Code), you can attach the debugger to the process running your Bokeh code at the section of code you’re interested in.
In case anyone finds this useful later, here are the steps I use to debug my Bokeh server applications in Visual Studio Code.
Steps
In the place where you’d like to start debugging, use an environment variable to decide whether to wait for debug attach:
# my_app.py
import ptvsd
...
# attach to VS Code debugger if this script was run with BOKEH_VS_DEBUG=true
# (place this just before the code you're interested in)
if os.environ['BOKEH_VS_DEBUG'] == 'true':
# 5678 is the default attach port in the VS Code debug configurations
print('Waiting for debugger attach')
ptvsd.enable_attach(address=('localhost', 5678), redirect_output=True)
ptvsd.wait_for_attach()
Add a debug configuration to VS Code:
// launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Bokeh server",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"preLaunchTask": "launch Bokeh server",
"postDebugTask": "kill Bokeh server"
}
]
}
// tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "launch Bokeh server",
"type": "shell",
"isBackground": true,
"command": "bokeh",
"args": [
"serve",
"my_app.py"
],
"options": {
"env": {
"BOKEH_VS_DEBUG": "true"
}
},
// you have to allow the task to not complete by specifying a complete problem matcher
"problemMatcher": {
"fileLocation": [
"relative",
"${workspaceFolder}"
],
"pattern": [
{
"regexp": ".",
"file": 1,
"location": 2,
"message": 3
}
],
"background": {
"activeOnStart": true,
"endsPattern": "^.*Waiting for debugger attach.*",
"beginsPattern": "^this should not match on anything"
}
}
}
]
}
Set any desired breakpoints, press the “Start debugging” button, and then interact with the application until the breakpoint occurs.