As web applications grow more complex, visualizing and monitoring them becomes increasingly important. This is especially true for asynchronous Python web apps built with aiohttp. aiohttp allows handling multiple requests asynchronously and efficiently, but adds complexity when trying to visualize and debug your app.
That's where Bokeh comes in. Bokeh is a Python data visualization library that creates interactive visualizations in modern web browsers. By integrating Bokeh visualizations into your aiohttp web app, you can monitor and debug things like:
Integrating Bokeh
Integrating Bokeh is straightforward. First install Bokeh:
pip install bokeh
Then in your aiohttp app, create Bokeh figure instances and use the
Here is an example handler that visualizes active connections:
import aiohttp
from bokeh.plotting import figure
from bokeh.embed import json_item
app = aiohttp.web.Application()
@aiohttp_jinja2.template('index.html')
async def index(request):
p = figure(title="Active Connections")
p.line([1, 2, 3], [4, 5, 6])
plot_script = json_item(p)
return {"plot_script": plot_script}
The key is that the JSON can be directly included in your web templates and rendered into interactive plots by BokehJS.
Tips and Tricks
With some creativity, Bokeh gives you an interactive visual debugger right in your aiohttp web app!