'Debugging Bokeh serve application using PyCharm
Bokeh serve allows to write fast web apps with plots and widgets.
How can I debug the python code when I use bokeh serve --show code.py?
Solution 1:[1]
Here's how I did it:
Updated to Pycharm CE 2017
At command prompt, run:
$ which bokeh /Users/myname/envs/my_venv/bin/bokeh
From menu selected : Run > Edit Configurations
Entered the path from step 2 into "Script path:"
Filled in "Parameters:"
serve --show /full/path/to/viz.py --args myargs
This opened a browser window http://localhost:5006/viz and stopped at the breakpoint set inside viz.py
Hope that works for you
Solution 2:[2]
I'm not really an IDE user, so I can't really say how to get things working with pycharm and the bokeh serve app.py way of running apps. However, as of 0.12.4 there is now guidance and examples for embedding a Bokeh server as a library. In particular you can create an app.py that you run in the "normal" way:
python app.py
My guess is that this way of doing things might work better with IDEs.
For reference, her is a complete "standalone script" embed:
import numpy as np
from tornado.ioloop import IOLoop
from bokeh.application.handlers import FunctionHandler
from bokeh.application import Application
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, Slider
from bokeh.plotting import figure
from bokeh.server.server import Server
io_loop = IOLoop.current()
def modify_doc(doc):
x = np.linspace(0, 10, 1000)
y = np.log(x) * np.sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))
plot = figure()
plot.line('x', 'y', source=source)
slider = Slider(start=1, end=10, value=1, step=0.1)
def callback(attr, old, new):
y = np.log(x) * np.sin(x*new)
source.data = dict(x=x, y=y)
slider.on_change('value', callback)
doc.add_root(column(slider, plot))
bokeh_app = Application(FunctionHandler(modify_doc))
server = Server({'/': bokeh_app}, io_loop=io_loop)
server.start()
if __name__ == '__main__':
print('Opening Bokeh application on http://localhost:5006/')
io_loop.add_callback(server.show, "/")
io_loop.start()
Solution 3:[3]
bokeh can also be run via python -m bokeh
given that, you could open up the Run/Debug Configuration dialog and set your interpreter options to -m bokeh serve --show and your script will run as-is
Solution 4:[4]
Another way is to run bokeh serve without arguments
bokeh serve
and push your app to the server. This allows you to debug the python script directly:
def update():
...
...
curdoc().add_periodic_callback(update,500)
session=push_session(curdoc())
session.show(plot)
session.loop_until_closed()
You will get a warning about push_session and loop_until_closed being discouraged. But for debugging purposes, it works well enough.
Solution 5:[5]
I found that you start the process in a process that already has the debugger attached to it, the debugger would be added there as well.
For example you can run the code below to do so
bokeh_process = subprocess.Popen(
['python', '-m', 'bokeh', 'serve', 'bokeh_server.py'], stdout=subprocess.PIPE)
Solution 6:[6]
The approach that worked best for me was using the "Python Debug Server" run config within IntelliJ 2021.2.4, and following the instructions there: https://www.jetbrains.com/help/idea/run-debug-configuration-python-remote-debug.html
Using this approach I added the settrace method call in the runner of my Bokeh app:
pydevd_pycharm.settrace('localhost', port=28091, stdoutToServer=True, stderrToServer=True)
Then I was able to debug it from IntelliJ while serving the Bokeh app from the cmd line:
bokeh serve my_app --address 0.0.0.0 --port 28090
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Giorgos Myrianthous |
| Solution 2 | bigreddot |
| Solution 3 | tvt173 |
| Solution 4 | jpmorris |
| Solution 5 | Alexander Wolf |
| Solution 6 | Steve |
